From 04e875d138b0f0687b75c3c2bd7ae8a22dfc293e Mon Sep 17 00:00:00 2001 From: kaiw Date: Sun, 13 Jun 2010 10:58:50 +0000 Subject: * Improve compatibility with existing application code by permitting the use of `elf_getbase()` on non-archive members. This change is needed for gcc LTO (-flto) to work properly. * Style fix: paranthesize returned values. * Document the current behaviour of `elf_getbase()`. Tested by: gerald, Steve Kargl (original patch) Obtained from: elftoolchain MFC after: 3 days --- lib/libelf/elf_getbase.3 | 24 ++++++++++++++---------- lib/libelf/elf_getbase.c | 8 +++++--- 2 files changed, 19 insertions(+), 13 deletions(-) (limited to 'lib') diff --git a/lib/libelf/elf_getbase.3 b/lib/libelf/elf_getbase.3 index be9d103..c19445d 100644 --- a/lib/libelf/elf_getbase.3 +++ b/lib/libelf/elf_getbase.3 @@ -1,4 +1,4 @@ -.\" Copyright (c) 2006 Joseph Koshy. All rights reserved. +.\" Copyright (c) 2006,2008,2010 Joseph Koshy. All rights reserved. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions @@ -23,7 +23,7 @@ .\" .\" $FreeBSD$ .\" -.Dd June 11, 2006 +.Dd June 6, 2010 .Dt ELF_GETBASE 3 .Os .Sh NAME @@ -38,17 +38,21 @@ .Sh DESCRIPTION Function .Fn elf_getbase -returns the file offset in the containing archive of the first byte of -the file referenced by ELF descriptor +returns the file offset to the first byte of the object referenced by ELF +descriptor .Ar elf . +.Pp +For descriptors referencing members of archives, the returned offset is +the file offset of the member in its containing archive. +For descriptors to regular objects, the returned offset is (vacuously) +zero. .Sh RETURN VALUES Function .Fn elf_getbase -returns a valid file offset into the containing archive if successful. -It returns -1 if argument -.Ar elf -is NULL or is not a member of an -archive. +returns a valid file offset if successful, or +.Pq Vt off_t +.Li -1 +in case of an error. .Sh ERRORS Function .Fn elf_getbase @@ -57,7 +61,7 @@ may fail with the following errors: .It Bq Er ELF_E_ARGUMENT Argument .Ar elf -is not an ELF descriptor for an archive member. +was NULL. .El .Sh SEE ALSO .Xr elf 3 , diff --git a/lib/libelf/elf_getbase.c b/lib/libelf/elf_getbase.c index d32fa6c..863c858 100644 --- a/lib/libelf/elf_getbase.c +++ b/lib/libelf/elf_getbase.c @@ -34,12 +34,14 @@ __FBSDID("$FreeBSD$"); off_t elf_getbase(Elf *e) { - if (e == NULL || - e->e_parent == NULL) { + if (e == NULL) { LIBELF_SET_ERROR(ARGUMENT, 0); - return (off_t) -1; + return ((off_t) -1); } + if (e->e_parent == NULL) + return ((off_t) 0); + return ((off_t) ((uintptr_t) e->e_rawfile - (uintptr_t) e->e_parent->e_rawfile)); } -- cgit v1.1