aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEd Maste <emaste@FreeBSD.org>2015-12-11 20:13:36 +0000
committerEd Maste <emaste@FreeBSD.org>2015-12-11 20:13:36 +0000
commit768a24a304ee84c17865b4348beacb61aa47af1b (patch)
treefa4048de2a3b89d666f609663721fe14dee0627a
parent91b92b626c28ca4664d62bd9c7bc84681d6202e3 (diff)
downloadsrc-768a24a304ee84c17865b4348beacb61aa47af1b.tar.gz
src-768a24a304ee84c17865b4348beacb61aa47af1b.zip
Import ELF Tool Chain snapshot revision r3272vendor/elftoolchain/elftoolchain-r3272
Notes
Notes: svn path=/vendor/elftoolchain/dist/; revision=292118 svn path=/vendor/elftoolchain/elftoolchain-r3272/; revision=292119; tag=vendor/elftoolchain/elftoolchain-r3272
-rw-r--r--addr2line/addr2line.127
-rw-r--r--addr2line/addr2line.c473
-rw-r--r--common/elfdefinitions.h53
-rw-r--r--elfcopy/binary.c11
-rw-r--r--elfcopy/elfcopy.19
-rw-r--r--elfcopy/main.c6
-rw-r--r--elfcopy/sections.c16
-rw-r--r--elfcopy/segments.c4
-rw-r--r--libelf/elf_data.c8
-rw-r--r--readelf/readelf.c161
10 files changed, 568 insertions, 200 deletions
diff --git a/addr2line/addr2line.1 b/addr2line/addr2line.1
index f000c2911d7b..15c24acb6b68 100644
--- a/addr2line/addr2line.1
+++ b/addr2line/addr2line.1
@@ -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
diff --git a/addr2line/addr2line.c b/addr2line/addr2line.c
index 9863fcdc634a..53105762565c 100644
--- a/addr2line/addr2line.c
+++ b/addr2line/addr2line.c
@@ -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 struct Func *
+search_func(struct CU *cu, Dwarf_Unsigned addr)
+{
+ 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
-search_func(Dwarf_Debug dbg, Dwarf_Die die, Dwarf_Addr addr, char **rlt_func)
+collect_func(Dwarf_Debug dbg, Dwarf_Die die, struct Func *parent, struct CU *cu)
{
- Dwarf_Die ret_die, spec_die;
+ 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;
+
+ /* Skip if no name assoicated with this DIE. */
+ goto cont_search;
- goto done;
+ 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 @@ cont_search:
/* 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
+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, const char* addrstr)
+translate(Dwarf_Debug dbg, Elf *e, const char* addrstr)
{
Dwarf_Die die, ret_die;
Dwarf_Line *lbuf;
@@ -219,17 +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;
- lbuf = NULL;
- lcount = 0;
+ cu = NULL;
+ die = NULL;
while ((ret = dwarf_next_cu_header(dbg, NULL, NULL, NULL, NULL, NULL,
&de)) == DW_DLV_OK) {
@@ -252,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;
- }
-
- 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",
+ if (dwarf_dieoffset(die, &off, &de) != DW_DLV_OK) {
+ warnx("dwarf_dieoffset failed: %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;
+ 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);
}
- plineaddr = lineaddr;
- plineno = lineno;
- pfile = file;
+
+ if (addr >= lopc && addr < hipc)
+ break;
}
+
next_cu:
if (die != NULL) {
dwarf_dealloc(dbg, die, DW_DLA_DIE);
@@ -312,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);
@@ -420,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;
@@ -435,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;
@@ -472,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);
}
diff --git a/common/elfdefinitions.h b/common/elfdefinitions.h
index ab7001fb82e7..e953c924147d 100644
--- a/common/elfdefinitions.h
+++ b/common/elfdefinitions.h
@@ -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 $
*/
/*
@@ -1659,25 +1659,38 @@ _ELF_DEFINE_RELOC(R_IA_64_DTPREL64MSB, 0xB6) \
_ELF_DEFINE_RELOC(R_IA_64_DTPREL64LSB, 0xB7) \
_ELF_DEFINE_RELOC(R_IA_64_LTOFF_DTPREL22, 0xBA)
-#define _ELF_DEFINE_MIPS_RELOCATIONS() \
-_ELF_DEFINE_RELOC(R_MIPS_NONE, 0) \
-_ELF_DEFINE_RELOC(R_MIPS_16, 1) \
-_ELF_DEFINE_RELOC(R_MIPS_32, 2) \
-_ELF_DEFINE_RELOC(R_MIPS_REL32, 3) \
-_ELF_DEFINE_RELOC(R_MIPS_26, 4) \
-_ELF_DEFINE_RELOC(R_MIPS_HI16, 5) \
-_ELF_DEFINE_RELOC(R_MIPS_LO16, 6) \
-_ELF_DEFINE_RELOC(R_MIPS_GPREL16, 7) \
-_ELF_DEFINE_RELOC(R_MIPS_LITERAL, 8) \
-_ELF_DEFINE_RELOC(R_MIPS_GOT16, 9) \
-_ELF_DEFINE_RELOC(R_MIPS_PC16, 10) \
-_ELF_DEFINE_RELOC(R_MIPS_CALL16, 11) \
-_ELF_DEFINE_RELOC(R_MIPS_GPREL32, 12) \
-_ELF_DEFINE_RELOC(R_MIPS_64, 18) \
-_ELF_DEFINE_RELOC(R_MIPS_GOTHI16, 21) \
-_ELF_DEFINE_RELOC(R_MIPS_GOTLO16, 22) \
-_ELF_DEFINE_RELOC(R_MIPS_CALLHI16, 30) \
-_ELF_DEFINE_RELOC(R_MIPS_CALLLO16, 31)
+#define _ELF_DEFINE_MIPS_RELOCATIONS() \
+_ELF_DEFINE_RELOC(R_MIPS_NONE, 0) \
+_ELF_DEFINE_RELOC(R_MIPS_16, 1) \
+_ELF_DEFINE_RELOC(R_MIPS_32, 2) \
+_ELF_DEFINE_RELOC(R_MIPS_REL32, 3) \
+_ELF_DEFINE_RELOC(R_MIPS_26, 4) \
+_ELF_DEFINE_RELOC(R_MIPS_HI16, 5) \
+_ELF_DEFINE_RELOC(R_MIPS_LO16, 6) \
+_ELF_DEFINE_RELOC(R_MIPS_GPREL16, 7) \
+_ELF_DEFINE_RELOC(R_MIPS_LITERAL, 8) \
+_ELF_DEFINE_RELOC(R_MIPS_GOT16, 9) \
+_ELF_DEFINE_RELOC(R_MIPS_PC16, 10) \
+_ELF_DEFINE_RELOC(R_MIPS_CALL16, 11) \
+_ELF_DEFINE_RELOC(R_MIPS_GPREL32, 12) \
+_ELF_DEFINE_RELOC(R_MIPS_64, 18) \
+_ELF_DEFINE_RELOC(R_MIPS_GOTHI16, 21) \
+_ELF_DEFINE_RELOC(R_MIPS_GOTLO16, 22) \
+_ELF_DEFINE_RELOC(R_MIPS_CALLHI16, 30) \
+_ELF_DEFINE_RELOC(R_MIPS_CALLLO16, 31) \
+_ELF_DEFINE_RELOC(R_MIPS_TLS_DTPMOD32, 38) \
+_ELF_DEFINE_RELOC(R_MIPS_TLS_DTPREL32, 39) \
+_ELF_DEFINE_RELOC(R_MIPS_TLS_DTPMOD64, 40) \
+_ELF_DEFINE_RELOC(R_MIPS_TLS_DTPREL64, 41) \
+_ELF_DEFINE_RELOC(R_MIPS_TLS_GD, 42) \
+_ELF_DEFINE_RELOC(R_MIPS_TLS_LDM, 43) \
+_ELF_DEFINE_RELOC(R_MIPS_TLS_DTPREL_HI16, 44) \
+_ELF_DEFINE_RELOC(R_MIPS_TLS_DTPREL_LO16, 45) \
+_ELF_DEFINE_RELOC(R_MIPS_TLS_GOTTPREL, 46) \
+_ELF_DEFINE_RELOC(R_MIPS_TLS_TPREL32, 47) \
+_ELF_DEFINE_RELOC(R_MIPS_TLS_TPREL64, 48) \
+_ELF_DEFINE_RELOC(R_MIPS_TLS_TPREL_HI16, 49) \
+_ELF_DEFINE_RELOC(R_MIPS_TLS_TPREL_LO16, 50)
#define _ELF_DEFINE_PPC32_RELOCATIONS() \
_ELF_DEFINE_RELOC(R_PPC_NONE, 0) \
diff --git a/elfcopy/binary.c b/elfcopy/binary.c
index 23e46e77f392..7c834a9aaa34 100644
--- a/elfcopy/binary.c
+++ b/elfcopy/binary.c
@@ -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
@@ -140,6 +140,7 @@ create_elf_from_binary(struct elfcopy *ecp, int ifd, const char *ifn)
GElf_Shdr sh;
void *content;
uint64_t off, data_start, data_end, data_size;
+ char *sym_basename, *p;
/* Reset internal section list. */
if (!TAILQ_EMPTY(&ecp->v_sec))
@@ -210,8 +211,13 @@ create_elf_from_binary(struct elfcopy *ecp, int ifd, const char *ifn)
/* Count in .symtab and .strtab section headers. */
shtab->sz += gelf_fsize(ecp->eout, ELF_T_SHDR, 2, EV_CURRENT);
+ if ((sym_basename = strdup(ifn)) == NULL)
+ err(1, "strdup");
+ p = sym_basename;
+ while ((p = strchr(p, '.')) != NULL)
+ *p++ = '_';
#define _GEN_SYMNAME(S) do { \
- snprintf(name, sizeof(name), "%s%s%s", "_binary_", ifn, S); \
+ snprintf(name, sizeof(name), "%s%s%s", "_binary_", sym_basename, S); \
} while (0)
/*
@@ -233,6 +239,7 @@ create_elf_from_binary(struct elfcopy *ecp, int ifd, const char *ifn)
finalize_external_symtab(ecp);
create_symtab_data(ecp);
#undef _GEN_SYMNAME
+ free(sym_basename);
/*
* Write the underlying ehdr. Note that it should be called
diff --git a/elfcopy/elfcopy.1 b/elfcopy/elfcopy.1
index 4889570ebc8e..83cda5d8c5f7 100644
--- a/elfcopy/elfcopy.1
+++ b/elfcopy/elfcopy.1
@@ -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 .
diff --git a/elfcopy/main.c b/elfcopy/main.c
index cbd48d3fc4b9..e2685b476573 100644
--- a/elfcopy/main.c
+++ b/elfcopy/main.c
@@ -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\
diff --git a/elfcopy/sections.c b/elfcopy/sections.c
index f1ec6c0f8023..0c18171752be 100644
--- a/elfcopy/sections.c
+++ b/elfcopy/sections.c
@@ -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);
diff --git a/elfcopy/segments.c b/elfcopy/segments.c
index 1e271a6f4347..837cea5fdbe5 100644
--- a/elfcopy/segments.c
+++ b/elfcopy/segments.c
@@ -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;
diff --git a/libelf/elf_data.c b/libelf/elf_data.c
index ce80e1c94c40..3d8ef6cadace 100644
--- a/libelf/elf_data.c
+++ b/libelf/elf_data.c
@@ -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);
diff --git a/readelf/readelf.c b/readelf/readelf.c
index 0111f1a59376..3b4b2d3c56b0 100644
--- a/readelf/readelf.c
+++ b/readelf/readelf.c
@@ -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);
@@ -332,6 +332,7 @@ static const char *note_type_gnu(unsigned int nt);
static const char *note_type_netbsd(unsigned int nt);
static const char *note_type_openbsd(unsigned int nt);
static const char *note_type_unknown(unsigned int nt);
+static const char *note_type_xen(unsigned int nt);
static const char *option_kind(uint8_t kind);
static const char *phdr_type(unsigned int ptype);
static const char *ppc_abi_fp(uint64_t fp);
@@ -357,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"},
@@ -1170,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";
@@ -1182,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";
@@ -1295,6 +1311,20 @@ r_type(unsigned int mach, unsigned int type)
case 22: return "R_MIPS_GOTLO16";
case 30: return "R_MIPS_CALLHI16";
case 31: return "R_MIPS_CALLLO16";
+ case 38: return "R_MIPS_TLS_DTPMOD32";
+ case 39: return "R_MIPS_TLS_DTPREL32";
+ case 40: return "R_MIPS_TLS_DTPMOD64";
+ case 41: return "R_MIPS_TLS_DTPREL64";
+ case 42: return "R_MIPS_TLS_GD";
+ case 43: return "R_MIPS_TLS_LDM";
+ case 44: return "R_MIPS_TLS_DTPREL_HI16";
+ case 45: return "R_MIPS_TLS_DTPREL_LO16";
+ case 46: return "R_MIPS_TLS_GOTTPREL";
+ case 47: return "R_MIPS_TLS_TPREL32";
+ case 48: return "R_MIPS_TLS_TPREL64";
+ case 49: return "R_MIPS_TLS_TPREL_HI16";
+ case 50: return "R_MIPS_TLS_TPREL_LO16";
+
default: return "";
}
case EM_PPC:
@@ -1571,6 +1601,8 @@ note_type(const char *name, unsigned int et, unsigned int nt)
return note_type_netbsd(nt);
else if (strcmp(name, "OpenBSD") == 0 && et != ET_CORE)
return note_type_openbsd(nt);
+ else if (strcmp(name, "Xen") == 0 && et != ET_CORE)
+ return note_type_xen(nt);
return note_type_unknown(nt);
}
@@ -1679,6 +1711,32 @@ note_type_unknown(unsigned int nt)
return (s_nt);
}
+static const char *
+note_type_xen(unsigned int nt)
+{
+ switch (nt) {
+ case 0: return "XEN_ELFNOTE_INFO";
+ case 1: return "XEN_ELFNOTE_ENTRY";
+ case 2: return "XEN_ELFNOTE_HYPERCALL_PAGE";
+ case 3: return "XEN_ELFNOTE_VIRT_BASE";
+ case 4: return "XEN_ELFNOTE_PADDR_OFFSET";
+ case 5: return "XEN_ELFNOTE_XEN_VERSION";
+ case 6: return "XEN_ELFNOTE_GUEST_OS";
+ case 7: return "XEN_ELFNOTE_GUEST_VERSION";
+ case 8: return "XEN_ELFNOTE_LOADER";
+ case 9: return "XEN_ELFNOTE_PAE_MODE";
+ case 10: return "XEN_ELFNOTE_FEATURES";
+ case 11: return "XEN_ELFNOTE_BSD_SYMTAB";
+ case 12: return "XEN_ELFNOTE_HV_START_LOW";
+ case 13: return "XEN_ELFNOTE_L1_MFN_VALID";
+ case 14: return "XEN_ELFNOTE_SUSPEND_CANCEL";
+ case 15: return "XEN_ELFNOTE_INIT_P2M";
+ case 16: return "XEN_ELFNOTE_MOD_START_PFN";
+ case 17: return "XEN_ELFNOTE_SUPPORTED_FEATURES";
+ default: return (note_type_unknown(nt));
+ }
+}
+
static struct {
const char *name;
int value;
@@ -2804,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");
}
@@ -4199,7 +4257,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;
@@ -4216,7 +4274,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);
}
@@ -4224,11 +4282,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;
@@ -4245,7 +4303,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++) {
@@ -4254,7 +4312,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));
}
@@ -4264,7 +4322,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)
@@ -4278,21 +4336,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++;
@@ -4316,17 +4374,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;
}
}
@@ -4346,22 +4404,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;
}
}
@@ -4372,7 +4430,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;
@@ -4393,6 +4451,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);
@@ -4403,18 +4462,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;
@@ -4426,14 +4485,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);
@@ -4796,9 +4855,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);
@@ -4830,7 +4889,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:
@@ -4849,9 +4908,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:
@@ -4868,7 +4927,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",
@@ -4876,19 +4935,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;
@@ -4921,7 +4980,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:
@@ -7411,7 +7470,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;
@@ -7420,6 +7479,8 @@ _decode_sleb128(uint8_t **dp)
uint8_t *src = *dp;
do {
+ if (src >= dpe)
+ break;
b = *src++;
ret |= ((b & 0x7f) << shift);
shift += 7;
@@ -7434,7 +7495,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;
@@ -7443,6 +7504,8 @@ _decode_uleb128(uint8_t **dp)
uint8_t *src = *dp;
do {
+ if (src >= dpe)
+ break;
b = *src++;
ret |= ((b & 0x7f) << shift);
shift += 7;