1
0
mirror of https://git.FreeBSD.org/src.git synced 2024-12-17 10:26:15 +00:00

Update to ELF Tool Chain r3272

Highlights (not already in the FreeBSD tree):
 - addr2line: Speed up and support searching inlined functions
 - addr2line: Support -i, -a, -p options
 - readelf: Add some ARM relocation types
 - readelf, libelf: Avoid reading beyond end of buffer/file

Relnotes:	Yes
Sponsored by:	The FreeBSD Foundation
This commit is contained in:
Ed Maste 2015-12-11 20:28:27 +00:00
commit 95fd7f2615
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=292120
10 changed files with 488 additions and 184 deletions

View File

@ -22,9 +22,9 @@
.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.\"
.\" $Id: addr2line.1 3195 2015-05-12 17:22:19Z emaste $
.\" $Id: addr2line.1 3263 2015-11-30 04:25:54Z kaiwang27 $
.\"
.Dd July 25, 2010
.Dd November 30, 2015
.Os
.Dt ADDR2LINE 1
.Sh NAME
@ -32,10 +32,13 @@
.Nd translate program addresses to source file names and line numbers
.Sh SYNOPSIS
.Nm
.Op Fl a | Fl -addresses
.Op Fl b Ar target | Fl -target Ns = Ns Ar target
.Op Fl e Ar pathname | Fl -exe Ns = Ns Ar pathname
.Op Fl f | Fl -functions
.Op Fl i | Fl -inlines
.Op Fl j Ar sectionname | Fl -section Ns = Ns Ar sectionname
.Op Fl p | Fl -pretty-print
.Op Fl s | Fl -basename
.Op Fl C | Fl -demangle
.Op Fl H | Fl -help
@ -69,6 +72,8 @@ The
.Nm
utility recognizes the following options:
.Bl -tag -width indent
.It Fl a | Fl -addresses
Display the address prior to the line number information.
.It Fl b Ar target | Fl -target Ns = Ns Ar target
This option is recognized by
.Nm
@ -84,11 +89,17 @@ will use the file
.Dq Pa a.out .
.It Fl f | Fl -functions
Display function names in addition to file and line number information.
.It Fl i | Fl -inlines
If the address specified belongs to an inlined function, also display the line
number information for its caller, recursively until the first non-inlined
caller.
.It Fl j Ar sectionname | Fl -section Ns = Ns Ar sectionname
The values specified by arguments
.Ar hexaddress
are to be treated as offsets into the section named
.Ar sectionname .
.It Fl p | -pretty-print
Display the line number information on one line, in human readable manner.
.It Fl s | -basename
Display only the base name for each file name.
.It Fl C | Fl -demangle
@ -115,6 +126,18 @@ to program address
.Ar hexaddress ,
followed by a line with the file name and line number.
.Pp
If the
.Fl p
option was specified,
.Nm
will print line number information and function name on one line in
human readable manner. If the
.Fl i
option was also specified,
.Nm
will print the caller function information prefixed with
.Dq (inlined by) .
.Pp
The
.Nm
utility prints the file name and line number using the format

View File

@ -37,33 +37,64 @@
#include <stdlib.h>
#include <string.h>
#include "uthash.h"
#include "_elftc.h"
ELFTC_VCSID("$Id: addr2line.c 3249 2015-10-04 08:11:30Z kaiwang27 $");
ELFTC_VCSID("$Id: addr2line.c 3264 2015-11-30 05:38:14Z kaiwang27 $");
struct Func {
char *name;
Dwarf_Unsigned lopc;
Dwarf_Unsigned hipc;
Dwarf_Unsigned call_file;
Dwarf_Unsigned call_line;
Dwarf_Ranges *ranges;
Dwarf_Signed ranges_cnt;
struct Func *inlined_caller;
STAILQ_ENTRY(Func) next;
};
struct CU {
Dwarf_Off off;
Dwarf_Unsigned lopc;
Dwarf_Unsigned hipc;
char **srcfiles;
Dwarf_Signed nsrcfiles;
STAILQ_HEAD(, Func) funclist;
UT_hash_handle hh;
};
static struct option longopts[] = {
{"addresses", no_argument, NULL, 'a'},
{"target" , required_argument, NULL, 'b'},
{"demangle", no_argument, NULL, 'C'},
{"exe", required_argument, NULL, 'e'},
{"functions", no_argument, NULL, 'f'},
{"inlines", no_argument, NULL, 'i'},
{"section", required_argument, NULL, 'j'},
{"pretty-print", no_argument, NULL, 'p'},
{"basename", no_argument, NULL, 's'},
{"help", no_argument, NULL, 'H'},
{"version", no_argument, NULL, 'V'},
{NULL, 0, NULL, 0}
};
static int demangle, func, base;
static int demangle, func, base, inlines, print_addr, pretty_print;
static char unknown[] = { '?', '?', '\0' };
static Dwarf_Addr section_base;
static struct CU *culist;
#define USAGE_MESSAGE "\
Usage: %s [options] hexaddress...\n\
Map program addresses to source file names and line numbers.\n\n\
Options:\n\
-a | --addresses Display address prior to line number info.\n\
-b TGT | --target=TGT (Accepted but ignored).\n\
-e EXE | --exe=EXE Use program \"EXE\" to translate addresses.\n\
-f | --functions Display function names.\n\
-i | --inlines Display caller info for inlined functions.\n\
-j NAME | --section=NAME Values are offsets into section \"NAME\".\n\
-p | --pretty-print Display line number info and function name\n\
in human readable manner.\n\
-s | --basename Only show the base name for each file name.\n\
-C | --demangle Demangle C++ names.\n\
-H | --help Print a help message.\n\
@ -122,71 +153,160 @@ handle_high_pc(Dwarf_Die die, Dwarf_Unsigned lopc, Dwarf_Unsigned *hipc)
return (DW_DLV_OK);
}
static void
search_func(Dwarf_Debug dbg, Dwarf_Die die, Dwarf_Addr addr, char **rlt_func)
static struct Func *
search_func(struct CU *cu, Dwarf_Unsigned addr)
{
Dwarf_Die ret_die, spec_die;
struct Func *f, *f0;
Dwarf_Unsigned lopc, hipc, addr_base;
int i;
f0 = NULL;
STAILQ_FOREACH(f, &cu->funclist, next) {
if (f->ranges != NULL) {
addr_base = 0;
for (i = 0; i < f->ranges_cnt; i++) {
if (f->ranges[i].dwr_type == DW_RANGES_END)
break;
if (f->ranges[i].dwr_type ==
DW_RANGES_ADDRESS_SELECTION) {
addr_base = f->ranges[i].dwr_addr2;
continue;
}
/* DW_RANGES_ENTRY */
lopc = f->ranges[i].dwr_addr1 + addr_base;
hipc = f->ranges[i].dwr_addr2 + addr_base;
if (addr >= lopc && addr < hipc) {
if (f0 == NULL ||
(lopc >= f0->lopc &&
hipc <= f0->hipc)) {
f0 = f;
f0->lopc = lopc;
f0->hipc = hipc;
break;
}
}
}
} else if (addr >= f->lopc && addr < f->hipc) {
if (f0 == NULL ||
(f->lopc >= f0->lopc && f->hipc <= f0->hipc))
f0 = f;
}
}
return (f0);
}
static void
collect_func(Dwarf_Debug dbg, Dwarf_Die die, struct Func *parent, struct CU *cu)
{
Dwarf_Die ret_die, abst_die, spec_die;
Dwarf_Error de;
Dwarf_Half tag;
Dwarf_Unsigned lopc, hipc;
Dwarf_Unsigned lopc, hipc, ranges_off;
Dwarf_Signed ranges_cnt;
Dwarf_Off ref;
Dwarf_Attribute sub_at, spec_at;
char *func0;
const char *func1;
int ret;
Dwarf_Attribute abst_at, spec_at;
Dwarf_Ranges *ranges;
const char *funcname;
struct Func *f;
int found_ranges, ret;
if (*rlt_func != NULL)
goto done;
f = NULL;
abst_die = spec_die = NULL;
if (dwarf_tag(die, &tag, &de)) {
warnx("dwarf_tag: %s", dwarf_errmsg(de));
goto cont_search;
}
if (tag == DW_TAG_subprogram) {
if (tag == DW_TAG_subprogram || tag == DW_TAG_entry_point ||
tag == DW_TAG_inlined_subroutine) {
/*
* Function address range can be specified by either
* a DW_AT_ranges attribute which points to a range list or
* by a pair of DW_AT_low_pc and DW_AT_high_pc attributes.
*/
ranges = NULL;
ranges_cnt = 0;
found_ranges = 0;
if (dwarf_attrval_unsigned(die, DW_AT_ranges, &ranges_off,
&de) == DW_DLV_OK &&
dwarf_get_ranges(dbg, (Dwarf_Off) ranges_off, &ranges,
&ranges_cnt, NULL, &de) == DW_DLV_OK) {
if (ranges != NULL && ranges_cnt > 0) {
found_ranges = 1;
goto get_func_name;
}
}
/*
* Search for DW_AT_low_pc/DW_AT_high_pc if ranges pointer
* not found.
*/
if (dwarf_attrval_unsigned(die, DW_AT_low_pc, &lopc, &de) ||
dwarf_attrval_unsigned(die, DW_AT_high_pc, &hipc, &de))
goto cont_search;
if (handle_high_pc(die, lopc, &hipc) != DW_DLV_OK)
goto cont_search;
if (addr < lopc || addr >= hipc)
goto cont_search;
/* Found it! */
get_func_name:
/*
* Most common case the function name is stored in DW_AT_name
* attribute.
*/
if (dwarf_attrval_string(die, DW_AT_name, &funcname, &de) ==
DW_DLV_OK)
goto add_func;
if ((*rlt_func = strdup(unknown)) == NULL)
err(EXIT_FAILURE, "strdup");
ret = dwarf_attr(die, DW_AT_name, &sub_at, &de);
if (ret == DW_DLV_ERROR)
goto done;
if (ret == DW_DLV_OK) {
if (dwarf_formstring(sub_at, &func0, &de) ==
DW_DLV_OK) {
free(*rlt_func);
if ((*rlt_func = strdup(func0)) == NULL)
err(EXIT_FAILURE, "strdup");
}
goto done;
}
/*
* For inlined function, the actual name is probably in the DIE
* referenced by DW_AT_abstract_origin. (if present)
*/
if (dwarf_attr(die, DW_AT_abstract_origin, &abst_at, &de) ==
DW_DLV_OK &&
dwarf_global_formref(abst_at, &ref, &de) == DW_DLV_OK &&
dwarf_offdie(dbg, ref, &abst_die, &de) == DW_DLV_OK &&
dwarf_attrval_string(abst_die, DW_AT_name, &funcname,
&de) == DW_DLV_OK)
goto add_func;
/*
* If DW_AT_name is not present, but DW_AT_specification is
* present, then probably the actual name is in the DIE
* referenced by DW_AT_specification.
*/
if (dwarf_attr(die, DW_AT_specification, &spec_at, &de))
goto done;
if (dwarf_global_formref(spec_at, &ref, &de))
goto done;
if (dwarf_offdie(dbg, ref, &spec_die, &de))
goto done;
if (dwarf_attrval_string(spec_die, DW_AT_name, &func1, &de) ==
DW_DLV_OK) {
free(*rlt_func);
if ((*rlt_func = strdup(func1)) == NULL)
err(EXIT_FAILURE, "strdup");
}
if (dwarf_attr(die, DW_AT_specification, &spec_at, &de) ==
DW_DLV_OK &&
dwarf_global_formref(spec_at, &ref, &de) == DW_DLV_OK &&
dwarf_offdie(dbg, ref, &spec_die, &de) == DW_DLV_OK &&
dwarf_attrval_string(spec_die, DW_AT_name, &funcname,
&de) == DW_DLV_OK)
goto add_func;
goto done;
/* Skip if no name assoicated with this DIE. */
goto cont_search;
add_func:
if ((f = calloc(1, sizeof(*f))) == NULL)
err(EXIT_FAILURE, "calloc");
if ((f->name = strdup(funcname)) == NULL)
err(EXIT_FAILURE, "strdup");
if (found_ranges) {
f->ranges = ranges;
f->ranges_cnt = ranges_cnt;
} else {
f->lopc = lopc;
f->hipc = hipc;
}
if (tag == DW_TAG_inlined_subroutine) {
f->inlined_caller = parent;
dwarf_attrval_unsigned(die, DW_AT_call_file,
&f->call_file, &de);
dwarf_attrval_unsigned(die, DW_AT_call_line,
&f->call_line, &de);
}
STAILQ_INSERT_TAIL(&cu->funclist, f, next);
}
cont_search:
@ -194,23 +314,69 @@ search_func(Dwarf_Debug dbg, Dwarf_Die die, Dwarf_Addr addr, char **rlt_func)
/* Search children. */
ret = dwarf_child(die, &ret_die, &de);
if (ret == DW_DLV_ERROR)
errx(EXIT_FAILURE, "dwarf_child: %s", dwarf_errmsg(de));
else if (ret == DW_DLV_OK)
search_func(dbg, ret_die, addr, rlt_func);
warnx("dwarf_child: %s", dwarf_errmsg(de));
else if (ret == DW_DLV_OK) {
if (f != NULL)
collect_func(dbg, ret_die, f, cu);
else
collect_func(dbg, ret_die, parent, cu);
}
/* Search sibling. */
ret = dwarf_siblingof(dbg, die, &ret_die, &de);
if (ret == DW_DLV_ERROR)
errx(EXIT_FAILURE, "dwarf_siblingof: %s", dwarf_errmsg(de));
warnx("dwarf_siblingof: %s", dwarf_errmsg(de));
else if (ret == DW_DLV_OK)
search_func(dbg, ret_die, addr, rlt_func);
collect_func(dbg, ret_die, parent, cu);
done:
/* Cleanup */
dwarf_dealloc(dbg, die, DW_DLA_DIE);
if (abst_die != NULL)
dwarf_dealloc(dbg, abst_die, DW_DLA_DIE);
if (spec_die != NULL)
dwarf_dealloc(dbg, spec_die, DW_DLA_DIE);
}
static void
translate(Dwarf_Debug dbg, const char* addrstr)
print_inlines(struct CU *cu, struct Func *f, Dwarf_Unsigned call_file,
Dwarf_Unsigned call_line)
{
char demangled[1024];
char *file;
if (call_file > 0 && (Dwarf_Signed) call_file <= cu->nsrcfiles)
file = cu->srcfiles[call_file - 1];
else
file = unknown;
if (pretty_print)
printf(" (inlined by) ");
if (func) {
if (demangle && !elftc_demangle(f->name, demangled,
sizeof(demangled), 0)) {
if (pretty_print)
printf("%s at ", demangled);
else
printf("%s\n", demangled);
} else {
if (pretty_print)
printf("%s at ", f->name);
else
printf("%s\n", f->name);
}
}
(void) printf("%s:%ju\n", base ? basename(file) : file, call_line);
if (f->inlined_caller != NULL)
print_inlines(cu, f->inlined_caller, f->call_file,
f->call_line);
}
static void
translate(Dwarf_Debug dbg, Elf *e, const char* addrstr)
{
Dwarf_Die die, ret_die;
Dwarf_Line *lbuf;
@ -219,18 +385,20 @@ translate(Dwarf_Debug dbg, const char* addrstr)
Dwarf_Unsigned lopc, hipc, addr, lineno, plineno;
Dwarf_Signed lcount;
Dwarf_Addr lineaddr, plineaddr;
char *funcname;
Dwarf_Off off;
struct CU *cu;
struct Func *f;
const char *funcname;
char *file, *file0, *pfile;
char demangled[1024];
int i, ret;
int ec, i, ret;
addr = strtoull(addrstr, NULL, 16);
addr += section_base;
lineno = 0;
file = unknown;
cu = NULL;
die = NULL;
lbuf = NULL;
lcount = 0;
while ((ret = dwarf_next_cu_header(dbg, NULL, NULL, NULL, NULL, NULL,
&de)) == DW_DLV_OK) {
@ -253,59 +421,46 @@ translate(Dwarf_Debug dbg, const char* addrstr)
warnx("could not find DW_TAG_compile_unit die");
goto next_cu;
}
if (!dwarf_attrval_unsigned(die, DW_AT_low_pc, &lopc, &de) &&
!dwarf_attrval_unsigned(die, DW_AT_high_pc, &hipc, &de)) {
if (dwarf_attrval_unsigned(die, DW_AT_low_pc, &lopc, &de) ==
DW_DLV_OK) {
if (dwarf_attrval_unsigned(die, DW_AT_high_pc, &hipc,
&de) == DW_DLV_OK) {
/*
* Check if the address falls into the PC
* range of this CU.
*/
if (handle_high_pc(die, lopc, &hipc) !=
DW_DLV_OK)
goto out;
} else {
/* Assume ~0ULL if DW_AT_high_pc not present */
hipc = ~0ULL;
}
/*
* Check if the address falls into the PC range of
* this CU.
* Record the CU in the hash table for faster lookup
* later.
*/
if (handle_high_pc(die, lopc, &hipc) != DW_DLV_OK)
goto next_cu;
if (addr < lopc || addr >= hipc)
goto next_cu;
if (dwarf_dieoffset(die, &off, &de) != DW_DLV_OK) {
warnx("dwarf_dieoffset failed: %s",
dwarf_errmsg(de));
goto out;
}
HASH_FIND(hh, culist, &off, sizeof(off), cu);
if (cu == NULL) {
if ((cu = calloc(1, sizeof(*cu))) == NULL)
err(EXIT_FAILURE, "calloc");
cu->off = off;
cu->lopc = lopc;
cu->hipc = hipc;
STAILQ_INIT(&cu->funclist);
HASH_ADD(hh, culist, off, sizeof(off), cu);
}
if (addr >= lopc && addr < hipc)
break;
}
switch (dwarf_srclines(die, &lbuf, &lcount, &de)) {
case DW_DLV_OK:
break;
case DW_DLV_NO_ENTRY:
/* If a CU lacks debug info, just skip it. */
goto next_cu;
default:
warnx("dwarf_srclines: %s", dwarf_errmsg(de));
goto out;
}
plineaddr = ~0ULL;
plineno = 0;
pfile = unknown;
for (i = 0; i < lcount; i++) {
if (dwarf_lineaddr(lbuf[i], &lineaddr, &de)) {
warnx("dwarf_lineaddr: %s",
dwarf_errmsg(de));
goto out;
}
if (dwarf_lineno(lbuf[i], &lineno, &de)) {
warnx("dwarf_lineno: %s",
dwarf_errmsg(de));
goto out;
}
if (dwarf_linesrc(lbuf[i], &file0, &de)) {
warnx("dwarf_linesrc: %s",
dwarf_errmsg(de));
} else
file = file0;
if (addr == lineaddr)
goto out;
else if (addr < lineaddr && addr > plineaddr) {
lineno = plineno;
file = pfile;
goto out;
}
plineaddr = lineaddr;
plineno = lineno;
pfile = file;
}
next_cu:
if (die != NULL) {
dwarf_dealloc(dbg, die, DW_DLA_DIE);
@ -313,27 +468,107 @@ translate(Dwarf_Debug dbg, const char* addrstr)
}
}
if (ret != DW_DLV_OK || die == NULL)
goto out;
switch (dwarf_srclines(die, &lbuf, &lcount, &de)) {
case DW_DLV_OK:
break;
case DW_DLV_NO_ENTRY:
/* If a CU lacks debug info, just skip it. */
goto out;
default:
warnx("dwarf_srclines: %s", dwarf_errmsg(de));
goto out;
}
plineaddr = ~0ULL;
plineno = 0;
pfile = unknown;
for (i = 0; i < lcount; i++) {
if (dwarf_lineaddr(lbuf[i], &lineaddr, &de)) {
warnx("dwarf_lineaddr: %s", dwarf_errmsg(de));
goto out;
}
if (dwarf_lineno(lbuf[i], &lineno, &de)) {
warnx("dwarf_lineno: %s", dwarf_errmsg(de));
goto out;
}
if (dwarf_linesrc(lbuf[i], &file0, &de)) {
warnx("dwarf_linesrc: %s", dwarf_errmsg(de));
} else
file = file0;
if (addr == lineaddr)
goto out;
else if (addr < lineaddr && addr > plineaddr) {
lineno = plineno;
file = pfile;
goto out;
}
plineaddr = lineaddr;
plineno = lineno;
pfile = file;
}
out:
f = NULL;
funcname = NULL;
if (ret == DW_DLV_OK && func) {
search_func(dbg, die, addr, &funcname);
die = NULL;
if (ret == DW_DLV_OK && (func || inlines) && cu != NULL) {
if (cu->srcfiles == NULL)
if (dwarf_srcfiles(die, &cu->srcfiles, &cu->nsrcfiles,
&de))
warnx("dwarf_srcfiles: %s", dwarf_errmsg(de));
if (STAILQ_EMPTY(&cu->funclist)) {
collect_func(dbg, die, NULL, cu);
die = NULL;
}
f = search_func(cu, addr);
if (f != NULL)
funcname = f->name;
}
if (print_addr) {
if ((ec = gelf_getclass(e)) == ELFCLASSNONE) {
warnx("gelf_getclass failed: %s", elf_errmsg(-1));
ec = ELFCLASS64;
}
if (ec == ELFCLASS32) {
if (pretty_print)
printf("0x%08jx: ", (uintmax_t) addr);
else
printf("0x%08jx\n", (uintmax_t) addr);
} else {
if (pretty_print)
printf("0x%016jx: ", (uintmax_t) addr);
else
printf("0x%016jx\n", (uintmax_t) addr);
}
}
if (func) {
if (funcname == NULL)
if ((funcname = strdup(unknown)) == NULL)
err(EXIT_FAILURE, "strdup");
if (demangle &&
!elftc_demangle(funcname, demangled, sizeof(demangled), 0))
printf("%s\n", demangled);
else
printf("%s\n", funcname);
free(funcname);
funcname = unknown;
if (demangle && !elftc_demangle(funcname, demangled,
sizeof(demangled), 0)) {
if (pretty_print)
printf("%s at ", demangled);
else
printf("%s\n", demangled);
} else {
if (pretty_print)
printf("%s at ", funcname);
else
printf("%s\n", funcname);
}
}
(void) printf("%s:%ju\n", base ? basename(file) : file, lineno);
if (ret == DW_DLV_OK && inlines && cu != NULL &&
cu->srcfiles != NULL && f != NULL && f->inlined_caller != NULL)
print_inlines(cu, f->inlined_caller, f->call_file,
f->call_line);
if (die != NULL)
dwarf_dealloc(dbg, die, DW_DLA_DIE);
@ -421,9 +656,12 @@ main(int argc, char **argv)
exe = NULL;
section = NULL;
while ((opt = getopt_long(argc, argv, "b:Ce:fj:sHV", longopts, NULL)) !=
-1) {
while ((opt = getopt_long(argc, argv, "ab:Ce:fij:psHV", longopts,
NULL)) != -1) {
switch (opt) {
case 'a':
print_addr = 1;
break;
case 'b':
/* ignored */
break;
@ -436,9 +674,15 @@ main(int argc, char **argv)
case 'f':
func = 1;
break;
case 'i':
inlines = 1;
break;
case 'j':
section = optarg;
break;
case 'p':
pretty_print = 1;
break;
case 's':
base = 1;
break;
@ -473,10 +717,10 @@ main(int argc, char **argv)
if (argc > 0)
for (i = 0; i < argc; i++)
translate(dbg, argv[i]);
translate(dbg, e, argv[i]);
else
while (fgets(line, sizeof(line), stdin) != NULL) {
translate(dbg, line);
translate(dbg, e, line);
fflush(stdout);
}

View File

@ -23,7 +23,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: elfdefinitions.h 3247 2015-09-22 16:57:51Z emaste $
* $Id: elfdefinitions.h 3253 2015-10-10 18:31:33Z kaiwang27 $
*/
/*

View File

@ -35,7 +35,7 @@
#include "elfcopy.h"
ELFTC_VCSID("$Id: binary.c 3174 2015-03-27 17:13:41Z emaste $");
ELFTC_VCSID("$Id: binary.c 3270 2015-12-11 18:48:56Z emaste $");
/*
* Convert ELF object to `binary'. Sections with SHF_ALLOC flag set

View File

@ -21,9 +21,9 @@
.\" out of the use of this software, even if advised of the possibility of
.\" such damage.
.\"
.\" $Id: elfcopy.1 3195 2015-05-12 17:22:19Z emaste $
.\" $Id: elfcopy.1 3266 2015-12-07 15:38:26Z emaste $
.\"
.Dd March 27, 2015
.Dd December 7, 2015
.Os
.Dt ELFCOPY 1
.Sh NAME
@ -47,6 +47,7 @@
.Op Fl p | Fl -preserve-dates
.Op Fl w | Fl -wildcard
.Op Fl x | Fl -discard-all
.Op Fl -add-gnu-debuglink Ns = Ns Ar filename
.Op Fl -add-section Ar sectionname Ns = Ns Ar filename
.Oo
.Fl -adjust-section-vma Ar section Ns {+|-|=} Ns Ar val |
@ -165,6 +166,10 @@ Mark the end of a character class.
.El
.It Fl x | Fl -discard-all
Do not copy non-global symbols to the output.
.It Fl -add-gnu-debuglink Ns = Ns Ar filename
Create a .gnu_debuglink section in the output file that references the
debug data in
.Ar filename .
.It Fl -add-section Ar sectionname Ns = Ns Ar filename
Add a new section to the output file with name
.Ar sectionname .

View File

@ -39,7 +39,7 @@
#include "elfcopy.h"
ELFTC_VCSID("$Id: main.c 3216 2015-05-23 21:16:36Z kaiwang27 $");
ELFTC_VCSID("$Id: main.c 3268 2015-12-07 20:30:55Z emaste $");
enum options
{
@ -1375,11 +1375,13 @@ Usage: %s [options] infile [outfile]\n\
-w | --wildcard Use shell-style patterns to name symbols.\n\
-x | --discard-all Do not copy non-globals to the output.\n\
-I FORMAT | --input-target=FORMAT\n\
(Accepted but ignored).\n\
Specify object format for the input file.\n\
-K SYM | --keep-symbol=SYM Copy symbol SYM to the output.\n\
-L SYM | --localize-symbol=SYM\n\
Make symbol SYM local to the output file.\n\
-N SYM | --strip-symbol=SYM Do not copy symbol SYM to the output.\n\
-O FORMAT | --output-target=FORMAT\n\
Specify object format for the output file.\n\
-R NAME | --remove-section=NAME\n\
Remove the named section.\n\
-S | --strip-all Remove all symbol and relocation information\n\

View File

@ -34,7 +34,7 @@
#include "elfcopy.h"
ELFTC_VCSID("$Id: sections.c 3225 2015-06-06 02:35:23Z kaiwang27 $");
ELFTC_VCSID("$Id: sections.c 3272 2015-12-11 20:00:54Z kaiwang27 $");
static void add_gnu_debuglink(struct elfcopy *ecp);
static uint32_t calc_crc32(const char *p, size_t len, uint32_t crc);
@ -457,11 +457,17 @@ create_scn(struct elfcopy *ecp)
/*
* If strip action is STRIP_NONDEBUG(only keep debug),
* change sections flags of loadable sections to SHF_NOBITS,
* and the content of those sections will be ignored.
* change sections type of loadable sections and section
* groups to SHT_NOBITS, and the content of those sections
* will be discarded. However, SHT_NOTE sections should
* be kept.
*/
if (ecp->strip == STRIP_NONDEBUG && (ish.sh_flags & SHF_ALLOC))
s->type = SHT_NOBITS;
if (ecp->strip == STRIP_NONDEBUG) {
if (((ish.sh_flags & SHF_ALLOC) ||
(ish.sh_flags & SHF_GROUP)) &&
ish.sh_type != SHT_NOTE)
s->type = SHT_NOBITS;
}
check_section_rename(ecp, s);

View File

@ -34,7 +34,7 @@
#include "elfcopy.h"
ELFTC_VCSID("$Id: segments.c 3196 2015-05-12 17:33:48Z emaste $");
ELFTC_VCSID("$Id: segments.c 3269 2015-12-11 18:38:43Z kaiwang27 $");
static void insert_to_inseg_list(struct segment *seg, struct section *sec);
@ -77,8 +77,6 @@ add_to_inseg_list(struct elfcopy *ecp, struct section *s)
if (s->off + s->sz > seg->off + seg->fsz &&
s->type != SHT_NOBITS)
continue;
if (s->off + s->sz > seg->off + seg->msz)
continue;
if (s->vma + s->sz > seg->addr + seg->msz)
continue;

View File

@ -32,7 +32,7 @@
#include "_libelf.h"
ELFTC_VCSID("$Id: elf_data.c 3177 2015-03-30 18:19:41Z emaste $");
ELFTC_VCSID("$Id: elf_data.c 3258 2015-11-20 18:59:43Z emaste $");
Elf_Data *
elf_getdata(Elf_Scn *s, Elf_Data *ed)
@ -253,6 +253,12 @@ elf_rawdata(Elf_Scn *s, Elf_Data *ed)
return (NULL);
}
if (sh_type != SHT_NOBITS &&
sh_offset + sh_size > (uint64_t) e->e_rawsize) {
LIBELF_SET_ERROR(SECTION, 0);
return (NULL);
}
if ((d = _libelf_allocate_data(s)) == NULL)
return (NULL);

View File

@ -47,7 +47,7 @@
#include "_elftc.h"
ELFTC_VCSID("$Id: readelf.c 3250 2015-10-06 13:56:15Z emaste $");
ELFTC_VCSID("$Id: readelf.c 3271 2015-12-11 18:53:08Z kaiwang27 $");
/*
* readelf(1) options.
@ -256,7 +256,7 @@ static const char *dt_type(unsigned int mach, unsigned int dtype);
static void dump_ar(struct readelf *re, int);
static void dump_arm_attributes(struct readelf *re, uint8_t *p, uint8_t *pe);
static void dump_attributes(struct readelf *re);
static uint8_t *dump_compatibility_tag(uint8_t *p);
static uint8_t *dump_compatibility_tag(uint8_t *p, uint8_t *pe);
static void dump_dwarf(struct readelf *re);
static void dump_dwarf_abbrev(struct readelf *re);
static void dump_dwarf_aranges(struct readelf *re);
@ -306,7 +306,7 @@ static void dump_ppc_attributes(uint8_t *p, uint8_t *pe);
static void dump_section_groups(struct readelf *re);
static void dump_symtab(struct readelf *re, int i);
static void dump_symtabs(struct readelf *re);
static uint8_t *dump_unknown_tag(uint64_t tag, uint8_t *p);
static uint8_t *dump_unknown_tag(uint64_t tag, uint8_t *p, uint8_t *pe);
static void dump_ver(struct readelf *re);
static void dump_verdef(struct readelf *re, int dump);
static void dump_verneed(struct readelf *re, int dump);
@ -358,8 +358,8 @@ static uint64_t _read_msb(Elf_Data *d, uint64_t *offsetp,
int bytes_to_read);
static uint64_t _decode_lsb(uint8_t **data, int bytes_to_read);
static uint64_t _decode_msb(uint8_t **data, int bytes_to_read);
static int64_t _decode_sleb128(uint8_t **dp);
static uint64_t _decode_uleb128(uint8_t **dp);
static int64_t _decode_sleb128(uint8_t **dp, uint8_t *dpe);
static uint64_t _decode_uleb128(uint8_t **dp, uint8_t *dpe);
static struct eflags_desc arm_eflags_desc[] = {
{EF_ARM_RELEXEC, "relocatable executable"},
@ -1171,10 +1171,14 @@ r_type(unsigned int mach, unsigned int type)
case 10: return "R_ARM_THM_PC22";
case 11: return "R_ARM_THM_PC8";
case 12: return "R_ARM_AMP_VCALL9";
case 13: return "R_ARM_SWI24";
case 13: return "R_ARM_TLS_DESC";
/* Obsolete R_ARM_SWI24 is also 13 */
case 14: return "R_ARM_THM_SWI8";
case 15: return "R_ARM_XPC25";
case 16: return "R_ARM_THM_XPC22";
case 17: return "R_ARM_TLS_DTPMOD32";
case 18: return "R_ARM_TLS_DTPOFF32";
case 19: return "R_ARM_TLS_TPOFF32";
case 20: return "R_ARM_COPY";
case 21: return "R_ARM_GLOB_DAT";
case 22: return "R_ARM_JUMP_SLOT";
@ -1183,6 +1187,17 @@ r_type(unsigned int mach, unsigned int type)
case 25: return "R_ARM_GOTPC";
case 26: return "R_ARM_GOT32";
case 27: return "R_ARM_PLT32";
case 28: return "R_ARM_CALL";
case 29: return "R_ARM_JUMP24";
case 30: return "R_ARM_THM_JUMP24";
case 31: return "R_ARM_BASE_ABS";
case 38: return "R_ARM_TARGET1";
case 40: return "R_ARM_V4BX";
case 42: return "R_ARM_PREL31";
case 43: return "R_ARM_MOVW_ABS_NC";
case 44: return "R_ARM_MOVT_ABS";
case 45: return "R_ARM_MOVW_PREL_NC";
case 46: return "R_ARM_MOVT_PREL";
case 100: return "R_ARM_GNU_VTENTRY";
case 101: return "R_ARM_GNU_VTINHERIT";
case 250: return "R_ARM_RSBREL32";
@ -2847,9 +2862,9 @@ dump_phdr(struct readelf *re)
printf(" %2.2d ", i);
/* skip NULL section. */
for (j = 1; (size_t)j < re->shnum; j++)
if (re->sl[j].off >= phdr.p_offset &&
re->sl[j].off + re->sl[j].sz <=
phdr.p_offset + phdr.p_memsz)
if (re->sl[j].addr >= phdr.p_vaddr &&
re->sl[j].addr + re->sl[j].sz <=
phdr.p_vaddr + phdr.p_memsz)
printf("%s ", re->sl[j].name);
printf("\n");
}
@ -4245,7 +4260,7 @@ dump_section_groups(struct readelf *re)
}
static uint8_t *
dump_unknown_tag(uint64_t tag, uint8_t *p)
dump_unknown_tag(uint64_t tag, uint8_t *p, uint8_t *pe)
{
uint64_t val;
@ -4262,7 +4277,7 @@ dump_unknown_tag(uint64_t tag, uint8_t *p)
printf("%s\n", (char *) p);
p += strlen((char *) p) + 1;
} else {
val = _decode_uleb128(&p);
val = _decode_uleb128(&p, pe);
printf("%ju\n", (uintmax_t) val);
}
@ -4270,11 +4285,11 @@ dump_unknown_tag(uint64_t tag, uint8_t *p)
}
static uint8_t *
dump_compatibility_tag(uint8_t *p)
dump_compatibility_tag(uint8_t *p, uint8_t *pe)
{
uint64_t val;
val = _decode_uleb128(&p);
val = _decode_uleb128(&p, pe);
printf("flag = %ju, vendor = %s\n", val, p);
p += strlen((char *) p) + 1;
@ -4291,7 +4306,7 @@ dump_arm_attributes(struct readelf *re, uint8_t *p, uint8_t *pe)
(void) re;
while (p < pe) {
tag = _decode_uleb128(&p);
tag = _decode_uleb128(&p, pe);
found = desc = 0;
for (i = 0; i < sizeof(aeabi_tags) / sizeof(aeabi_tags[0]);
i++) {
@ -4300,7 +4315,7 @@ dump_arm_attributes(struct readelf *re, uint8_t *p, uint8_t *pe)
printf(" %s: ", aeabi_tags[i].s_tag);
if (aeabi_tags[i].get_desc) {
desc = 1;
val = _decode_uleb128(&p);
val = _decode_uleb128(&p, pe);
printf("%s\n",
aeabi_tags[i].get_desc(val));
}
@ -4310,7 +4325,7 @@ dump_arm_attributes(struct readelf *re, uint8_t *p, uint8_t *pe)
break;
}
if (!found) {
p = dump_unknown_tag(tag, p);
p = dump_unknown_tag(tag, p, pe);
continue;
}
if (desc)
@ -4324,21 +4339,21 @@ dump_arm_attributes(struct readelf *re, uint8_t *p, uint8_t *pe)
p += strlen((char *) p) + 1;
break;
case 32: /* Tag_compatibility */
p = dump_compatibility_tag(p);
p = dump_compatibility_tag(p, pe);
break;
case 64: /* Tag_nodefaults */
/* ignored, written as 0. */
(void) _decode_uleb128(&p);
(void) _decode_uleb128(&p, pe);
printf("True\n");
break;
case 65: /* Tag_also_compatible_with */
val = _decode_uleb128(&p);
val = _decode_uleb128(&p, pe);
/* Must be Tag_CPU_arch */
if (val != 6) {
printf("unknown\n");
break;
}
val = _decode_uleb128(&p);
val = _decode_uleb128(&p, pe);
printf("%s\n", aeabi_cpu_arch(val));
/* Skip NUL terminator. */
p++;
@ -4362,17 +4377,17 @@ dump_mips_attributes(struct readelf *re, uint8_t *p, uint8_t *pe)
(void) re;
while (p < pe) {
tag = _decode_uleb128(&p);
tag = _decode_uleb128(&p, pe);
switch (tag) {
case Tag_GNU_MIPS_ABI_FP:
val = _decode_uleb128(&p);
val = _decode_uleb128(&p, pe);
printf(" Tag_GNU_MIPS_ABI_FP: %s\n", mips_abi_fp(val));
break;
case 32: /* Tag_compatibility */
p = dump_compatibility_tag(p);
p = dump_compatibility_tag(p, pe);
break;
default:
p = dump_unknown_tag(tag, p);
p = dump_unknown_tag(tag, p, pe);
break;
}
}
@ -4392,22 +4407,22 @@ dump_ppc_attributes(uint8_t *p, uint8_t *pe)
uint64_t tag, val;
while (p < pe) {
tag = _decode_uleb128(&p);
tag = _decode_uleb128(&p, pe);
switch (tag) {
case Tag_GNU_Power_ABI_FP:
val = _decode_uleb128(&p);
val = _decode_uleb128(&p, pe);
printf(" Tag_GNU_Power_ABI_FP: %s\n", ppc_abi_fp(val));
break;
case Tag_GNU_Power_ABI_Vector:
val = _decode_uleb128(&p);
val = _decode_uleb128(&p, pe);
printf(" Tag_GNU_Power_ABI_Vector: %s\n",
ppc_abi_vector(val));
break;
case 32: /* Tag_compatibility */
p = dump_compatibility_tag(p);
p = dump_compatibility_tag(p, pe);
break;
default:
p = dump_unknown_tag(tag, p);
p = dump_unknown_tag(tag, p, pe);
break;
}
}
@ -4418,7 +4433,7 @@ dump_attributes(struct readelf *re)
{
struct section *s;
Elf_Data *d;
uint8_t *p, *sp;
uint8_t *p, *pe, *sp;
size_t len, seclen, nlen, sublen;
uint64_t val;
int tag, i, elferr;
@ -4439,6 +4454,7 @@ dump_attributes(struct readelf *re)
if (d->d_size <= 0)
continue;
p = d->d_buf;
pe = p + d->d_size;
if (*p != 'A') {
printf("Unknown Attribute Section Format: %c\n",
(char) *p);
@ -4449,18 +4465,18 @@ dump_attributes(struct readelf *re)
while (len > 0) {
if (len < 4) {
warnx("truncated attribute section length");
break;
return;
}
seclen = re->dw_decode(&p, 4);
if (seclen > len) {
warnx("invalid attribute section length");
break;
return;
}
len -= seclen;
nlen = strlen((char *) p) + 1;
if (nlen + 4 > seclen) {
warnx("invalid attribute section name");
break;
return;
}
printf("Attribute Section: %s\n", (char *) p);
p += nlen;
@ -4472,14 +4488,14 @@ dump_attributes(struct readelf *re)
if (sublen > seclen) {
warnx("invalid attribute sub-section"
" length");
break;
return;
}
seclen -= sublen;
printf("%s", top_tag(tag));
if (tag == 2 || tag == 3) {
putchar(':');
for (;;) {
val = _decode_uleb128(&p);
val = _decode_uleb128(&p, pe);
if (val == 0)
break;
printf(" %ju", (uintmax_t) val);
@ -4842,9 +4858,9 @@ dump_dwarf_line(struct readelf *re)
i++;
pn = (char *) p;
p += strlen(pn) + 1;
dirndx = _decode_uleb128(&p);
mtime = _decode_uleb128(&p);
fsize = _decode_uleb128(&p);
dirndx = _decode_uleb128(&p, pe);
mtime = _decode_uleb128(&p, pe);
fsize = _decode_uleb128(&p, pe);
printf(" %d\t%ju\t%ju\t%ju\t%s\n", i,
(uintmax_t) dirndx, (uintmax_t) mtime,
(uintmax_t) fsize, pn);
@ -4876,7 +4892,7 @@ dump_dwarf_line(struct readelf *re)
* Extended Opcodes.
*/
p++;
opsize = _decode_uleb128(&p);
opsize = _decode_uleb128(&p, pe);
printf(" Extended opcode %u: ", *p);
switch (*p) {
case DW_LNE_end_sequence:
@ -4895,9 +4911,9 @@ dump_dwarf_line(struct readelf *re)
p++;
pn = (char *) p;
p += strlen(pn) + 1;
dirndx = _decode_uleb128(&p);
mtime = _decode_uleb128(&p);
fsize = _decode_uleb128(&p);
dirndx = _decode_uleb128(&p, pe);
mtime = _decode_uleb128(&p, pe);
fsize = _decode_uleb128(&p, pe);
printf("define new file: %s\n", pn);
break;
default:
@ -4914,7 +4930,7 @@ dump_dwarf_line(struct readelf *re)
printf(" Copy\n");
break;
case DW_LNS_advance_pc:
udelta = _decode_uleb128(&p) *
udelta = _decode_uleb128(&p, pe) *
minlen;
address += udelta;
printf(" Advance PC by %ju to %#jx\n",
@ -4922,19 +4938,19 @@ dump_dwarf_line(struct readelf *re)
(uintmax_t) address);
break;
case DW_LNS_advance_line:
sdelta = _decode_sleb128(&p);
sdelta = _decode_sleb128(&p, pe);
line += sdelta;
printf(" Advance Line by %jd to %ju\n",
(intmax_t) sdelta,
(uintmax_t) line);
break;
case DW_LNS_set_file:
file = _decode_uleb128(&p);
file = _decode_uleb128(&p, pe);
printf(" Set File to %ju\n",
(uintmax_t) file);
break;
case DW_LNS_set_column:
column = _decode_uleb128(&p);
column = _decode_uleb128(&p, pe);
printf(" Set Column to %ju\n",
(uintmax_t) column);
break;
@ -4967,7 +4983,7 @@ dump_dwarf_line(struct readelf *re)
printf(" Set epilogue begin flag\n");
break;
case DW_LNS_set_isa:
isa = _decode_uleb128(&p);
isa = _decode_uleb128(&p, pe);
printf(" Set isa to %ju\n", isa);
break;
default:
@ -7457,7 +7473,7 @@ _decode_msb(uint8_t **data, int bytes_to_read)
}
static int64_t
_decode_sleb128(uint8_t **dp)
_decode_sleb128(uint8_t **dp, uint8_t *dpe)
{
int64_t ret = 0;
uint8_t b;
@ -7466,6 +7482,8 @@ _decode_sleb128(uint8_t **dp)
uint8_t *src = *dp;
do {
if (src >= dpe)
break;
b = *src++;
ret |= ((b & 0x7f) << shift);
shift += 7;
@ -7480,7 +7498,7 @@ _decode_sleb128(uint8_t **dp)
}
static uint64_t
_decode_uleb128(uint8_t **dp)
_decode_uleb128(uint8_t **dp, uint8_t *dpe)
{
uint64_t ret = 0;
uint8_t b;
@ -7489,6 +7507,8 @@ _decode_uleb128(uint8_t **dp)
uint8_t *src = *dp;
do {
if (src >= dpe)
break;
b = *src++;
ret |= ((b & 0x7f) << shift);
shift += 7;