diff options
author | Simon J. Gerraty <sjg@FreeBSD.org> | 2015-05-27 01:19:58 +0000 |
---|---|---|
committer | Simon J. Gerraty <sjg@FreeBSD.org> | 2015-05-27 01:19:58 +0000 |
commit | 98e0ffaefb0f241cda3a72395d3be04192ae0d47 (patch) | |
tree | 55c065b6730aaac2afb6c29933ee6ec5fa4c4249 /contrib/elftoolchain | |
parent | b17ff922d4072ae132ece458f5b5d74a236880ac (diff) | |
parent | e81032ad243db32b8fd615b2d55ee94b9f6a5b6a (diff) | |
download | src-98e0ffaefb0f241cda3a72395d3be04192ae0d47.tar.gz src-98e0ffaefb0f241cda3a72395d3be04192ae0d47.zip |
Merge sync of head
Notes
Notes:
svn path=/projects/bmake/; revision=283595
Diffstat (limited to 'contrib/elftoolchain')
186 files changed, 32196 insertions, 1129 deletions
diff --git a/contrib/elftoolchain/addr2line/Makefile b/contrib/elftoolchain/addr2line/Makefile new file mode 100644 index 000000000000..e388bc0a6edb --- /dev/null +++ b/contrib/elftoolchain/addr2line/Makefile @@ -0,0 +1,15 @@ +# $Id: Makefile 2066 2011-10-26 15:40:28Z jkoshy $ + +TOP= .. + +PROG= addr2line +SRCS= addr2line.c + +WARNS?= 6 + +DPADD= ${LIBELF} ${LIBELFTC} ${LIBDWARF} +LDADD= -lelftc -ldwarf -lelf + +MAN1= addr2line.1 + +.include "${TOP}/mk/elftoolchain.prog.mk" diff --git a/contrib/elftoolchain/addr2line/addr2line.1 b/contrib/elftoolchain/addr2line/addr2line.1 new file mode 100644 index 000000000000..f000c2911d7b --- /dev/null +++ b/contrib/elftoolchain/addr2line/addr2line.1 @@ -0,0 +1,159 @@ +.\" Copyright (c) 2009,2010 Joseph Koshy <jkoshy@users.sourceforge.net> +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer +.\" in this position and unchanged. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR +.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +.\" (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 $ +.\" +.Dd July 25, 2010 +.Os +.Dt ADDR2LINE 1 +.Sh NAME +.Nm addr2line +.Nd translate program addresses to source file names and line numbers +.Sh SYNOPSIS +.Nm +.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 j Ar sectionname | Fl -section Ns = Ns Ar sectionname +.Op Fl s | Fl -basename +.Op Fl C | Fl -demangle +.Op Fl H | Fl -help +.Op Fl V | Fl -version +.Op Ar hexaddress Ns ... +.Sh DESCRIPTION +The +.Nm +utility translates program addresses specified by the command line +arguments +.Ar hexaddress +to their corresponding source file names and line numbers. +If no arguments are given to +.Nm , +it will read these addresses from standard input. +.Pp +Program addresses specified by arguments +.Ar hexaddress +are encoded using the conventions accepted by +.Xr strtoull 3 . +.Pp +By default, +.Nm +will use the executable +.Dq Pa a.out . +The +.Fl e +option may be used to specified a different ELF object. +.Pp +The +.Nm +utility recognizes the following options: +.Bl -tag -width indent +.It Fl b Ar target | Fl -target Ns = Ns Ar target +This option is recognized by +.Nm +but is ignored. +It is supported for compatibility with GNU binutils. +.It Fl e Ar pathname | Fl -exe Ns = Ns Ar pathname +Use the ELF object specified by argument +.Ar pathname +to translate addresses. +If this option is not specified, +.Nm +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 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 s | -basename +Display only the base name for each file name. +.It Fl C | Fl -demangle +Demangle C++ names. +.It Fl H | Fl -help +Print a help message. +.It Fl V | Fl -version +Print a version identifier and exit. +.El +.Sh OUTPUT FORMAT +If the +.Fl f +option was not specified, +.Nm +will print the file name and line number for each address specified +on a separate line. +.Pp +If the +.Fl f +option was specified, +.Nm +will print a line containing the name of the function corresponding +to program address +.Ar hexaddress , +followed by a line with the file name and line number. +.Pp +The +.Nm +utility prints the file name and line number using the format +.Dq FILENAME:LINENUMBER . +.Pp +If a file or function name could not be determined, +.Nm +will print a question mark in their place. +If the line number could not be determined, +.Nm +will print a zero in its place. +.Sh EXAMPLES +To map address 080483c4 in the default executable +.Pa a.out +to a source file name and line number use: +.D1 "% addr2line 080483c4" +.Pp +To map address 080483c4 in executable +.Pa helloworld , +use: +.D1 "% addr2line -e helloworld 080483c4" +.Pp +To have +.Nm +act as a filter reading addresses from its standard input use: +.D1 "% addr2line" +.Pp +To print the function name corresponding to an address in addition to +its source file and line number use: +.D1 "% addr2line -f 080483c4" +.Sh EXIT STATUS +.Ex -std +.Sh SEE ALSO +.Xr nm 1 , +.Xr elfdump 1 , +.Xr elfcopy 1 , +.Xr strtoull 3 +.Sh AUTHORS +The +.Nm +utility was written by +.An Kai Wang Aq Mt kaiwang27@users.sourceforge.net . diff --git a/contrib/elftoolchain/addr2line/addr2line.c b/contrib/elftoolchain/addr2line/addr2line.c new file mode 100644 index 000000000000..cfdcc745f9fb --- /dev/null +++ b/contrib/elftoolchain/addr2line/addr2line.c @@ -0,0 +1,453 @@ +/*- + * Copyright (c) 2009 Kai Wang + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <sys/param.h> +#include <dwarf.h> +#include <err.h> +#include <fcntl.h> +#include <gelf.h> +#include <getopt.h> +#include <libdwarf.h> +#include <libelftc.h> +#include <libgen.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "_elftc.h" + +ELFTC_VCSID("$Id: addr2line.c 3197 2015-05-12 21:01:31Z emaste $"); + +static struct option longopts[] = { + {"target" , required_argument, NULL, 'b'}, + {"demangle", no_argument, NULL, 'C'}, + {"exe", required_argument, NULL, 'e'}, + {"functions", no_argument, NULL, 'f'}, + {"section", required_argument, NULL, 'j'}, + {"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 char unknown[] = { '?', '?', '\0' }; +static Dwarf_Addr section_base; + +#define USAGE_MESSAGE "\ +Usage: %s [options] hexaddress...\n\ + Map program addresses to source file names and line numbers.\n\n\ + Options:\n\ + -b TGT | --target=TGT (Accepted but ignored).\n\ + -e EXE | --exec=EXE Use program \"EXE\" to translate addresses.\n\ + -f | --functions Display function names.\n\ + -j NAME | --section=NAME Values are offsets into section \"NAME\".\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\ + -V | --version Print a version identifier and exit.\n" + +static void +usage(void) +{ + (void) fprintf(stderr, USAGE_MESSAGE, ELFTC_GETPROGNAME()); + exit(1); +} + +static void +version(void) +{ + + fprintf(stderr, "%s (%s)\n", ELFTC_GETPROGNAME(), elftc_version()); + exit(0); +} + +/* + * Handle DWARF 4 'offset from' DW_AT_high_pc. Although we don't + * fully support DWARF 4, some compilers (like FreeBSD Clang 3.5.1) + * generate DW_AT_high_pc as an offset from DW_AT_low_pc. + * + * "If the value of the DW_AT_high_pc is of class address, it is the + * relocated address of the first location past the last instruction + * associated with the entity; if it is of class constant, the value + * is an unsigned integer offset which when added to the low PC gives + * the address of the first location past the last instruction + * associated with the entity." + * + * DWARF4 spec, section 2.17.2. + */ +static int +handle_high_pc(Dwarf_Die die, Dwarf_Unsigned lopc, Dwarf_Unsigned *hipc) +{ + Dwarf_Error de; + Dwarf_Half form; + Dwarf_Attribute at; + int ret; + + ret = dwarf_attr(die, DW_AT_high_pc, &at, &de); + if (ret == DW_DLV_ERROR) { + warnx("dwarf_attr failed: %s", dwarf_errmsg(de)); + return (ret); + } + ret = dwarf_whatform(at, &form, &de); + if (ret == DW_DLV_ERROR) { + warnx("dwarf_whatform failed: %s", dwarf_errmsg(de)); + return (ret); + } + if (dwarf_get_form_class(2, 0, 0, form) == DW_FORM_CLASS_CONSTANT) + *hipc += lopc; + + return (DW_DLV_OK); +} + +static void +search_func(Dwarf_Debug dbg, Dwarf_Die die, Dwarf_Addr addr, + const char **rlt_func) +{ + Dwarf_Die ret_die, spec_die; + Dwarf_Error de; + Dwarf_Half tag; + Dwarf_Unsigned lopc, hipc; + Dwarf_Off ref; + Dwarf_Attribute sub_at, spec_at; + char *func0; + int ret; + + if (*rlt_func != NULL) + return; + + if (dwarf_tag(die, &tag, &de)) { + warnx("dwarf_tag: %s", dwarf_errmsg(de)); + goto cont_search; + } + if (tag == DW_TAG_subprogram) { + 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! */ + + *rlt_func = unknown; + ret = dwarf_attr(die, DW_AT_name, &sub_at, &de); + if (ret == DW_DLV_ERROR) + return; + if (ret == DW_DLV_OK) { + if (dwarf_formstring(sub_at, &func0, &de)) + *rlt_func = unknown; + else + *rlt_func = func0; + return; + } + + /* + * 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)) + return; + if (dwarf_global_formref(spec_at, &ref, &de)) + return; + if (dwarf_offdie(dbg, ref, &spec_die, &de)) + return; + if (dwarf_attrval_string(spec_die, DW_AT_name, rlt_func, &de)) + *rlt_func = unknown; + + return; + } + +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); + + /* Search sibling. */ + ret = dwarf_siblingof(dbg, die, &ret_die, &de); + if (ret == DW_DLV_ERROR) + errx(EXIT_FAILURE, "dwarf_siblingof: %s", dwarf_errmsg(de)); + else if (ret == DW_DLV_OK) + search_func(dbg, ret_die, addr, rlt_func); +} + +static void +translate(Dwarf_Debug dbg, const char* addrstr) +{ + Dwarf_Die die; + Dwarf_Line *lbuf; + Dwarf_Error de; + Dwarf_Half tag; + Dwarf_Unsigned lopc, hipc, addr, lineno, plineno; + Dwarf_Signed lcount; + Dwarf_Addr lineaddr, plineaddr; + const char *funcname; + char *file, *file0, *pfile; + char demangled[1024]; + int i, ret; + + addr = strtoull(addrstr, NULL, 16); + addr += section_base; + lineno = 0; + file = unknown; + + while ((ret = dwarf_next_cu_header(dbg, NULL, NULL, NULL, NULL, NULL, + &de)) == DW_DLV_OK) { + die = NULL; + while (dwarf_siblingof(dbg, die, &die, &de) == DW_DLV_OK) { + if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) { + warnx("dwarf_tag failed: %s", + dwarf_errmsg(de)); + goto out; + } + /* XXX: What about DW_TAG_partial_unit? */ + if (tag == DW_TAG_compile_unit) + break; + } + if (die == NULL) { + warnx("could not find DW_TAG_compile_unit die"); + goto out; + } + if (!dwarf_attrval_unsigned(die, DW_AT_low_pc, &lopc, &de) && + !dwarf_attrval_unsigned(die, DW_AT_high_pc, &hipc, &de)) { + /* + * Check if the address falls into the PC range of + * this CU. + */ + if (handle_high_pc(die, lopc, &hipc) != DW_DLV_OK) + continue; + if (addr < lopc || addr >= hipc) + continue; + } + + if (dwarf_srclines(die, &lbuf, &lcount, &de) != DW_DLV_OK) { + 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: + funcname = NULL; + if (ret == DW_DLV_OK && func) + search_func(dbg, die, addr, &funcname); + + if (func) { + if (funcname == NULL) + funcname = unknown; + if (demangle && + !elftc_demangle(funcname, demangled, sizeof(demangled), 0)) + printf("%s\n", demangled); + else + printf("%s\n", funcname); + } + + (void) printf("%s:%ju\n", base ? basename(file) : file, lineno); + + /* + * Reset internal CU pointer, so we will start from the first CU + * next round. + */ + while (ret != DW_DLV_NO_ENTRY) { + if (ret == DW_DLV_ERROR) + errx(EXIT_FAILURE, "dwarf_next_cu_header: %s", + dwarf_errmsg(de)); + ret = dwarf_next_cu_header(dbg, NULL, NULL, NULL, NULL, NULL, + &de); + } +} + +static void +find_section_base(const char *exe, Elf *e, const char *section) +{ + Dwarf_Addr off; + Elf_Scn *scn; + GElf_Ehdr eh; + GElf_Shdr sh; + size_t shstrndx; + int elferr; + const char *name; + + if (gelf_getehdr(e, &eh) != &eh) { + warnx("gelf_getehdr failed: %s", elf_errmsg(-1)); + return; + } + + if (!elf_getshstrndx(e, &shstrndx)) { + warnx("elf_getshstrndx failed: %s", elf_errmsg(-1)); + return; + } + + (void) elf_errno(); + off = 0; + scn = NULL; + while ((scn = elf_nextscn(e, scn)) != NULL) { + if (gelf_getshdr(scn, &sh) == NULL) { + warnx("gelf_getshdr failed: %s", elf_errmsg(-1)); + continue; + } + if ((name = elf_strptr(e, shstrndx, sh.sh_name)) == NULL) + goto next; + if (!strcmp(section, name)) { + if (eh.e_type == ET_EXEC || eh.e_type == ET_DYN) { + /* + * For executables, section base is the virtual + * address of the specified section. + */ + section_base = sh.sh_addr; + } else if (eh.e_type == ET_REL) { + /* + * For relocatables, section base is the + * relative offset of the specified section + * to the start of the first section. + */ + section_base = off; + } else + warnx("unknown e_type %u", eh.e_type); + return; + } + next: + off += sh.sh_size; + } + elferr = elf_errno(); + if (elferr != 0) + warnx("elf_nextscn failed: %s", elf_errmsg(elferr)); + + errx(EXIT_FAILURE, "%s: cannot find section %s", exe, section); +} + +int +main(int argc, char **argv) +{ + Elf *e; + Dwarf_Debug dbg; + Dwarf_Error de; + const char *exe, *section; + char line[1024]; + int fd, i, opt; + + exe = NULL; + section = NULL; + while ((opt = getopt_long(argc, argv, "b:Ce:fj:sHV", longopts, NULL)) != + -1) { + switch (opt) { + case 'b': + /* ignored */ + break; + case 'C': + demangle = 1; + break; + case 'e': + exe = optarg; + break; + case 'f': + func = 1; + break; + case 'j': + section = optarg; + break; + case 's': + base = 1; + break; + case 'H': + usage(); + case 'V': + version(); + default: + usage(); + } + } + + argv += optind; + argc -= optind; + + if (exe == NULL) + exe = "a.out"; + + if ((fd = open(exe, O_RDONLY)) < 0) + err(EXIT_FAILURE, "%s", exe); + + if (dwarf_init(fd, DW_DLC_READ, NULL, NULL, &dbg, &de)) + errx(EXIT_FAILURE, "dwarf_init: %s", dwarf_errmsg(de)); + + if (dwarf_get_elf(dbg, &e, &de) != DW_DLV_OK) + errx(EXIT_FAILURE, "dwarf_get_elf: %s", dwarf_errmsg(de)); + + if (section) + find_section_base(exe, e, section); + else + section_base = 0; + + if (argc > 0) + for (i = 0; i < argc; i++) + translate(dbg, argv[i]); + else + while (fgets(line, sizeof(line), stdin) != NULL) { + translate(dbg, line); + fflush(stdout); + } + + dwarf_finish(dbg, &de); + + (void) elf_end(e); + + exit(0); +} diff --git a/contrib/elftoolchain/common/Makefile b/contrib/elftoolchain/common/Makefile index d3df246e78fc..9551bcd20f34 100644 --- a/contrib/elftoolchain/common/Makefile +++ b/contrib/elftoolchain/common/Makefile @@ -1,4 +1,4 @@ -# $Id: Makefile 2606 2012-10-02 17:52:57Z jkoshy $ +# $Id: Makefile 3022 2014-04-17 18:05:58Z jkoshy $ TOP= .. @@ -12,4 +12,7 @@ all depend obj: clean clobber: rm -f ${CLEANFILES} +cleandepend: + rm -f .depend + .include "${TOP}/mk/elftoolchain.inc.mk" diff --git a/contrib/elftoolchain/common/_elftc.h b/contrib/elftoolchain/common/_elftc.h index 0b8c77c09115..45f0abd80774 100644 --- a/contrib/elftoolchain/common/_elftc.h +++ b/contrib/elftoolchain/common/_elftc.h @@ -23,7 +23,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: _elftc.h 2922 2013-03-17 22:53:15Z kaiwang27 $ + * $Id: _elftc.h 3175 2015-03-27 17:21:24Z emaste $ */ /** @@ -76,10 +76,17 @@ * SUCH DAMAGE. */ +#ifndef LIST_FOREACH_SAFE +#define LIST_FOREACH_SAFE(var, head, field, tvar) \ + for ((var) = LIST_FIRST((head)); \ + (var) && ((tvar) = LIST_NEXT((var), field), 1); \ + (var) = (tvar)) +#endif + #ifndef SLIST_FOREACH_SAFE -#define SLIST_FOREACH_SAFE(var, head, field, tvar) \ - for ((var) = SLIST_FIRST((head)); \ - (var) && ((tvar) = SLIST_NEXT((var), field), 1); \ +#define SLIST_FOREACH_SAFE(var, head, field, tvar) \ + for ((var) = SLIST_FIRST((head)); \ + (var) && ((tvar) = SLIST_NEXT((var), field), 1); \ (var) = (tvar)) #endif @@ -287,7 +294,8 @@ struct name { \ #define ELFTC_VCSID(ID) __FBSDID(ID) #endif -#if defined(__linux__) || defined(__GNU__) || defined(__GLIBC__) +#if defined(__APPLE__) || defined(__GLIBC__) || defined(__GNU__) || \ + defined(__linux__) #if defined(__GNUC__) #define ELFTC_VCSID(ID) __asm__(".ident\t\"" ID "\"") #else @@ -323,8 +331,8 @@ struct name { \ #ifndef ELFTC_GETPROGNAME -#if defined(__DragonFly__) || defined(__FreeBSD__) || defined(__minix) || \ - defined(__NetBSD__) +#if defined(__APPLE__) || defined(__DragonFly__) || defined(__FreeBSD__) || \ + defined(__minix) || defined(__NetBSD__) #include <stdlib.h> @@ -333,7 +341,7 @@ struct name { \ #endif /* __DragonFly__ || __FreeBSD__ || __minix || __NetBSD__ */ -#if defined(__GLIBC__) +#if defined(__GLIBC__) || defined(__linux__) /* * GLIBC based systems have a global 'char *' pointer referencing @@ -343,7 +351,7 @@ extern const char *program_invocation_short_name; #define ELFTC_GETPROGNAME() program_invocation_short_name -#endif /* __GLIBC__ */ +#endif /* __GLIBC__ || __linux__ */ #if defined(__OpenBSD__) @@ -361,6 +369,21 @@ extern const char *__progname; ** Per-OS configuration. **/ +#if defined(__APPLE__) + +#include <machine/endian.h> +#define roundup2 roundup + +#define ELFTC_BYTE_ORDER _BYTE_ORDER +#define ELFTC_BYTE_ORDER_LITTLE_ENDIAN _LITTLE_ENDIAN +#define ELFTC_BYTE_ORDER_BIG_ENDIAN _BIG_ENDIAN + +#define ELFTC_HAVE_MMAP 1 +#define ELFTC_HAVE_STRMODE 1 + +#endif /* __APPLE__ */ + + #if defined(__DragonFly__) #include <osreldate.h> @@ -374,7 +397,7 @@ extern const char *__progname; #endif -#if defined(__GLIBC__) +#if defined(__GLIBC__) || defined(__linux__) #include <endian.h> @@ -394,7 +417,7 @@ extern const char *__progname; #define roundup2 roundup -#endif /* __GLIBC__ */ +#endif /* __GLIBC__ || __linux__ */ #if defined(__FreeBSD__) diff --git a/contrib/elftoolchain/common/elfdefinitions.h b/contrib/elftoolchain/common/elfdefinitions.h index 7bed9a19a0dc..a53acde8b6ce 100644 --- a/contrib/elftoolchain/common/elfdefinitions.h +++ b/contrib/elftoolchain/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 2950 2013-06-15 13:36:02Z jkoshy $ + * $Id: elfdefinitions.h 3178 2015-03-30 18:29:13Z emaste $ */ /* @@ -171,7 +171,7 @@ _ELF_DEFINE_DT(DT_MOVEENT, 0x6FFFFDFAUL, \ "size of DT_MOVETAB entries") \ _ELF_DEFINE_DT(DT_MOVESZ, 0x6FFFFDFBUL, \ "total size of the MOVETAB table") \ -_ELF_DEFINE_DT(DT_FEATURE_1, 0x6FFFFDFCUL, "feature values") \ +_ELF_DEFINE_DT(DT_FEATURE, 0x6FFFFDFCUL, "feature values") \ _ELF_DEFINE_DT(DT_POSFLAG_1, 0x6FFFFDFDUL, \ "dynamic position flags") \ _ELF_DEFINE_DT(DT_SYMINSZ, 0x6FFFFDFEUL, \ @@ -770,6 +770,8 @@ _ELF_DEFINE_EM(EM_ETPU, 178, \ "Freescale Extended Time Processing Unit") \ _ELF_DEFINE_EM(EM_SLE9X, 179, \ "Infineon Technologies SLE9X core") \ +_ELF_DEFINE_EM(EM_AARCH64, 183, \ + "AArch64 (64-bit ARM)") \ _ELF_DEFINE_EM(EM_AVR32, 185, \ "Atmel Corporation 32-bit microprocessor family") \ _ELF_DEFINE_EM(EM_STM8, 186, \ @@ -819,7 +821,8 @@ enum { EM__LAST__ }; -/* Older synonyms. */ +/* Other synonyms. */ +#define EM_AMD64 EM_X86_64 #define EM_ARC_A5 EM_ARC_COMPACT /* @@ -1393,6 +1396,12 @@ _ELF_DEFINE_RELOC(R_386_8, 22) \ _ELF_DEFINE_RELOC(R_386_PC8, 23) /* + */ +#define _ELF_DEFINE_AARCH64_RELOCATIONS() \ +_ELF_DEFINE_RELOC(R_AARCH64_ABS64, 257) \ +_ELF_DEFINE_RELOC(R_AARCH64_ABS32, 258) \ + +/* * These are the symbols used in the Sun ``Linkers and Loaders * Guide'', Document No: 817-1984-17. See the X86_64 relocations list * below for the spellings used in the ELF specification. @@ -1945,14 +1954,21 @@ _ELF_DEFINE_RELOC(R_X86_64_TPOFF32, 23) \ _ELF_DEFINE_RELOC(R_X86_64_PC64, 24) \ _ELF_DEFINE_RELOC(R_X86_64_GOTOFF64, 25) \ _ELF_DEFINE_RELOC(R_X86_64_GOTPC32, 26) \ +_ELF_DEFINE_RELOC(R_X86_64_GOT64, 27) \ +_ELF_DEFINE_RELOC(R_X86_64_GOTPCREL64, 28) \ +_ELF_DEFINE_RELOC(R_X86_64_GOTPC64, 29) \ +_ELF_DEFINE_RELOC(R_X86_64_GOTPLT64, 30) \ +_ELF_DEFINE_RELOC(R_X86_64_PLTOFF64, 31) \ _ELF_DEFINE_RELOC(R_X86_64_SIZE32, 32) \ _ELF_DEFINE_RELOC(R_X86_64_SIZE64, 33) \ _ELF_DEFINE_RELOC(R_X86_64_GOTPC32_TLSDESC, 34) \ _ELF_DEFINE_RELOC(R_X86_64_TLSDESC_CALL, 35) \ -_ELF_DEFINE_RELOC(R_X86_64_TLSDESC, 36) +_ELF_DEFINE_RELOC(R_X86_64_TLSDESC, 36) \ +_ELF_DEFINE_RELOC(R_X86_64_IRELATIVE, 37) #define _ELF_DEFINE_RELOCATIONS() \ _ELF_DEFINE_386_RELOCATIONS() \ +_ELF_DEFINE_AARCH64_RELOCATIONS() \ _ELF_DEFINE_AMD64_RELOCATIONS() \ _ELF_DEFINE_ARM_RELOCATIONS() \ _ELF_DEFINE_IA64_RELOCATIONS() \ @@ -2112,11 +2128,11 @@ typedef struct { /* 64-bit entry. */ typedef struct { - Elf64_Word l_name; - Elf64_Word l_time_stamp; - Elf64_Word l_checksum; - Elf64_Word l_version; - Elf64_Word l_flags; + Elf64_Word l_name; /* The name of a shared object. */ + Elf64_Word l_time_stamp; /* 32-bit timestamp. */ + Elf64_Word l_checksum; /* Checksum of visible symbols, sizes. */ + Elf64_Word l_version; /* Interface version string index. */ + Elf64_Word l_flags; /* Flags (LL_*). */ } Elf64_Lib; #define _ELF_DEFINE_LL_FLAGS() \ @@ -2364,12 +2380,12 @@ typedef struct { /* 64 bit PHDR entry. */ typedef struct { Elf64_Word p_type; /* Type of segment. */ - Elf64_Word p_flags; /* File offset to segment. */ - Elf64_Off p_offset; /* Virtual address in memory. */ - Elf64_Addr p_vaddr; /* Physical address (if relevant). */ - Elf64_Addr p_paddr; /* Size of segment in file. */ - Elf64_Xword p_filesz; /* Size of segment in memory. */ - Elf64_Xword p_memsz; /* Segment flags. */ + Elf64_Word p_flags; /* Segment flags. */ + Elf64_Off p_offset; /* File offset to segment. */ + Elf64_Addr p_vaddr; /* Virtual address in memory. */ + Elf64_Addr p_paddr; /* Physical address (if relevant). */ + Elf64_Xword p_filesz; /* Size of segment in file. */ + Elf64_Xword p_memsz; /* Size of segment in memory. */ Elf64_Xword p_align; /* Alignment constraints. */ } Elf64_Phdr; @@ -2453,11 +2469,11 @@ typedef struct { typedef struct { Elf64_Word st_name; /* index of symbol's name */ - unsigned char st_info; /* value for the symbol */ - unsigned char st_other; /* size of associated data */ - Elf64_Half st_shndx; /* type and binding attributes */ - Elf64_Addr st_value; /* visibility */ - Elf64_Xword st_size; /* index of related section */ + unsigned char st_info; /* type and binding attributes */ + unsigned char st_other; /* visibility */ + Elf64_Half st_shndx; /* index of related section */ + Elf64_Addr st_value; /* value for the symbol */ + Elf64_Xword st_size; /* size of associated data */ } Elf64_Sym; #define ELF32_ST_BIND(I) ((I) >> 4) diff --git a/contrib/elftoolchain/common/native-elf-format b/contrib/elftoolchain/common/native-elf-format index af707591d19d..d36ab53806de 100755 --- a/contrib/elftoolchain/common/native-elf-format +++ b/contrib/elftoolchain/common/native-elf-format @@ -1,6 +1,6 @@ #!/bin/sh # -# $Id: native-elf-format 2064 2011-10-26 15:12:32Z jkoshy $ +# $Id: native-elf-format 3186 2015-04-16 22:16:40Z emaste $ # # Find the native ELF format for a host platform by compiling a # test object and examining the resulting object. @@ -19,7 +19,7 @@ touch ${tmp_c} echo "/* Generated by ${program} on `date` */" cc -c ${tmp_c} -o ${tmp_o} -readelf -h ${tmp_o} | awk ' +LC_ALL=C readelf -h ${tmp_o} | awk ' $1 ~ "Class:" { sub("ELF","",$2); elfclass = $2; } @@ -33,7 +33,7 @@ $1 ~ "Data:" { $1 ~ "Machine:" { if (match($0, "Intel.*386")) { elfarch = "EM_386"; - } else if (match($0, ".*X86-64")) { + } else if (match($0, ".*[xX]86-64")) { elfarch = "EM_X86_64"; } else { elfarch = "unknown"; diff --git a/contrib/elftoolchain/cxxfilt/Makefile b/contrib/elftoolchain/cxxfilt/Makefile new file mode 100644 index 000000000000..ca50f91face8 --- /dev/null +++ b/contrib/elftoolchain/cxxfilt/Makefile @@ -0,0 +1,15 @@ +# $Id: Makefile 2066 2011-10-26 15:40:28Z jkoshy $ + +TOP= .. + +PROG= c++filt +SRCS= cxxfilt.c + +WARNS?= 6 + +DPADD= ${LIBELFTC} ${LIBELF} +LDADD= -lelftc -lelf + +MAN1= c++filt.1 + +.include "${TOP}/mk/elftoolchain.prog.mk" diff --git a/contrib/elftoolchain/cxxfilt/c++filt.1 b/contrib/elftoolchain/cxxfilt/c++filt.1 new file mode 100644 index 000000000000..b191b44e8215 --- /dev/null +++ b/contrib/elftoolchain/cxxfilt/c++filt.1 @@ -0,0 +1,109 @@ +.\" Copyright (c) 2009-2011 Joseph Koshy <jkoshy@users.sourceforge.net> +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer +.\" in this position and unchanged. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR +.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +.\" (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: c++filt.1 3195 2015-05-12 17:22:19Z emaste $ +.\" +.Dd August 24, 2011 +.Os +.Dt C++FILT 1 +.Sh NAME +.Nm c++filt +.Nd decode C++ symbols +.Sh SYNOPSIS +.Nm +.Op Fl -help +.Op Fl _ | Fl -strip-underscores +.Op Fl n | Fl -no-strip-underscores +.Op Fl p | Fl -no-params +.Op Fl s Ar scheme | Fl -format Ns = Ns Ar scheme +.Op Fl V | Fl -version +.Op Ar encoded-names ... +.Sh DESCRIPTION +The +.Nm +utility translates encoded C++ symbol names to human-readable form. +.Pp +The +.Nm +utility has two operating modes. +.Bl -bullet +.It +If arguments +.Ar encoded-names +are not specified, then +.Nm +will act as a filter, reading from standard input +and writing to standard output. +.It +If arguments +.Ar encoded-names +are specified, then +.Nm +will decode each such argument in turn, writing its decoded form +to standard output. +.El +.Pp +The +.Nm +utility recognizes the following options: +.Bl -tag -width indent +.It Fl -help +Print a help message and exit. +.It Fl _ | Fl -strip-underscores +Remove a leading underscore from symbol names prior to decoding them. +.It Fl n | Fl -no-strip-underscores +Do not remove leading underscores from names. +.It Fl p | Fl -no-params +This option is recognized but ignored. +.It Fl s Ar scheme | Fl -format Ns = Ns Ar scheme +Select the encoding scheme to use. +Argument +.Ar scheme +can be one of the following: +.Bl -tag -width "gnu-v5" +.It Ar arm +Use the encoding scheme specified by the C++ Annotated Reference Manual. +.It Ar auto +Guess the encoding scheme from the input. +.It Ar gnu +Use the encoding scheme used by the GNU C++ compiler. +.It Ar gnu-v3 +Use the encoding scheme used by the GNU C++ compiler, version 3. +.El +.It Fl V | Fl -version +Print a version identifier for +.Nm +and exit. +.El +.Sh EXIT STATUS +.Ex -std +.Sh SEE ALSO +.Xr nm 1 , +.Xr strip 1 , +.Xr elftc_demangle 3 +.Sh AUTHORS +The +.Nm +utility was written by +.An Kai Wang Aq Mt kaiwang27@users.sourceforge.net . diff --git a/contrib/elftoolchain/cxxfilt/cxxfilt.c b/contrib/elftoolchain/cxxfilt/cxxfilt.c new file mode 100644 index 000000000000..4efa45b33f22 --- /dev/null +++ b/contrib/elftoolchain/cxxfilt/cxxfilt.c @@ -0,0 +1,223 @@ +/*- + * Copyright (c) 2009 Kai Wang + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer + * in this position and unchanged. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/param.h> +#include <ctype.h> +#include <err.h> +#include <getopt.h> +#include <libelftc.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +#include "_elftc.h" + +ELFTC_VCSID("$Id: cxxfilt.c 3174 2015-03-27 17:13:41Z emaste $"); + +#define STRBUFSZ 8192 + +static int stripus = 0; +static int noparam = 0; +static int format = 0; + +enum options +{ + OPTION_HELP, + OPTION_VERSION +}; + +static struct option longopts[] = +{ + {"format", required_argument, NULL, 's'}, + {"help", no_argument, NULL, OPTION_HELP}, + {"no-params", no_argument, NULL, 'p'}, + {"no-strip-underscores", no_argument, NULL, 'n'}, + {"strip-underscores", no_argument, NULL, '_'}, + {"version", no_argument, NULL, 'V'}, + {NULL, 0, NULL, 0} +}; + +static struct { + const char *fname; + int fvalue; +} flist[] = { + {"auto", 0}, + {"arm", ELFTC_DEM_ARM}, + {"gnu", ELFTC_DEM_GNU2}, + {"gnu-v3", ELFTC_DEM_GNU3} +}; + +#define USAGE_MESSAGE "\ +Usage: %s [options] [encoded-names...]\n\ + Translate C++ symbol names to human-readable form.\n\n\ + Options:\n\ + -_ | --strip-underscores Remove leading underscores prior to decoding.\n\ + -n | --no-strip-underscores Do not remove leading underscores.\n\ + -p | --no-params (Accepted but ignored).\n\ + -s SCHEME | --format=SCHEME Select the encoding scheme to use.\n\ + Valid schemes are: 'arm', 'auto', 'gnu' and\n\ + 'gnu-v3'.\n\ + --help Print a help message.\n\ + --version Print a version identifier and exit.\n" + +static void +usage(void) +{ + + (void) fprintf(stderr, USAGE_MESSAGE, ELFTC_GETPROGNAME()); + exit(1); +} + +static void +version(void) +{ + fprintf(stderr, "%s (%s)\n", ELFTC_GETPROGNAME(), elftc_version()); + exit(0); +} + +static int +find_format(const char *fstr) +{ + int i; + + for (i = 0; (size_t) i < sizeof(flist) / sizeof(flist[0]); i++) { + if (!strcmp(fstr, flist[i].fname)) + return (flist[i].fvalue); + } + + return (-1); +} + +static char * +demangle(char *name, int strict, int *pos) +{ + static char dem[STRBUFSZ]; + char nb[STRBUFSZ]; + int p, t; + + if (stripus && *name == '_') { + strncpy(nb, name + 1, sizeof(nb) - 1); + t = 1; + } else { + strncpy(nb, name, sizeof(nb) - 1); + t = 0; + } + nb[sizeof(nb) - 1] = '\0'; + + p = strlen(nb); + if (p <= 0) + return NULL; + + while (elftc_demangle(nb, dem, sizeof(dem), format) < 0) { + if (!strict && p > 1) { + nb[--p] = '\0'; + continue; + } else + return (NULL); + } + + if (pos != NULL) + *pos = t ? p + 1 : p; + + return (dem); +} + +int +main(int argc, char **argv) +{ + char *dem, buf[STRBUFSZ]; + int c, i, p, s, opt; + + while ((opt = getopt_long(argc, argv, "_nps:V", longopts, NULL)) != + -1) { + switch (opt) { + case '_': + stripus = 1; + break; + case 'n': + stripus = 0; + break; + case 'p': + noparam = 1; + break; + case 's': + if ((format = find_format(optarg)) < 0) + errx(EXIT_FAILURE, "unsupported format: %s", + optarg); + break; + case 'V': + version(); + /* NOT REACHED */ + case OPTION_HELP: + default: + usage(); + /* NOT REACHED */ + } + } + + argv += optind; + argc -= optind; + + if (*argv != NULL) { + for (i = 0; i < argc; i++) { + if ((dem = demangle(argv[i], 1, NULL)) == NULL) + fprintf(stderr, "Failed: %s\n", argv[i]); + else + printf("%s\n", dem); + } + } else { + p = 0; + for (;;) { + c = fgetc(stdin); + if (c == EOF || !isprint(c) || strchr(" \t\n", c)) { + if (p > 0) { + buf[p] = '\0'; + if ((dem = demangle(buf, 0, &s)) == + NULL) + printf("%s", buf); + else { + printf("%s", dem); + for (i = s; i < p; i++) + putchar(buf[i]); + } + p = 0; + } + if (c == EOF) + break; + if (isprint(c) || strchr(" \t\n", c)) + putchar(c); + } else { + if ((size_t) p >= sizeof(buf) - 1) + warnx("buffer overflowed"); + else + buf[p++] = c; + } + + } + } + + exit(0); +} diff --git a/contrib/elftoolchain/elfcopy/Makefile b/contrib/elftoolchain/elfcopy/Makefile new file mode 100644 index 000000000000..cb1a31b400ee --- /dev/null +++ b/contrib/elftoolchain/elfcopy/Makefile @@ -0,0 +1,41 @@ +# $Id: Makefile 2290 2011-12-04 07:20:46Z jkoshy $ + +TOP= .. + +PROG= elfcopy + +SRCS= archive.c ascii.c binary.c main.c sections.c segments.c symbols.c + +WARNS?= 5 + +DPADD= ${LIBELF} ${LIBELFTC} +LDADD= -lelf -lelftc + +.if !defined(LIBELF_AR) +LDADD+= -larchive +.endif + +MAN= elfcopy.1 mcs.1 strip.1 + +NO_SHARED?= yes + +LINKS= ${BINDIR}/elfcopy ${BINDIR}/strip \ + ${BINDIR}/elfcopy ${BINDIR}/mcs + +EXTRA_TARGETS= strip mcs + +CLEANFILES+= ${EXTRA_TARGETS} + +# Create in-place symbolic links to "elfcopy" at build time. + +all: ${EXTRA_TARGETS} + +${EXTRA_TARGETS}: ${PROG} + ln -s ${PROG} ${.TARGET} + +.include "${TOP}/mk/elftoolchain.prog.mk" + +.if ${OS_HOST} == "OpenBSD" +CFLAGS+= -I/usr/local/include +LDFLAGS+= -L/usr/local/lib +.endif diff --git a/contrib/elftoolchain/elfcopy/archive.c b/contrib/elftoolchain/elfcopy/archive.c new file mode 100644 index 000000000000..682a1df66dcc --- /dev/null +++ b/contrib/elftoolchain/elfcopy/archive.c @@ -0,0 +1,523 @@ +/*- + * Copyright (c) 2007-2009 Kai Wang + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <sys/param.h> +#include <sys/stat.h> +#include <err.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#ifndef LIBELF_AR +#include <archive.h> +#include <archive_entry.h> +#endif /* ! LIBELF_AR */ + +#include "elfcopy.h" + +ELFTC_VCSID("$Id: archive.c 3174 2015-03-27 17:13:41Z emaste $"); + +#define _ARMAG_LEN 8 /* length of ar magic string */ +#define _ARHDR_LEN 60 /* length of ar header */ +#define _INIT_AS_CAP 128 /* initial archive string table size */ +#define _INIT_SYMOFF_CAP (256*(sizeof(uint32_t))) /* initial so table size */ +#define _INIT_SYMNAME_CAP 1024 /* initial sn table size */ +#define _MAXNAMELEN_SVR4 15 /* max member name length in svr4 variant */ + +#ifndef LIBELF_AR +static void ac_read_objs(struct elfcopy *ecp, int ifd); +static void ac_write_cleanup(struct elfcopy *ecp); +static void ac_write_data(struct archive *a, const void *buf, size_t s); +static void ac_write_objs(struct elfcopy *ecp, int ofd); +#endif /* ! LIBELF_AR */ +static void add_to_ar_str_table(struct elfcopy *elfcopy, const char *name); +static void add_to_ar_sym_table(struct elfcopy *ecp, const char *name); +static void extract_arsym(struct elfcopy *ecp); +static void process_ar_obj(struct elfcopy *ecp, struct ar_obj *obj); +static void sync_ar(struct elfcopy *ecp); + + +static void +process_ar_obj(struct elfcopy *ecp, struct ar_obj *obj) +{ + struct stat sb; + char *tempfile; + int fd; + + /* Output to a temporary file. */ + create_tempfile(&tempfile, &fd); + if ((ecp->eout = elf_begin(fd, ELF_C_WRITE, NULL)) == NULL) + errx(EXIT_FAILURE, "elf_begin() failed: %s", + elf_errmsg(-1)); + elf_flagelf(ecp->eout, ELF_C_SET, ELF_F_LAYOUT); + create_elf(ecp); + elf_end(ecp->ein); + elf_end(ecp->eout); + free(obj->buf); + obj->buf = NULL; + + /* Extract archive symbols. */ + if (lseek(fd, 0, SEEK_SET) < 0) + err(EXIT_FAILURE, "lseek failed for '%s'", tempfile); + if ((ecp->eout = elf_begin(fd, ELF_C_READ, NULL)) == NULL) + errx(EXIT_FAILURE, "elf_begin() failed: %s", + elf_errmsg(-1)); + extract_arsym(ecp); + elf_end(ecp->eout); + + if (fstat(fd, &sb) == -1) + err(EXIT_FAILURE, "fstat %s failed", tempfile); + if (lseek(fd, 0, SEEK_SET) < 0) + err(EXIT_FAILURE, "lseek %s failed", tempfile); + obj->size = sb.st_size; + if ((obj->maddr = malloc(obj->size)) == NULL) + err(EXIT_FAILURE, "memory allocation failed for '%s'", + tempfile); + if ((size_t) read(fd, obj->maddr, obj->size) != obj->size) + err(EXIT_FAILURE, "read failed for '%s'", tempfile); + if (unlink(tempfile)) + err(EXIT_FAILURE, "unlink %s failed", tempfile); + free(tempfile); + close(fd); + if (strlen(obj->name) > _MAXNAMELEN_SVR4) + add_to_ar_str_table(ecp, obj->name); + ecp->rela_off += _ARHDR_LEN + obj->size + obj->size % 2; + STAILQ_INSERT_TAIL(&ecp->v_arobj, obj, objs); +} + +/* + * Append to the archive string table buffer. + */ +static void +add_to_ar_str_table(struct elfcopy *ecp, const char *name) +{ + + if (ecp->as == NULL) { + ecp->as_cap = _INIT_AS_CAP; + ecp->as_sz = 0; + if ((ecp->as = malloc(ecp->as_cap)) == NULL) + err(EXIT_FAILURE, "malloc failed"); + } + + /* + * The space required for holding one member name in as table includes: + * strlen(name) + (1 for '/') + (1 for '\n') + (possibly 1 for padding). + */ + while (ecp->as_sz + strlen(name) + 3 > ecp->as_cap) { + ecp->as_cap *= 2; + ecp->as = realloc(ecp->as, ecp->as_cap); + if (ecp->as == NULL) + err(EXIT_FAILURE, "realloc failed"); + } + strncpy(&ecp->as[ecp->as_sz], name, strlen(name)); + ecp->as_sz += strlen(name); + ecp->as[ecp->as_sz++] = '/'; + ecp->as[ecp->as_sz++] = '\n'; +} + +/* + * Append to the archive symbol table buffer. + */ +static void +add_to_ar_sym_table(struct elfcopy *ecp, const char *name) +{ + + if (ecp->s_so == NULL) { + if ((ecp->s_so = malloc(_INIT_SYMOFF_CAP)) == NULL) + err(EXIT_FAILURE, "malloc failed"); + ecp->s_so_cap = _INIT_SYMOFF_CAP; + ecp->s_cnt = 0; + } + + if (ecp->s_sn == NULL) { + if ((ecp->s_sn = malloc(_INIT_SYMNAME_CAP)) == NULL) + err(EXIT_FAILURE, "malloc failed"); + ecp->s_sn_cap = _INIT_SYMNAME_CAP; + ecp->s_sn_sz = 0; + } + + if (ecp->s_cnt * sizeof(uint32_t) >= ecp->s_so_cap) { + ecp->s_so_cap *= 2; + ecp->s_so = realloc(ecp->s_so, ecp->s_so_cap); + if (ecp->s_so == NULL) + err(EXIT_FAILURE, "realloc failed"); + } + ecp->s_so[ecp->s_cnt] = ecp->rela_off; + ecp->s_cnt++; + + /* + * The space required for holding one symbol name in sn table includes: + * strlen(name) + (1 for '\n') + (possibly 1 for padding). + */ + while (ecp->s_sn_sz + strlen(name) + 2 > ecp->s_sn_cap) { + ecp->s_sn_cap *= 2; + ecp->s_sn = realloc(ecp->s_sn, ecp->s_sn_cap); + if (ecp->s_sn == NULL) + err(EXIT_FAILURE, "realloc failed"); + } + strncpy(&ecp->s_sn[ecp->s_sn_sz], name, strlen(name)); + ecp->s_sn_sz += strlen(name); + ecp->s_sn[ecp->s_sn_sz++] = '\0'; +} + +static void +sync_ar(struct elfcopy *ecp) +{ + size_t s_sz; /* size of archive symbol table. */ + size_t pm_sz; /* size of pseudo members */ + int i; + + /* + * Pad the symbol name string table. It is treated specially because + * symbol name table should be padded by a '\0', not the common '\n' + * for other members. The size of sn table includes the pad bit. + */ + if (ecp->s_cnt != 0 && ecp->s_sn_sz % 2 != 0) + ecp->s_sn[ecp->s_sn_sz++] = '\0'; + + /* + * Archive string table is padded by a "\n" as the normal members. + * The difference is that the size of archive string table counts + * in the pad bit, while normal members' size fileds do not. + */ + if (ecp->as != NULL && ecp->as_sz % 2 != 0) + ecp->as[ecp->as_sz++] = '\n'; + + /* + * If there is a symbol table, calculate the size of pseudo members, + * convert previously stored relative offsets to absolute ones, and + * then make them Big Endian. + * + * absolute_offset = htobe32(relative_offset + size_of_pseudo_members) + */ + + if (ecp->s_cnt != 0) { + s_sz = (ecp->s_cnt + 1) * sizeof(uint32_t) + ecp->s_sn_sz; + pm_sz = _ARMAG_LEN + (_ARHDR_LEN + s_sz); + if (ecp->as != NULL) + pm_sz += _ARHDR_LEN + ecp->as_sz; + for (i = 0; (size_t)i < ecp->s_cnt; i++) + *(ecp->s_so + i) = htobe32(*(ecp->s_so + i) + + pm_sz); + } +} + +/* + * Extract global symbols from archive members. + */ +static void +extract_arsym(struct elfcopy *ecp) +{ + Elf_Scn *scn; + GElf_Shdr shdr; + GElf_Sym sym; + Elf_Data *data; + char *name; + size_t n, shstrndx; + int elferr, tabndx, len, i; + + if (elf_kind(ecp->eout) != ELF_K_ELF) { + warnx("internal: cannot extract symbols from non-elf object"); + return; + } + if (elf_getshstrndx(ecp->eout, &shstrndx) == 0) { + warnx("elf_getshstrndx failed: %s", elf_errmsg(-1)); + return; + } + + tabndx = -1; + scn = NULL; + while ((scn = elf_nextscn(ecp->eout, scn)) != NULL) { + if (gelf_getshdr(scn, &shdr) != &shdr) { + warnx("elf_getshdr failed: %s", elf_errmsg(-1)); + continue; + } + if ((name = elf_strptr(ecp->eout, shstrndx, shdr.sh_name)) == + NULL) { + warnx("elf_strptr failed: %s", elf_errmsg(-1)); + continue; + } + if (strcmp(name, ".strtab") == 0) { + tabndx = elf_ndxscn(scn); + break; + } + } + elferr = elf_errno(); + if (elferr != 0) + warnx("elf_nextscn failed: %s", elf_errmsg(elferr)); + + /* Ignore members without symbol table. */ + if (tabndx == -1) + return; + + scn = NULL; + while ((scn = elf_nextscn(ecp->eout, scn)) != NULL) { + if (gelf_getshdr(scn, &shdr) != &shdr) { + warnx("elf_getshdr failed: %s", elf_errmsg(-1)); + continue; + } + if (shdr.sh_type != SHT_SYMTAB) + continue; + + data = NULL; + n = 0; + while (n < shdr.sh_size && + (data = elf_getdata(scn, data)) != NULL) { + len = data->d_size / shdr.sh_entsize; + for (i = 0; i < len; i++) { + if (gelf_getsym(data, i, &sym) != &sym) { + warnx("gelf_getsym failed: %s", + elf_errmsg(-1)); + continue; + } + + /* keep only global or weak symbols */ + if (GELF_ST_BIND(sym.st_info) != STB_GLOBAL && + GELF_ST_BIND(sym.st_info) != STB_WEAK) + continue; + + /* keep only defined symbols */ + if (sym.st_shndx == SHN_UNDEF) + continue; + + if ((name = elf_strptr(ecp->eout, tabndx, + sym.st_name)) == NULL) { + warnx("elf_strptr failed: %s", + elf_errmsg(-1)); + continue; + } + + add_to_ar_sym_table(ecp, name); + } + } + } + elferr = elf_errno(); + if (elferr != 0) + warnx("elf_nextscn failed: %s", elf_errmsg(elferr)); +} + +#ifndef LIBELF_AR + +/* + * Convenient wrapper for general libarchive error handling. + */ +#define AC(CALL) do { \ + if ((CALL)) \ + errx(EXIT_FAILURE, "%s", archive_error_string(a)); \ +} while (0) + +/* Earlier versions of libarchive had some functions that returned 'void'. */ +#if ARCHIVE_VERSION_NUMBER >= 2000000 +#define ACV(CALL) AC(CALL) +#else +#define ACV(CALL) do { \ + (CALL); \ + } while (0) +#endif + +int +ac_detect_ar(int ifd) +{ + struct archive *a; + struct archive_entry *entry; + int r; + + r = -1; + if ((a = archive_read_new()) == NULL) + return (0); + archive_read_support_format_ar(a); + if (archive_read_open_fd(a, ifd, 10240) == ARCHIVE_OK) + r = archive_read_next_header(a, &entry); + archive_read_close(a); + archive_read_free(a); + + return (r == ARCHIVE_OK); +} + +void +ac_create_ar(struct elfcopy *ecp, int ifd, int ofd) +{ + + ac_read_objs(ecp, ifd); + sync_ar(ecp); + ac_write_objs(ecp, ofd); + ac_write_cleanup(ecp); +} + +static void +ac_read_objs(struct elfcopy *ecp, int ifd) +{ + struct archive *a; + struct archive_entry *entry; + struct ar_obj *obj; + const char *name; + char *buff; + size_t size; + int r; + + ecp->rela_off = 0; + if (lseek(ifd, 0, SEEK_SET) == -1) + err(EXIT_FAILURE, "lseek failed"); + if ((a = archive_read_new()) == NULL) + errx(EXIT_FAILURE, "%s", archive_error_string(a)); + archive_read_support_format_ar(a); + AC(archive_read_open_fd(a, ifd, 10240)); + for(;;) { + r = archive_read_next_header(a, &entry); + if (r == ARCHIVE_FATAL) + errx(EXIT_FAILURE, "%s", archive_error_string(a)); + if (r == ARCHIVE_EOF) + break; + if (r == ARCHIVE_WARN || r == ARCHIVE_RETRY) + warnx("%s", archive_error_string(a)); + if (r == ARCHIVE_RETRY) + continue; + + name = archive_entry_pathname(entry); + + /* skip pseudo members. */ + if (strcmp(name, "/") == 0 || strcmp(name, "//") == 0) + continue; + + size = archive_entry_size(entry); + + if (size > 0) { + if ((buff = malloc(size)) == NULL) + err(EXIT_FAILURE, "malloc failed"); + if (archive_read_data(a, buff, size) != (ssize_t)size) { + warnx("%s", archive_error_string(a)); + free(buff); + continue; + } + if ((obj = malloc(sizeof(*obj))) == NULL) + err(EXIT_FAILURE, "malloc failed"); + if ((obj->name = strdup(name)) == NULL) + err(EXIT_FAILURE, "strdup failed"); + obj->buf = buff; + obj->uid = archive_entry_uid(entry); + obj->gid = archive_entry_gid(entry); + obj->md = archive_entry_mode(entry); + obj->mtime = archive_entry_mtime(entry); + if ((ecp->ein = elf_memory(buff, size)) == NULL) + errx(EXIT_FAILURE, "elf_memory() failed: %s", + elf_errmsg(-1)); + if (elf_kind(ecp->ein) != ELF_K_ELF) + errx(EXIT_FAILURE, + "file format not recognized"); + process_ar_obj(ecp, obj); + } + } + AC(archive_read_close(a)); + ACV(archive_read_free(a)); +} + +static void +ac_write_objs(struct elfcopy *ecp, int ofd) +{ + struct archive *a; + struct archive_entry *entry; + struct ar_obj *obj; + int nr; + + if ((a = archive_write_new()) == NULL) + errx(EXIT_FAILURE, "%s", archive_error_string(a)); + archive_write_set_format_ar_svr4(a); + AC(archive_write_open_fd(a, ofd)); + + /* Write the archive symbol table, even if it's empty. */ + entry = archive_entry_new(); + archive_entry_copy_pathname(entry, "/"); + archive_entry_set_mtime(entry, time(NULL), 0); + archive_entry_set_size(entry, (ecp->s_cnt + 1) * sizeof(uint32_t) + + ecp->s_sn_sz); + AC(archive_write_header(a, entry)); + nr = htobe32(ecp->s_cnt); + ac_write_data(a, &nr, sizeof(uint32_t)); + ac_write_data(a, ecp->s_so, sizeof(uint32_t) * ecp->s_cnt); + ac_write_data(a, ecp->s_sn, ecp->s_sn_sz); + archive_entry_free(entry); + + /* Write the archive string table, if exist. */ + if (ecp->as != NULL) { + entry = archive_entry_new(); + archive_entry_copy_pathname(entry, "//"); + archive_entry_set_size(entry, ecp->as_sz); + AC(archive_write_header(a, entry)); + ac_write_data(a, ecp->as, ecp->as_sz); + archive_entry_free(entry); + } + + /* Write normal members. */ + STAILQ_FOREACH(obj, &ecp->v_arobj, objs) { + entry = archive_entry_new(); + archive_entry_copy_pathname(entry, obj->name); + archive_entry_set_uid(entry, obj->uid); + archive_entry_set_gid(entry, obj->gid); + archive_entry_set_mode(entry, obj->md); + archive_entry_set_size(entry, obj->size); + archive_entry_set_mtime(entry, obj->mtime, 0); + archive_entry_set_filetype(entry, AE_IFREG); + AC(archive_write_header(a, entry)); + ac_write_data(a, obj->maddr, obj->size); + archive_entry_free(entry); + } + + AC(archive_write_close(a)); + ACV(archive_write_free(a)); +} + +static void +ac_write_cleanup(struct elfcopy *ecp) +{ + struct ar_obj *obj, *obj_temp; + + STAILQ_FOREACH_SAFE(obj, &ecp->v_arobj, objs, obj_temp) { + STAILQ_REMOVE(&ecp->v_arobj, obj, ar_obj, objs); + if (obj->maddr != NULL) + free(obj->maddr); + free(obj->name); + free(obj); + } + + free(ecp->as); + free(ecp->s_so); + free(ecp->s_sn); + ecp->as = NULL; + ecp->s_so = NULL; + ecp->s_sn = NULL; +} + +/* + * Wrapper for archive_write_data(). + */ +static void +ac_write_data(struct archive *a, const void *buf, size_t s) +{ + if (archive_write_data(a, buf, s) != (ssize_t)s) + errx(EXIT_FAILURE, "%s", archive_error_string(a)); +} + +#endif /* ! LIBELF_AR */ diff --git a/contrib/elftoolchain/elfcopy/ascii.c b/contrib/elftoolchain/elfcopy/ascii.c new file mode 100644 index 000000000000..cad4eb8a91eb --- /dev/null +++ b/contrib/elftoolchain/elfcopy/ascii.c @@ -0,0 +1,1078 @@ +/*- + * Copyright (c) 2010,2011 Kai Wang + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <sys/param.h> +#include <ctype.h> +#include <err.h> +#include <gelf.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include "elfcopy.h" + +ELFTC_VCSID("$Id: ascii.c 3177 2015-03-30 18:19:41Z emaste $"); + +static void append_data(struct section *s, const void *buf, size_t sz); +static char hex_digit(uint8_t n); +static int hex_value(int x); +static void finalize_data_section(struct section *s); +static int ishexdigit(int x); +static int ihex_read(const char *line, char *type, uint64_t *addr, + uint64_t *num, uint8_t *data, size_t *sz); +static void ihex_write(int ofd, int type, uint64_t addr, uint64_t num, + const void *buf, size_t sz); +static void ihex_write_00(int ofd, uint64_t addr, const void *buf, size_t sz); +static void ihex_write_01(int ofd); +static void ihex_write_04(int ofd, uint16_t addr); +static void ihex_write_05(int ofd, uint64_t e_entry); +static struct section *new_data_section(struct elfcopy *ecp, int sec_index, + uint64_t off, uint64_t addr); +static int read_num(const char *line, int *len, uint64_t *num, size_t sz, + int *checksum); +static int srec_read(const char *line, char *type, uint64_t *addr, + uint8_t *data, size_t *sz); +static void srec_write(int ofd, char type, uint64_t addr, const void *buf, + size_t sz); +static void srec_write_symtab(int ofd, const char *ofn, Elf *e, Elf_Scn *scn, + GElf_Shdr *sh); +static void srec_write_S0(int ofd, const char *ofn); +static void srec_write_Sd(int ofd, char dr, uint64_t addr, const void *buf, + size_t sz, size_t rlen); +static void srec_write_Se(int ofd, uint64_t e_entry, int forceS3); +static void write_num(char *line, int *len, uint64_t num, size_t sz, + int *checksum); + +#define _LINE_BUFSZ 1024 +#define _DATA_BUFSZ 256 + +/* + * Convert ELF object to S-Record. + */ +void +create_srec(struct elfcopy *ecp, int ifd, int ofd, const char *ofn) +{ + Elf *e; + Elf_Scn *scn; + Elf_Data *d; + GElf_Ehdr eh; + GElf_Shdr sh; + uint64_t max_addr; + size_t rlen; + int elferr, addr_sz; + char dr; + + if ((e = elf_begin(ifd, ELF_C_READ, NULL)) == NULL) + errx(EXIT_FAILURE, "elf_begin() failed: %s", + elf_errmsg(-1)); + + /* Output a symbol table for `symbolsrec' target. */ + if (!strncmp(ecp->otgt, "symbolsrec", strlen("symbolsrec"))) { + scn = NULL; + while ((scn = elf_nextscn(e, scn)) != NULL) { + if (gelf_getshdr(scn, &sh) == NULL) { + warnx("gelf_getshdr failed: %s", + elf_errmsg(-1)); + (void) elf_errno(); + continue; + } + if (sh.sh_type != SHT_SYMTAB) + continue; + srec_write_symtab(ofd, ofn, e, scn, &sh); + break; + } + } + + if (ecp->flags & SREC_FORCE_S3) + dr = '3'; + else { + /* + * Find maximum address size in the first iteration. + */ + max_addr = 0; + scn = NULL; + while ((scn = elf_nextscn(e, scn)) != NULL) { + if (gelf_getshdr(scn, &sh) == NULL) { + warnx("gelf_getshdr failed: %s", + elf_errmsg(-1)); + (void) elf_errno(); + continue; + } + if ((sh.sh_flags & SHF_ALLOC) == 0 || + sh.sh_type == SHT_NOBITS || + sh.sh_size == 0) + continue; + if ((uint64_t) sh.sh_addr > max_addr) + max_addr = sh.sh_addr; + } + elferr = elf_errno(); + if (elferr != 0) + warnx("elf_nextscn failed: %s", elf_errmsg(elferr)); + + if (max_addr <= 0xFFFF) + dr = '1'; + else if (max_addr <= 0xFFFFFF) + dr = '2'; + else + dr = '3'; + } + + if (ecp->flags & SREC_FORCE_LEN) { + addr_sz = dr - '0' + 1; + if (ecp->srec_len < 1) + rlen = 1; + else if (ecp->srec_len + addr_sz + 1 > 255) + rlen = 255 - (addr_sz + 1); + else + rlen = ecp->srec_len; + } else + rlen = 16; + + /* Generate S0 record which contains the output filename. */ + srec_write_S0(ofd, ofn); + + /* Generate S{1,2,3} data records for section data. */ + scn = NULL; + while ((scn = elf_nextscn(e, scn)) != NULL) { + if (gelf_getshdr(scn, &sh) == NULL) { + warnx("gelf_getshdr failed: %s", elf_errmsg(-1)); + (void) elf_errno(); + continue; + } + if ((sh.sh_flags & SHF_ALLOC) == 0 || + sh.sh_type == SHT_NOBITS || + sh.sh_size == 0) + continue; + if (sh.sh_addr > 0xFFFFFFFF) { + warnx("address space too big for S-Record file"); + continue; + } + (void) elf_errno(); + if ((d = elf_getdata(scn, NULL)) == NULL) { + elferr = elf_errno(); + if (elferr != 0) + warnx("elf_getdata failed: %s", elf_errmsg(-1)); + continue; + } + if (d->d_buf == NULL || d->d_size == 0) + continue; + srec_write_Sd(ofd, dr, sh.sh_addr, d->d_buf, d->d_size, rlen); + } + elferr = elf_errno(); + if (elferr != 0) + warnx("elf_nextscn failed: %s", elf_errmsg(elferr)); + + /* Generate S{7,8,9} end of block recrod. */ + if (gelf_getehdr(e, &eh) == NULL) + errx(EXIT_FAILURE, "gelf_getehdr() failed: %s", + elf_errmsg(-1)); + srec_write_Se(ofd, eh.e_entry, ecp->flags & SREC_FORCE_S3); +} + +void +create_elf_from_srec(struct elfcopy *ecp, int ifd) +{ + char line[_LINE_BUFSZ], name[_LINE_BUFSZ]; + uint8_t data[_DATA_BUFSZ]; + GElf_Ehdr oeh; + struct section *s, *shtab; + FILE *ifp; + uint64_t addr, entry, off, sec_addr; + uintmax_t st_value; + size_t sz; + int _ifd, first, sec_index, in_symtab, symtab_created; + char *rlt; + char type; + + if ((_ifd = dup(ifd)) < 0) + err(EXIT_FAILURE, "dup failed"); + if ((ifp = fdopen(_ifd, "r")) == NULL) + err(EXIT_FAILURE, "fdopen failed"); + + /* Create EHDR for output .o file. */ + if (gelf_newehdr(ecp->eout, ecp->oec) == NULL) + errx(EXIT_FAILURE, "gelf_newehdr failed: %s", + elf_errmsg(-1)); + if (gelf_getehdr(ecp->eout, &oeh) == NULL) + errx(EXIT_FAILURE, "gelf_getehdr() failed: %s", + elf_errmsg(-1)); + + /* Initialise e_ident fields. */ + oeh.e_ident[EI_CLASS] = ecp->oec; + oeh.e_ident[EI_DATA] = ecp->oed; + /* + * TODO: Set OSABI according to the OS platform where elfcopy(1) + * was build. (probably) + */ + oeh.e_ident[EI_OSABI] = ELFOSABI_NONE; + oeh.e_machine = ecp->oem; + oeh.e_type = ET_REL; + oeh.e_entry = 0; + + ecp->flags |= RELOCATABLE; + + /* Create .shstrtab section */ + init_shstrtab(ecp); + ecp->shstrtab->off = 0; + + /* Data sections are inserted after EHDR. */ + off = gelf_fsize(ecp->eout, ELF_T_EHDR, 1, EV_CURRENT); + if (off == 0) + errx(EXIT_FAILURE, "gelf_fsize() failed: %s", elf_errmsg(-1)); + + /* Create data sections. */ + s = NULL; + first = 1; + sec_index = 1; + sec_addr = entry = 0; + while (fgets(line, _LINE_BUFSZ, ifp) != NULL) { + if (line[0] == '\r' || line[0] == '\n') + continue; + if (line[0] == '$' && line[1] == '$') { + ecp->flags |= SYMTAB_EXIST; + while ((rlt = fgets(line, _LINE_BUFSZ, ifp)) != NULL) { + if (line[0] == '$' && line[1] == '$') + break; + } + if (rlt == NULL) + break; + continue; + } + if (line[0] != 'S' || line[1] < '0' || line[1] > '9') { + warnx("Invalid srec record"); + continue; + } + if (srec_read(line, &type, &addr, data, &sz) < 0) { + warnx("Invalid srec record or mismatched checksum"); + continue; + } + switch (type) { + case '1': + case '2': + case '3': + if (sz == 0) + break; + if (first || sec_addr != addr) { + if (s != NULL) + finalize_data_section(s); + s = new_data_section(ecp, sec_index, off, + addr); + if (s == NULL) { + warnx("new_data_section failed"); + break; + } + sec_index++; + sec_addr = addr; + first = 0; + } + append_data(s, data, sz); + off += sz; + sec_addr += sz; + break; + case '7': + case '8': + case '9': + entry = addr; + break; + default: + break; + } + } + if (s != NULL) + finalize_data_section(s); + if (ferror(ifp)) + warn("fgets failed"); + + /* Insert .shstrtab after data sections. */ + if ((ecp->shstrtab->os = elf_newscn(ecp->eout)) == NULL) + errx(EXIT_FAILURE, "elf_newscn failed: %s", + elf_errmsg(-1)); + insert_to_sec_list(ecp, ecp->shstrtab, 1); + + /* Insert section header table here. */ + shtab = insert_shtab(ecp, 1); + + /* + * Rescan and create symbol table if we found '$$' section in + * the first scan. + */ + symtab_created = 0; + in_symtab = 0; + if (ecp->flags & SYMTAB_EXIST) { + if (fseek(ifp, 0, SEEK_SET) < 0) { + warn("fseek failed"); + ecp->flags &= ~SYMTAB_EXIST; + goto done; + } + while (fgets(line, _LINE_BUFSZ, ifp) != NULL) { + if (in_symtab) { + if (line[0] == '$' && line[1] == '$') { + in_symtab = 0; + continue; + } + if (sscanf(line, "%s $%jx", name, + &st_value) != 2) { + warnx("Invalid symbolsrec record"); + continue; + } + if (!symtab_created) { + create_external_symtab(ecp); + symtab_created = 1; + } + add_to_symtab(ecp, name, st_value, 0, SHN_ABS, + ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0, 1); + } + if (line[0] == '$' && line[1] == '$') { + in_symtab = 1; + continue; + } + } + } + if (ferror(ifp)) + warn("fgets failed"); + if (symtab_created) { + finalize_external_symtab(ecp); + create_symtab_data(ecp); + /* Count in .symtab and .strtab section headers. */ + shtab->sz += gelf_fsize(ecp->eout, ELF_T_SHDR, 2, EV_CURRENT); + } else + ecp->flags &= ~SYMTAB_EXIST; + +done: + fclose(ifp); + + /* Set entry point. */ + oeh.e_entry = entry; + + /* + * Write the underlying ehdr. Note that it should be called + * before elf_setshstrndx() since it will overwrite e->e_shstrndx. + */ + if (gelf_update_ehdr(ecp->eout, &oeh) == 0) + errx(EXIT_FAILURE, "gelf_update_ehdr() failed: %s", + elf_errmsg(-1)); + + /* Generate section name string table (.shstrtab). */ + set_shstrtab(ecp); + + /* Update sh_name pointer for each section header entry. */ + update_shdr(ecp, 0); + + /* Renew oeh to get the updated e_shstrndx. */ + if (gelf_getehdr(ecp->eout, &oeh) == NULL) + errx(EXIT_FAILURE, "gelf_getehdr() failed: %s", + elf_errmsg(-1)); + + /* Resync section offsets. */ + resync_sections(ecp); + + /* Store SHDR offset in EHDR. */ + oeh.e_shoff = shtab->off; + + /* Update ehdr since we modified e_shoff. */ + if (gelf_update_ehdr(ecp->eout, &oeh) == 0) + errx(EXIT_FAILURE, "gelf_update_ehdr() failed: %s", + elf_errmsg(-1)); + + /* Write out the output elf object. */ + if (elf_update(ecp->eout, ELF_C_WRITE) < 0) + errx(EXIT_FAILURE, "elf_update() failed: %s", + elf_errmsg(-1)); + + /* Release allocated resource. */ + free_elf(ecp); +} + +void +create_ihex(int ifd, int ofd) +{ + Elf *e; + Elf_Scn *scn; + Elf_Data *d; + GElf_Ehdr eh; + GElf_Shdr sh; + int elferr; + uint16_t addr_hi, old_addr_hi; + + if ((e = elf_begin(ifd, ELF_C_READ, NULL)) == NULL) + errx(EXIT_FAILURE, "elf_begin() failed: %s", + elf_errmsg(-1)); + + old_addr_hi = 0; + scn = NULL; + while ((scn = elf_nextscn(e, scn)) != NULL) { + if (gelf_getshdr(scn, &sh) == NULL) { + warnx("gelf_getshdr failed: %s", elf_errmsg(-1)); + (void) elf_errno(); + continue; + } + if ((sh.sh_flags & SHF_ALLOC) == 0 || + sh.sh_type == SHT_NOBITS || + sh.sh_size == 0) + continue; + if (sh.sh_addr > 0xFFFFFFFF) { + warnx("address space too big for Intel Hex file"); + continue; + } + (void) elf_errno(); + if ((d = elf_getdata(scn, NULL)) == NULL) { + elferr = elf_errno(); + if (elferr != 0) + warnx("elf_getdata failed: %s", elf_errmsg(-1)); + continue; + } + if (d->d_buf == NULL || d->d_size == 0) + continue; + addr_hi = (sh.sh_addr >> 16) & 0xFFFF; + if (addr_hi > 0 && addr_hi != old_addr_hi) { + /* Write 04 record if addr_hi is new. */ + old_addr_hi = addr_hi; + ihex_write_04(ofd, addr_hi); + } + ihex_write_00(ofd, sh.sh_addr, d->d_buf, d->d_size); + } + elferr = elf_errno(); + if (elferr != 0) + warnx("elf_nextscn failed: %s", elf_errmsg(elferr)); + + if (gelf_getehdr(e, &eh) == NULL) + errx(EXIT_FAILURE, "gelf_getehdr() failed: %s", + elf_errmsg(-1)); + ihex_write_05(ofd, eh.e_entry); + ihex_write_01(ofd); +} + +void +create_elf_from_ihex(struct elfcopy *ecp, int ifd) +{ + char line[_LINE_BUFSZ]; + uint8_t data[_DATA_BUFSZ]; + GElf_Ehdr oeh; + struct section *s, *shtab; + FILE *ifp; + uint64_t addr, addr_base, entry, num, off, rec_addr, sec_addr; + size_t sz; + int _ifd, first, sec_index; + char type; + + if ((_ifd = dup(ifd)) < 0) + err(EXIT_FAILURE, "dup failed"); + if ((ifp = fdopen(_ifd, "r")) == NULL) + err(EXIT_FAILURE, "fdopen failed"); + + /* Create EHDR for output .o file. */ + if (gelf_newehdr(ecp->eout, ecp->oec) == NULL) + errx(EXIT_FAILURE, "gelf_newehdr failed: %s", + elf_errmsg(-1)); + if (gelf_getehdr(ecp->eout, &oeh) == NULL) + errx(EXIT_FAILURE, "gelf_getehdr() failed: %s", + elf_errmsg(-1)); + + /* Initialise e_ident fields. */ + oeh.e_ident[EI_CLASS] = ecp->oec; + oeh.e_ident[EI_DATA] = ecp->oed; + /* + * TODO: Set OSABI according to the OS platform where elfcopy(1) + * was build. (probably) + */ + oeh.e_ident[EI_OSABI] = ELFOSABI_NONE; + oeh.e_machine = ecp->oem; + oeh.e_type = ET_REL; + oeh.e_entry = 0; + + ecp->flags |= RELOCATABLE; + + /* Create .shstrtab section */ + init_shstrtab(ecp); + ecp->shstrtab->off = 0; + + /* Data sections are inserted after EHDR. */ + off = gelf_fsize(ecp->eout, ELF_T_EHDR, 1, EV_CURRENT); + if (off == 0) + errx(EXIT_FAILURE, "gelf_fsize() failed: %s", elf_errmsg(-1)); + + /* Create data sections. */ + s = NULL; + first = 1; + sec_index = 1; + addr_base = rec_addr = sec_addr = entry = 0; + while (fgets(line, _LINE_BUFSZ, ifp) != NULL) { + if (line[0] == '\r' || line[0] == '\n') + continue; + if (line[0] != ':') { + warnx("Invalid ihex record"); + continue; + } + if (ihex_read(line, &type, &addr, &num, data, &sz) < 0) { + warnx("Invalid ihex record or mismatched checksum"); + continue; + } + switch (type) { + case '0': + /* Data record. */ + if (sz == 0) + break; + rec_addr = addr_base + addr; + if (first || sec_addr != rec_addr) { + if (s != NULL) + finalize_data_section(s); + s = new_data_section(ecp, sec_index, off, + rec_addr); + if (s == NULL) { + warnx("new_data_section failed"); + break; + } + sec_index++; + sec_addr = rec_addr; + first = 0; + } + append_data(s, data, sz); + off += sz; + sec_addr += sz; + break; + case '1': + /* End of file record. */ + goto done; + case '2': + /* Extended segment address record. */ + addr_base = addr << 4; + break; + case '3': + /* Start segment address record (CS:IP). Ignored. */ + break; + case '4': + /* Extended linear address record. */ + addr_base = num << 16; + break; + case '5': + /* Start linear address record. */ + entry = num; + break; + default: + break; + } + } +done: + if (s != NULL) + finalize_data_section(s); + if (ferror(ifp)) + warn("fgets failed"); + fclose(ifp); + + /* Insert .shstrtab after data sections. */ + if ((ecp->shstrtab->os = elf_newscn(ecp->eout)) == NULL) + errx(EXIT_FAILURE, "elf_newscn failed: %s", + elf_errmsg(-1)); + insert_to_sec_list(ecp, ecp->shstrtab, 1); + + /* Insert section header table here. */ + shtab = insert_shtab(ecp, 1); + + /* Set entry point. */ + oeh.e_entry = entry; + + /* + * Write the underlying ehdr. Note that it should be called + * before elf_setshstrndx() since it will overwrite e->e_shstrndx. + */ + if (gelf_update_ehdr(ecp->eout, &oeh) == 0) + errx(EXIT_FAILURE, "gelf_update_ehdr() failed: %s", + elf_errmsg(-1)); + + /* Generate section name string table (.shstrtab). */ + set_shstrtab(ecp); + + /* Update sh_name pointer for each section header entry. */ + update_shdr(ecp, 0); + + /* Renew oeh to get the updated e_shstrndx. */ + if (gelf_getehdr(ecp->eout, &oeh) == NULL) + errx(EXIT_FAILURE, "gelf_getehdr() failed: %s", + elf_errmsg(-1)); + + /* Resync section offsets. */ + resync_sections(ecp); + + /* Store SHDR offset in EHDR. */ + oeh.e_shoff = shtab->off; + + /* Update ehdr since we modified e_shoff. */ + if (gelf_update_ehdr(ecp->eout, &oeh) == 0) + errx(EXIT_FAILURE, "gelf_update_ehdr() failed: %s", + elf_errmsg(-1)); + + /* Write out the output elf object. */ + if (elf_update(ecp->eout, ELF_C_WRITE) < 0) + errx(EXIT_FAILURE, "elf_update() failed: %s", + elf_errmsg(-1)); + + /* Release allocated resource. */ + free_elf(ecp); +} + +#define _SEC_NAMESZ 64 +#define _SEC_INIT_CAP 1024 + +static struct section * +new_data_section(struct elfcopy *ecp, int sec_index, uint64_t off, + uint64_t addr) +{ + char *name; + + if ((name = malloc(_SEC_NAMESZ)) == NULL) + errx(EXIT_FAILURE, "malloc failed"); + snprintf(name, _SEC_NAMESZ, ".sec%d", sec_index); + + return (create_external_section(ecp, name, name, NULL, 0, off, + SHT_PROGBITS, ELF_T_BYTE, SHF_ALLOC | SHF_WRITE, 1, addr, 0)); +} + +static void +finalize_data_section(struct section *s) +{ + Elf_Data *od; + + if ((od = elf_newdata(s->os)) == NULL) + errx(EXIT_FAILURE, "elf_newdata() failed: %s", + elf_errmsg(-1)); + od->d_align = s->align; + od->d_off = 0; + od->d_buf = s->buf; + od->d_size = s->sz; + od->d_version = EV_CURRENT; +} + +static void +append_data(struct section *s, const void *buf, size_t sz) +{ + uint8_t *p; + + if (s->buf == NULL) { + s->sz = 0; + s->cap = _SEC_INIT_CAP; + if ((s->buf = malloc(s->cap)) == NULL) + err(EXIT_FAILURE, "malloc failed"); + } + + while (sz + s->sz > s->cap) { + s->cap *= 2; + if ((s->buf = realloc(s->buf, s->cap)) == NULL) + err(EXIT_FAILURE, "realloc failed"); + } + + p = s->buf; + memcpy(&p[s->sz], buf, sz); + s->sz += sz; +} + +static int +srec_read(const char *line, char *type, uint64_t *addr, uint8_t *data, + size_t *sz) +{ + uint64_t count, _checksum, num; + size_t addr_sz; + int checksum, i, len; + + checksum = 0; + len = 2; + if (read_num(line, &len, &count, 1, &checksum) < 0) + return (-1); + *type = line[1]; + switch (*type) { + case '0': + case '1': + case '5': + case '9': + addr_sz = 2; + break; + case '2': + case '8': + addr_sz = 3; + break; + case '3': + case '7': + addr_sz = 4; + break; + default: + return (-1); + } + + if (read_num(line, &len, addr, addr_sz, &checksum) < 0) + return (-1); + + count -= addr_sz + 1; + if (*type >= '0' && *type <= '3') { + for (i = 0; (uint64_t) i < count; i++) { + if (read_num(line, &len, &num, 1, &checksum) < 0) + return -1; + data[i] = (uint8_t) num; + } + *sz = count; + } else + *sz = 0; + + if (read_num(line, &len, &_checksum, 1, NULL) < 0) + return (-1); + + if ((int) _checksum != (~checksum & 0xFF)) + return (-1); + + return (0); +} + +static void +srec_write_symtab(int ofd, const char *ofn, Elf *e, Elf_Scn *scn, GElf_Shdr *sh) +{ + char line[_LINE_BUFSZ]; + GElf_Sym sym; + Elf_Data *d; + const char *name; + size_t sc; + int elferr, i; + +#define _WRITE_LINE do { \ + if (write(ofd, line, strlen(line)) != (ssize_t) strlen(line)) \ + errx(EXIT_FAILURE, "write failed"); \ + } while (0) + + + (void) elf_errno(); + if ((d = elf_getdata(scn, NULL)) == NULL) { + elferr = elf_errno(); + if (elferr != 0) + warnx("elf_getdata failed: %s", + elf_errmsg(-1)); + return; + } + if (d->d_buf == NULL || d->d_size == 0) + return; + + snprintf(line, sizeof(line), "$$ %s\r\n", ofn); + _WRITE_LINE; + sc = d->d_size / sh->sh_entsize; + for (i = 1; (size_t) i < sc; i++) { + if (gelf_getsym(d, i, &sym) != &sym) { + warnx("gelf_getsym failed: %s", elf_errmsg(-1)); + continue; + } + if (GELF_ST_TYPE(sym.st_info) == STT_SECTION || + GELF_ST_TYPE(sym.st_info) == STT_FILE) + continue; + if ((name = elf_strptr(e, sh->sh_link, sym.st_name)) == NULL) { + warnx("elf_strptr failed: %s", elf_errmsg(-1)); + continue; + } + snprintf(line, sizeof(line), " %s $%jx\r\n", name, + (uintmax_t) sym.st_value); + _WRITE_LINE; + } + snprintf(line, sizeof(line), "$$ \r\n"); + _WRITE_LINE; + +#undef _WRITE_LINE +} + +static void +srec_write_S0(int ofd, const char *ofn) +{ + + srec_write(ofd, '0', 0, ofn, strlen(ofn)); +} + +static void +srec_write_Sd(int ofd, char dr, uint64_t addr, const void *buf, size_t sz, + size_t rlen) +{ + const uint8_t *p, *pe; + + p = buf; + pe = p + sz; + while (pe - p >= (int) rlen) { + srec_write(ofd, dr, addr, p, rlen); + addr += rlen; + p += rlen; + } + if (pe - p > 0) + srec_write(ofd, dr, addr, p, pe - p); +} + +static void +srec_write_Se(int ofd, uint64_t e_entry, int forceS3) +{ + char er; + + if (e_entry > 0xFFFFFFFF) { + warnx("address space too big for S-Record file"); + return; + } + + if (forceS3) + er = '7'; + else { + if (e_entry <= 0xFFFF) + er = '9'; + else if (e_entry <= 0xFFFFFF) + er = '8'; + else + er = '7'; + } + + srec_write(ofd, er, e_entry, NULL, 0); +} + +static void +srec_write(int ofd, char type, uint64_t addr, const void *buf, size_t sz) +{ + char line[_LINE_BUFSZ]; + const uint8_t *p, *pe; + int len, addr_sz, checksum; + + if (type == '0' || type == '1' || type == '5' || type == '9') + addr_sz = 2; + else if (type == '2' || type == '8') + addr_sz = 3; + else + addr_sz = 4; + + checksum = 0; + line[0] = 'S'; + line[1] = type; + len = 2; + write_num(line, &len, addr_sz + sz + 1, 1, &checksum); + write_num(line, &len, addr, addr_sz, &checksum); + for (p = buf, pe = p + sz; p < pe; p++) + write_num(line, &len, *p, 1, &checksum); + write_num(line, &len, ~checksum & 0xFF, 1, NULL); + line[len++] = '\r'; + line[len++] = '\n'; + if (write(ofd, line, len) != (ssize_t) len) + err(EXIT_FAILURE, "write failed"); +} + +static void +ihex_write_00(int ofd, uint64_t addr, const void *buf, size_t sz) +{ + uint16_t addr_hi, old_addr_hi; + const uint8_t *p, *pe; + + old_addr_hi = (addr >> 16) & 0xFFFF; + p = buf; + pe = p + sz; + while (pe - p >= 16) { + ihex_write(ofd, 0, addr, 0, p, 16); + addr += 16; + p += 16; + addr_hi = (addr >> 16) & 0xFFFF; + if (addr_hi != old_addr_hi) { + old_addr_hi = addr_hi; + ihex_write_04(ofd, addr_hi); + } + } + if (pe - p > 0) + ihex_write(ofd, 0, addr, 0, p, pe - p); +} + +static int +ihex_read(const char *line, char *type, uint64_t *addr, uint64_t *num, + uint8_t *data, size_t *sz) +{ + uint64_t count, _checksum; + int checksum, i, len; + + *sz = 0; + checksum = 0; + len = 1; + if (read_num(line, &len, &count, 1, &checksum) < 0) + return (-1); + if (read_num(line, &len, addr, 2, &checksum) < 0) + return (-1); + if (line[len++] != '0') + return (-1); + *type = line[len++]; + checksum += *type - '0'; + switch (*type) { + case '0': + for (i = 0; (uint64_t) i < count; i++) { + if (read_num(line, &len, num, 1, &checksum) < 0) + return (-1); + data[i] = (uint8_t) *num; + } + *sz = count; + break; + case '1': + if (count != 0) + return (-1); + break; + case '2': + case '4': + if (count != 2) + return (-1); + if (read_num(line, &len, num, 2, &checksum) < 0) + return (-1); + break; + case '3': + case '5': + if (count != 4) + return (-1); + if (read_num(line, &len, num, 4, &checksum) < 0) + return (-1); + break; + default: + return (-1); + } + + if (read_num(line, &len, &_checksum, 1, &checksum) < 0) + return (-1); + + if ((checksum & 0xFF) != 0) { + return (-1); + } + + return (0); +} + +static void +ihex_write_01(int ofd) +{ + + ihex_write(ofd, 1, 0, 0, NULL, 0); +} + +static void +ihex_write_04(int ofd, uint16_t addr) +{ + + ihex_write(ofd, 4, 0, addr, NULL, 2); +} + +static void +ihex_write_05(int ofd, uint64_t e_entry) +{ + + if (e_entry > 0xFFFFFFFF) { + warnx("address space too big for Intel Hex file"); + return; + } + + ihex_write(ofd, 5, 0, e_entry, NULL, 4); +} + +static void +ihex_write(int ofd, int type, uint64_t addr, uint64_t num, const void *buf, + size_t sz) +{ + char line[_LINE_BUFSZ]; + const uint8_t *p, *pe; + int len, checksum; + + if (sz > 16) + errx(EXIT_FAILURE, "Internal: ihex_write() sz too big"); + checksum = 0; + line[0] = ':'; + len = 1; + write_num(line, &len, sz, 1, &checksum); + write_num(line, &len, addr, 2, &checksum); + write_num(line, &len, type, 1, &checksum); + if (sz > 0) { + if (buf != NULL) { + for (p = buf, pe = p + sz; p < pe; p++) + write_num(line, &len, *p, 1, &checksum); + } else + write_num(line, &len, num, sz, &checksum); + } + write_num(line, &len, (~checksum + 1) & 0xFF, 1, NULL); + line[len++] = '\r'; + line[len++] = '\n'; + if (write(ofd, line, len) != (ssize_t) len) + err(EXIT_FAILURE, "write failed"); +} + +static int +read_num(const char *line, int *len, uint64_t *num, size_t sz, int *checksum) +{ + uint8_t b; + + *num = 0; + for (; sz > 0; sz--) { + if (!ishexdigit(line[*len]) || !ishexdigit(line[*len + 1])) + return (-1); + b = (hex_value(line[*len]) << 4) | hex_value(line[*len + 1]); + *num = (*num << 8) | b; + *len += 2; + if (checksum != NULL) + *checksum = (*checksum + b) & 0xFF; + } + + return (0); +} + +static void +write_num(char *line, int *len, uint64_t num, size_t sz, int *checksum) +{ + uint8_t b; + + for (; sz > 0; sz--) { + b = (num >> ((sz - 1) * 8)) & 0xFF; + line[*len] = hex_digit((b >> 4) & 0xF); + line[*len + 1] = hex_digit(b & 0xF); + *len += 2; + if (checksum != NULL) + *checksum = (*checksum + b) & 0xFF; + } +} + +static char +hex_digit(uint8_t n) +{ + + return ((n < 10) ? '0' + n : 'A' + (n - 10)); +} + +static int +hex_value(int x) +{ + + if (isdigit(x)) + return (x - '0'); + else if (x >= 'a' && x <= 'f') + return (x - 'a' + 10); + else + return (x - 'A' + 10); +} + +static int +ishexdigit(int x) +{ + + if (isdigit(x)) + return (1); + if ((x >= 'a' && x <= 'f') || (x >= 'A' && x <= 'F')) + return (1); + + return (0); +} diff --git a/contrib/elftoolchain/elfcopy/binary.c b/contrib/elftoolchain/elfcopy/binary.c new file mode 100644 index 000000000000..23e46e77f392 --- /dev/null +++ b/contrib/elftoolchain/elfcopy/binary.c @@ -0,0 +1,285 @@ +/*- + * Copyright (c) 2010,2011 Kai Wang + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <sys/param.h> +#include <sys/stat.h> +#include <err.h> +#include <gelf.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include "elfcopy.h" + +ELFTC_VCSID("$Id: binary.c 3174 2015-03-27 17:13:41Z emaste $"); + +/* + * Convert ELF object to `binary'. Sections with SHF_ALLOC flag set + * are copied to the result binary. The relative offsets for each section + * are retained, so the result binary file might contain "holes". + */ +void +create_binary(int ifd, int ofd) +{ + Elf *e; + Elf_Scn *scn; + Elf_Data *d; + GElf_Shdr sh; + off_t base, off; + int elferr; + + if ((e = elf_begin(ifd, ELF_C_READ, NULL)) == NULL) + errx(EXIT_FAILURE, "elf_begin() failed: %s", + elf_errmsg(-1)); + + base = 0; + if (lseek(ofd, base, SEEK_SET) < 0) + err(EXIT_FAILURE, "lseek failed"); + + /* + * Find base offset in the first iteration. + */ + base = -1; + scn = NULL; + while ((scn = elf_nextscn(e, scn)) != NULL) { + if (gelf_getshdr(scn, &sh) == NULL) { + warnx("gelf_getshdr failed: %s", elf_errmsg(-1)); + (void) elf_errno(); + continue; + } + if ((sh.sh_flags & SHF_ALLOC) == 0 || + sh.sh_type == SHT_NOBITS || + sh.sh_size == 0) + continue; + if (base == -1 || (off_t) sh.sh_offset < base) + base = sh.sh_offset; + } + elferr = elf_errno(); + if (elferr != 0) + warnx("elf_nextscn failed: %s", elf_errmsg(elferr)); + + if (base == -1) + return; + + /* + * Write out sections in the second iteration. + */ + scn = NULL; + while ((scn = elf_nextscn(e, scn)) != NULL) { + if (gelf_getshdr(scn, &sh) == NULL) { + warnx("gelf_getshdr failed: %s", elf_errmsg(-1)); + (void) elf_errno(); + continue; + } + if ((sh.sh_flags & SHF_ALLOC) == 0 || + sh.sh_type == SHT_NOBITS || + sh.sh_size == 0) + continue; + (void) elf_errno(); + if ((d = elf_getdata(scn, NULL)) == NULL) { + elferr = elf_errno(); + if (elferr != 0) + warnx("elf_getdata failed: %s", elf_errmsg(-1)); + continue; + } + if (d->d_buf == NULL || d->d_size == 0) + continue; + + /* lseek to section offset relative to `base'. */ + off = sh.sh_offset - base; + if (lseek(ofd, off, SEEK_SET) < 0) + err(EXIT_FAILURE, "lseek failed"); + + /* Write out section contents. */ + if (write(ofd, d->d_buf, d->d_size) != (ssize_t) d->d_size) + err(EXIT_FAILURE, "write failed"); + } + elferr = elf_errno(); + if (elferr != 0) + warnx("elf_nextscn failed: %s", elf_errmsg(elferr)); +} + +#define _SYMBOL_NAMSZ 1024 + +/* + * Convert `binary' to ELF object. The input `binary' is converted to + * a relocatable (.o) file, a few symbols will also be created to make + * it easier to access the binary data in other compilation units. + */ +void +create_elf_from_binary(struct elfcopy *ecp, int ifd, const char *ifn) +{ + char name[_SYMBOL_NAMSZ]; + struct section *sec, *sec_temp, *shtab; + struct stat sb; + GElf_Ehdr oeh; + GElf_Shdr sh; + void *content; + uint64_t off, data_start, data_end, data_size; + + /* Reset internal section list. */ + if (!TAILQ_EMPTY(&ecp->v_sec)) + TAILQ_FOREACH_SAFE(sec, &ecp->v_sec, sec_list, sec_temp) { + TAILQ_REMOVE(&ecp->v_sec, sec, sec_list); + free(sec); + } + + if (fstat(ifd, &sb) == -1) + err(EXIT_FAILURE, "fstat failed"); + + /* Read the input binary file to a internal buffer. */ + if ((content = malloc(sb.st_size)) == NULL) + err(EXIT_FAILURE, "malloc failed"); + if (read(ifd, content, sb.st_size) != sb.st_size) + err(EXIT_FAILURE, "read failed"); + + /* + * TODO: copy the input binary to output binary verbatim if -O is not + * specified. + */ + + /* Create EHDR for output .o file. */ + if (gelf_newehdr(ecp->eout, ecp->oec) == NULL) + errx(EXIT_FAILURE, "gelf_newehdr failed: %s", + elf_errmsg(-1)); + if (gelf_getehdr(ecp->eout, &oeh) == NULL) + errx(EXIT_FAILURE, "gelf_getehdr() failed: %s", + elf_errmsg(-1)); + + /* Initialise e_ident fields. */ + oeh.e_ident[EI_CLASS] = ecp->oec; + oeh.e_ident[EI_DATA] = ecp->oed; + /* + * TODO: Set OSABI according to the OS platform where elfcopy(1) + * was build. (probably) + */ + oeh.e_ident[EI_OSABI] = ELFOSABI_NONE; + oeh.e_machine = ecp->oem; + oeh.e_type = ET_REL; + oeh.e_entry = 0; + + ecp->flags |= RELOCATABLE; + + /* Create .shstrtab section */ + init_shstrtab(ecp); + ecp->shstrtab->off = 0; + + /* + * Create `.data' section which contains the binary data. The + * section is inserted immediately after EHDR. + */ + off = gelf_fsize(ecp->eout, ELF_T_EHDR, 1, EV_CURRENT); + if (off == 0) + errx(EXIT_FAILURE, "gelf_fsize() failed: %s", elf_errmsg(-1)); + (void) create_external_section(ecp, ".data", NULL, content, sb.st_size, + off, SHT_PROGBITS, ELF_T_BYTE, SHF_ALLOC | SHF_WRITE, 1, 0, 1); + + /* Insert .shstrtab after .data section. */ + if ((ecp->shstrtab->os = elf_newscn(ecp->eout)) == NULL) + errx(EXIT_FAILURE, "elf_newscn failed: %s", + elf_errmsg(-1)); + insert_to_sec_list(ecp, ecp->shstrtab, 1); + + /* Insert section header table here. */ + shtab = insert_shtab(ecp, 1); + + /* Count in .symtab and .strtab section headers. */ + shtab->sz += gelf_fsize(ecp->eout, ELF_T_SHDR, 2, EV_CURRENT); + +#define _GEN_SYMNAME(S) do { \ + snprintf(name, sizeof(name), "%s%s%s", "_binary_", ifn, S); \ +} while (0) + + /* + * Create symbol table. + */ + create_external_symtab(ecp); + data_start = 0; + data_end = data_start + sb.st_size; + data_size = sb.st_size; + _GEN_SYMNAME("_start"); + add_to_symtab(ecp, name, data_start, 0, 1, + ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0, 1); + _GEN_SYMNAME("_end"); + add_to_symtab(ecp, name, data_end, 0, 1, + ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0, 1); + _GEN_SYMNAME("_size"); + add_to_symtab(ecp, name, data_size, 0, SHN_ABS, + ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0, 1); + finalize_external_symtab(ecp); + create_symtab_data(ecp); +#undef _GEN_SYMNAME + + /* + * Write the underlying ehdr. Note that it should be called + * before elf_setshstrndx() since it will overwrite e->e_shstrndx. + */ + if (gelf_update_ehdr(ecp->eout, &oeh) == 0) + errx(EXIT_FAILURE, "gelf_update_ehdr() failed: %s", + elf_errmsg(-1)); + + /* Generate section name string table (.shstrtab). */ + ecp->flags |= SYMTAB_EXIST; + set_shstrtab(ecp); + + /* Update sh_name pointer for each section header entry. */ + update_shdr(ecp, 0); + + /* Properly set sh_link field of .symtab section. */ + if (gelf_getshdr(ecp->symtab->os, &sh) == NULL) + errx(EXIT_FAILURE, "692 gelf_getshdr() failed: %s", + elf_errmsg(-1)); + sh.sh_link = elf_ndxscn(ecp->strtab->os); + if (!gelf_update_shdr(ecp->symtab->os, &sh)) + errx(EXIT_FAILURE, "gelf_update_shdr() failed: %s", + elf_errmsg(-1)); + + /* Renew oeh to get the updated e_shstrndx. */ + if (gelf_getehdr(ecp->eout, &oeh) == NULL) + errx(EXIT_FAILURE, "gelf_getehdr() failed: %s", + elf_errmsg(-1)); + + /* Resync section offsets. */ + resync_sections(ecp); + + /* Store SHDR offset in EHDR. */ + oeh.e_shoff = shtab->off; + + /* Update ehdr since we modified e_shoff. */ + if (gelf_update_ehdr(ecp->eout, &oeh) == 0) + errx(EXIT_FAILURE, "gelf_update_ehdr() failed: %s", + elf_errmsg(-1)); + + /* Write out the output elf object. */ + if (elf_update(ecp->eout, ELF_C_WRITE) < 0) + errx(EXIT_FAILURE, "elf_update() failed: %s", + elf_errmsg(-1)); + + /* Release allocated resource. */ + free(content); + free_elf(ecp); +} diff --git a/contrib/elftoolchain/elfcopy/elfcopy.1 b/contrib/elftoolchain/elfcopy/elfcopy.1 new file mode 100644 index 000000000000..4889570ebc8e --- /dev/null +++ b/contrib/elftoolchain/elfcopy/elfcopy.1 @@ -0,0 +1,333 @@ +.\" Copyright (c) 2008-2009,2011 Joseph Koshy. All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" This software is provided by Joseph Koshy ``as is'' and +.\" any express or implied warranties, including, but not limited to, the +.\" implied warranties of merchantability and fitness for a particular purpose +.\" are disclaimed. in no event shall Joseph Koshy be liable +.\" for any direct, indirect, incidental, special, exemplary, or consequential +.\" damages (including, but not limited to, procurement of substitute goods +.\" or services; loss of use, data, or profits; or business interruption) +.\" however caused and on any theory of liability, whether in contract, strict +.\" liability, or tort (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: elfcopy.1 3195 2015-05-12 17:22:19Z emaste $ +.\" +.Dd March 27, 2015 +.Os +.Dt ELFCOPY 1 +.Sh NAME +.Nm elfcopy +.Nd copy and translate object files +.Sh SYNOPSIS +.Nm +.Op Fl I Ar objformat | Fl s Ar objformat | Fl -input-target= Ns Ar objformat +.Op Fl K Ar symbolname | Fl -keep-symbol= Ns Ar symbolname +.Op Fl L Ar symbolname | Fl -localize-symbol= Ns Ar symbolname +.Op Fl N Ar symbolname | Fl -strip-symbol= Ns Ar symbolname +.Op Fl O Ar objformat | Fl -output-target= Ns Ar objformat +.Op Fl R Ar sectionname | Fl -remove-section= Ns Ar sectionname +.Op Fl S | Fl -strip-all +.Op Fl V | Fl -version +.Op Fl W Ar symbolname | Fl -weaken-symbol= Ns Ar symbolname +.Op Fl X | Fl -discard-locals +.Op Fl d | Fl g | Fl -strip-debug +.Op Fl h | Fl -help +.Op Fl j Ar sectionname | Fl -only-section= Ns Ar sectionname +.Op Fl p | Fl -preserve-dates +.Op Fl w | Fl -wildcard +.Op Fl x | Fl -discard-all +.Op Fl -add-section Ar sectionname Ns = Ns Ar filename +.Oo +.Fl -adjust-section-vma Ar section Ns {+|-|=} Ns Ar val | +.Fl -change-section-address Ar section Ns {+|-|=} Ns Ar val +.Oc +.Oo +.Fl -adjust-start Ns = Ns Ar increment | +.Fl -change-start Ns = Ns Ar increment +.Oc +.Oo +.Fl -adjust-vma Ns = Ns Ar increment | +.Fl -change-addresses Ns = Ns Ar increment +.Oc +.Op Fl -adjust-warnings | Fl -change-warnings +.Op Fl -change-section-lma Ar section Ns {+|-|=} Ns Ar val +.Op Fl -change-section-vma Ar section Ns {+|-|=} Ns Ar val +.Op Fl -extract-dwo +.Op Fl -gap-fill Ns = Ns Ar val +.Op Fl -localize-hidden +.Op Fl -no-adjust-warnings | Fl -no-change-warnings +.Op Fl -only-keep-debug +.Op Fl -pad-to Ns = Ns Ar address +.Op Fl -prefix-alloc-sections Ns = Ns Ar string +.Op Fl -prefix-sections Ns = Ns Ar string +.Op Fl -prefix-symbols Ns = Ns Ar string +.Op Fl -rename-section Ar oldname Ns = Ns Ar newname Ns Op Ar ,flags +.Op Fl -set-section-flags Ar sectionname Ns = Ns Ar flags +.Op Fl -set-start Ns = Ns Ar address +.Op Fl -srec-forceS3 +.Op Fl -srec-len Ns = Ns Ar val +.Op Fl -strip-dwo +.Op Fl -strip-unneeded +.Ar infile +.Op Ar outfile +.Sh DESCRIPTION +The +.Nm +utility copies the content of the ELF object named by argument +.Ar infile +to that named by argument +.Ar outfile , +transforming it according to the command line options specified. +If argument +.Ar outfile +is not specified, +.Nm +will create a temporary file and will subsequently rename it as +.Ar infile . +.Pp +The +.Nm +utility supports the following options: +.Bl -tag -width indent +.It Fl I Ar objformat | Fl s Ar objformat | Fl -input-target= Ns Ar objformat +Specify that the input file named by the argument +.Ar infile +is in the object format specified by the argument +.Ar objformat . +.It Fl K Ar symbolname | Fl -keep-symbol= Ns Ar symbolname +Copy the symbol named by argument +.Ar symbolname +to the output. +.It Fl L Ar symbolname | Fl -localize-symbol= Ns Ar symbolname +Make the symbol named by argument +.Ar symbolname +local to the output file. +.It Fl N Ar symbol | Fl -strip-symbol= Ns Ar symbolname +Do not copy the symbol named by argument +.Ar symbolname +to the output. +.It Fl O Ar objformat | Fl -output-target= Ns Ar objformat +Write the output file using the object format specified in argument +.Ar objformat . +.It Fl R Ar sectionname | Fl -remove-section= Ns Ar sectionname +Remove any section with name +.Ar sectionname +from the output file. +.It Fl S | Fl -strip-all +Do not copy symbol and relocation information to the target file. +.It Fl V | Fl -version +Print a version identifier and exit. +.It Fl W Ar symbolname | Fl -weaken-symbol= Ns Ar symbolname +Mark the symbol named by argument +.Ar symbolname +as weak in the output. +.It Fl X | Fl -discard-locals +Do not copy compiler generated local symbols to the output. +.It Fl d | Fl g | Fl -strip-debug +Do not copy debugging information to the target file. +.It Fl h | Fl -help +Display a help message and exit. +.It Fl j Ar sectionname | Fl -only-section= Ns Ar sectionname +Copy only the section named by argument +.Ar sectionname +to the output. +.It Fl p | Fl -preserve-dates +Set the access and modification times of the output file to the +same as those of the input. +.It Fl w | Fl -wildcard +Use shell-style patterns to name symbols. +The following meta-characters are recognized in patterns: +.Bl -tag -width "...." -compact +.It Li ! +If this is the first character of the pattern, invert the sense of the +pattern match. +.It Li * +Matches any string of characters in a symbol name. +.It Li ? +Matches zero or one character in a symbol name. +.It Li [ +Mark the start of a character class. +.It Li \e +Remove the special meaning of the next character in the pattern. +.It Li ] +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-section Ar sectionname Ns = Ns Ar filename +Add a new section to the output file with name +.Ar sectionname . +The contents of the section are taken from the file named by +argument +.Ar filename . +The size of the section will be the number of bytes in file +.Ar filename . +.It Xo +.Fl -adjust-section-vma Ar section Ns {+|-|=} Ns Ar val | +.Fl -change-section-address Ar section Ns {+|-|=} Ns Ar val +.Xc +Depending on the operator specified, increase, decrease or set both +the virtual memory address and the load memory address of the section +named by the argument +.Ar section . +The argument +.Ar val +specifies the desired increment, decrement or new value for the +address. +.It Xo +.Fl -adjust-start Ns = Ns Ar increment | +.Fl -change-start Ns = Ns Ar increment +.Xc +Increase the entry point address of the output ELF object by the value +specified in the argument +.Ar increment . +.It Xo +.Fl -adjust-vma Ns = Ns Ar increment | +.Fl -change-addresses Ns = Ns Ar increment +.Xc +Increase the virtual memory address and the load memory address of all +sections by the value specified by the argument +.Ar increment . +.It Fl -adjust-warnings | Fl -change-warnings +Issue a warning if the section specified by the options +.Fl -change-section-address , +.Fl -change-section-lma +or +.Fl -change-section-vma +does not exist in the input object. +This is the default. +.It Fl -change-section-lma Ar section Ns {+|-|=} Ns Ar val +Change or set the load memory address of the section named by the +argument +.Ar section . +Depending on the operator specified, the value in argument +.Ar val +will be used as an increment, a decrement or as the new value +of the load memory address. +.It Fl -change-section-vma Ar section Ns {+|-|=} Ns Ar val +Change or set the virtual memory address of the section named by the +argument +.Ar section . +Depending on the operator specified, the value in argument +.Ar val +will be used as an increment, a decrement or as the new value +of the virtual memory address. +.It Fl -extract-dwo +Copy only .dwo debug sections to the output file. +.It Fl -gap-fill Ns = Ns Ar val +Fill the gaps between sections with the byte value specified by +the argument +.Ar val . +.It Fl -localize-hidden +Make all hidden symbols local to the output file. +This includes symbols with internal visiblity. +.It Fl -no-adjust-warnings | Fl -no-change-warnings +Do not issue a warning if the section specified by the options +.Fl -change-section-address , +.Fl -change-section-lma +or +.Fl -change-section-vma +is missing in the input object. +.It Fl -only-keep-debug +Copy only debugging information to the output file. +.It Fl -pad-to Ns = Ns Ar address +Pad the load memory address of the output object to the value +specified by the argument +.Ar address +by increasing the size of the section with the highest load memory +address. +.It Fl -prefix-alloc-sections Ns = Ns Ar string +Prefix the section names of all the allocated sections with +.Ar string . +.It Fl -prefix-sections Ns = Ns Ar string +Prefix the section names of all the sections with +.Ar string . +.It Fl -prefix-symbols Ns = Ns Ar string +Prefix the symbol names of all the symbols with +.Ar string . +.It Fl -rename-section Ar oldname Ns = Ns Ar newname Ns Op Ar ,flags +Rename the section named by argument +.Ar oldname +to +.Ar newname , +optionally changing the sections flags to that specified by argument +.Ar flags . +Allowed values for the argument +.Ar flags +are as for option +.Fl -set-section-flags +below. +.It Fl -set-section-flags Ar sectionname Ns = Ns Ar flags +Set the flags for the section named by argument +.Ar sectionname +to those specified by argument +.Ar flags . +Argument +.Ar flags +is a comma separated list of the following flag names: +.Bl -tag -width "readonly" -compact +.It alloc +The section occupies space in the output file. +.It code +The section contains machine instructions. +.It contents +This flag is accepted but is ignored. +.It data +The section contains writeable data. +.It debug +The section holds debugging information. +.It load +The section is loadable. +.It noload +The section should not be loaded into memory. +.It readonly +The section is not writable. +.It rom +The section contains ROM'able contents. +.It share +This flag is accepted but is ignored. +.El +.It Fl -set-start Ns = Ns Ar address +Set the start address of the output ELF object to the value specified +by the argument +.Ar address . +.It Fl -srec-forceS3 +Only generate S-records of type +.Dq S3 . +This option is only meaningful when the output target is set to +.Dq srec . +.It Fl -srec-len Ns = Ns Ar val +Set the maximum length of an S-record line to +.Ar val . +This option is only meaningful when the output target is set to +.Dq srec . +.It Fl -strip-dwo +Do not copy .dwo debug sections to the output file. +.It Fl -strip-unneeded +Do not copy symbols that are not needed for relocation processing. +.El +.Sh DIAGNOSTICS +.Ex -std +.Sh SEE ALSO +.Xr ar 1 , +.Xr ld 1 , +.Xr mcs 1 , +.Xr strip 1 , +.Xr elf 3 , +.Xr ar 5 , +.Xr elf 5 +.Sh HISTORY +.Nm +has been implemented by +.An Kai Wang Aq Mt kaiwang27@users.sourceforge.net . diff --git a/contrib/elftoolchain/elfcopy/elfcopy.h b/contrib/elftoolchain/elfcopy/elfcopy.h new file mode 100644 index 000000000000..b750246d850d --- /dev/null +++ b/contrib/elftoolchain/elfcopy/elfcopy.h @@ -0,0 +1,317 @@ +/*- + * Copyright (c) 2007-2013 Kai Wang + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (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: elfcopy.h 3173 2015-03-27 16:46:13Z emaste $ + */ + +#include <sys/queue.h> +#include <gelf.h> +#include <libelftc.h> + +#include "_elftc.h" + +/* + * User specified symbol operation (strip, keep, localize, globalize, + * weaken, rename, etc). + */ +struct symop { + const char *name; + const char *newname; + +#define SYMOP_KEEP 0x0001U +#define SYMOP_STRIP 0x0002U +#define SYMOP_GLOBALIZE 0x0004U +#define SYMOP_LOCALIZE 0x0008U +#define SYMOP_KEEPG 0x0010U +#define SYMOP_WEAKEN 0x0020U +#define SYMOP_REDEF 0x0040U + + unsigned int op; + + STAILQ_ENTRY(symop) symop_list; +}; + +/* File containing symbol list. */ +struct symfile { + dev_t dev; + ino_t ino; + size_t size; + char *data; + unsigned int op; + + STAILQ_ENTRY(symfile) symfile_list; +}; + +/* Sections to copy/remove/rename/... */ +struct sec_action { + const char *name; + const char *addopt; + const char *newname; + const char *string; + uint64_t lma; + uint64_t vma; + int64_t lma_adjust; + int64_t vma_adjust; + +#define SF_ALLOC 0x0001U +#define SF_LOAD 0x0002U +#define SF_NOLOAD 0x0004U +#define SF_READONLY 0x0008U +#define SF_DEBUG 0x0010U +#define SF_CODE 0x0020U +#define SF_DATA 0x0040U +#define SF_ROM 0x0080U +#define SF_SHARED 0X0100U +#define SF_CONTENTS 0x0200U + + int flags; + int add; + int append; + int compress; + int copy; + int print; + int remove; + int rename; + int setflags; + int setlma; + int setvma; + + STAILQ_ENTRY(sec_action) sac_list; +}; + +/* Sections to add from file. */ +struct sec_add { + char *name; + char *content; + size_t size; + + STAILQ_ENTRY(sec_add) sadd_list; +}; + +struct segment; + +/* Internal data structure for sections. */ +struct section { + struct segment *seg; /* containing segment */ + struct segment *seg_tls; /* tls segment */ + const char *name; /* section name */ + char *newname; /* new section name */ + Elf_Scn *is; /* input scn */ + Elf_Scn *os; /* output scn */ + void *buf; /* section content */ + uint8_t *pad; /* section padding */ + uint64_t off; /* section offset */ + uint64_t sz; /* section size */ + uint64_t cap; /* section capacity */ + uint64_t align; /* section alignment */ + uint64_t type; /* section type */ + uint64_t vma; /* section virtual addr */ + uint64_t lma; /* section load addr */ + uint64_t pad_sz;/* section padding size */ + int loadable; /* whether loadable */ + int pseudo; + int nocopy; + + TAILQ_ENTRY(section) sec_list; /* next section */ +}; + +/* Internal data structure for segments. */ +struct segment { + uint64_t addr; /* load addr */ + uint64_t off; /* file offset */ + uint64_t fsz; /* file size */ + uint64_t msz; /* memory size */ + uint64_t type; /* segment type */ + int remove; /* whether remove */ + int nsec; /* number of sections contained */ + struct section **v_sec; /* list of sections contained */ + + STAILQ_ENTRY(segment) seg_list; /* next segment */ +}; + +/* + * In-memory representation of ar(1) archive member(object). + */ +struct ar_obj { + char *name; /* member name */ + char *buf; /* member content */ + void *maddr; /* mmap start address */ + uid_t uid; /* user id */ + gid_t gid; /* group id */ + mode_t md; /* octal file permissions */ + size_t size; /* member size */ + time_t mtime; /* modification time */ + + STAILQ_ENTRY(ar_obj) objs; +}; + +/* + * Structure encapsulates the "global" data for "elfcopy" program. + */ +struct elfcopy { + const char *progname; /* program name */ + int iec; /* elfclass of input object */ + Elftc_Bfd_Target_Flavor itf; /* flavour of input object */ + Elftc_Bfd_Target_Flavor otf; /* flavour of output object */ + const char *otgt; /* output target name */ + int oec; /* elfclass of output object */ + unsigned char oed; /* endianess of output object */ + int oem; /* EM_XXX of output object */ + int abi; /* OSABI of output object */ + Elf *ein; /* ELF descriptor of input object */ + Elf *eout; /* ELF descriptor of output object */ + int iphnum; /* num. of input object phdr entries */ + int ophnum; /* num. of output object phdr entries */ + int nos; /* num. of output object sections */ + + enum { + STRIP_NONE = 0, + STRIP_ALL, + STRIP_DEBUG, + STRIP_DWO, + STRIP_NONDEBUG, + STRIP_NONDWO, + STRIP_UNNEEDED + } strip; + +#define EXECUTABLE 0x00000001U +#define DYNAMIC 0x00000002U +#define RELOCATABLE 0x00000004U +#define SYMTAB_EXIST 0x00000010U +#define SYMTAB_INTACT 0x00000020U +#define KEEP_GLOBAL 0x00000040U +#define DISCARD_LOCAL 0x00000080U +#define WEAKEN_ALL 0x00000100U +#define PRESERVE_DATE 0x00001000U +#define SREC_FORCE_S3 0x00002000U +#define SREC_FORCE_LEN 0x00004000U +#define SET_START 0x00008000U +#define GAP_FILL 0x00010000U +#define WILDCARD 0x00020000U +#define NO_CHANGE_WARN 0x00040000U +#define SEC_ADD 0x00080000U +#define SEC_APPEND 0x00100000U +#define SEC_COMPRESS 0x00200000U +#define SEC_PRINT 0x00400000U +#define SEC_REMOVE 0x00800000U +#define SEC_COPY 0x01000000U +#define DISCARD_LLABEL 0x02000000U +#define LOCALIZE_HIDDEN 0x04000000U + + int flags; /* elfcopy run control flags. */ + int64_t change_addr; /* Section address adjustment. */ + int64_t change_start; /* Entry point adjustment. */ + uint64_t set_start; /* Entry point value. */ + unsigned long srec_len; /* S-Record length. */ + uint64_t pad_to; /* load address padding. */ + uint8_t fill; /* gap fill value. */ + char *prefix_sec; /* section prefix. */ + char *prefix_alloc; /* alloc section prefix. */ + char *prefix_sym; /* symbol prefix. */ + char *debuglink; /* GNU debuglink file. */ + struct section *symtab; /* .symtab section. */ + struct section *strtab; /* .strtab section. */ + struct section *shstrtab; /* .shstrtab section. */ + uint64_t *secndx; /* section index map. */ + uint64_t *symndx; /* symbol index map. */ + unsigned char *v_rel; /* symbols needed by relocation. */ + unsigned char *v_secsym; /* sections with section symbol. */ + STAILQ_HEAD(, segment) v_seg; /* list of segments. */ + STAILQ_HEAD(, sec_action) v_sac;/* list of section operations. */ + STAILQ_HEAD(, sec_add) v_sadd; /* list of sections to add. */ + STAILQ_HEAD(, symop) v_symop; /* list of symbols operations. */ + STAILQ_HEAD(, symfile) v_symfile; /* list of symlist files. */ + TAILQ_HEAD(, section) v_sec; /* list of sections. */ + + /* + * Fields for the ar(1) archive. + */ + char *as; /* buffer for archive string table. */ + size_t as_sz; /* current size of as table. */ + size_t as_cap; /* capacity of as table buffer. */ + uint32_t s_cnt; /* current number of symbols. */ + uint32_t *s_so; /* symbol offset table. */ + size_t s_so_cap; /* capacity of so table buffer. */ + char *s_sn; /* symbol name table */ + size_t s_sn_cap; /* capacity of sn table buffer. */ + size_t s_sn_sz; /* current size of sn table. */ + off_t rela_off; /* offset relative to pseudo members. */ + STAILQ_HEAD(, ar_obj) v_arobj; /* archive object(member) list. */ +}; + +void add_section(struct elfcopy *_ecp, const char *_optarg); +void add_to_shstrtab(struct elfcopy *_ecp, const char *_name); +void add_to_symop_list(struct elfcopy *_ecp, const char *_name, + const char *_newname, unsigned int _op); +void add_to_symtab(struct elfcopy *_ecp, const char *_name, + uint64_t _st_value, uint64_t _st_size, uint16_t _st_shndx, + unsigned char _st_info, unsigned char _st_other, int _ndx_known); +int add_to_inseg_list(struct elfcopy *_ecp, struct section *_sec); +void adjust_addr(struct elfcopy *_ecp); +void copy_content(struct elfcopy *_ecp); +void copy_data(struct section *_s); +void copy_phdr(struct elfcopy *_ecp); +void copy_shdr(struct elfcopy *_ecp, struct section *_s, const char *_name, + int _copy, int _sec_flags); +void create_binary(int _ifd, int _ofd); +void create_elf(struct elfcopy *_ecp); +void create_elf_from_binary(struct elfcopy *_ecp, int _ifd, const char *ifn); +void create_elf_from_ihex(struct elfcopy *_ecp, int _ifd); +void create_elf_from_srec(struct elfcopy *_ecp, int _ifd); +struct section *create_external_section(struct elfcopy *_ecp, const char *_name, + char *_newname, void *_buf, uint64_t _size, uint64_t _off, uint64_t _stype, + Elf_Type _dtype, uint64_t flags, uint64_t _align, uint64_t _vma, + int _loadable); +void create_external_symtab(struct elfcopy *_ecp); +void create_ihex(int _ifd, int _ofd); +void create_scn(struct elfcopy *_ecp); +void create_srec(struct elfcopy *_ecp, int _ifd, int _ofd, const char *_ofn); +void create_symtab(struct elfcopy *_ecp); +void create_symtab_data(struct elfcopy *_ecp); +void create_tempfile(char **_fn, int *_fd); +void finalize_external_symtab(struct elfcopy *_ecp); +void free_elf(struct elfcopy *_ecp); +void free_sec_act(struct elfcopy *_ecp); +void free_sec_add(struct elfcopy *_ecp); +void free_symtab(struct elfcopy *_ecp); +void init_shstrtab(struct elfcopy *_ecp); +void insert_to_sec_list(struct elfcopy *_ecp, struct section *_sec, + int _tail); +struct section *insert_shtab(struct elfcopy *_ecp, int tail); +int is_remove_reloc_sec(struct elfcopy *_ecp, uint32_t _sh_info); +int is_remove_section(struct elfcopy *_ecp, const char *_name); +struct sec_action *lookup_sec_act(struct elfcopy *_ecp, + const char *_name, int _add); +struct symop *lookup_symop_list(struct elfcopy *_ecp, const char *_name, + unsigned int _op); +void resync_sections(struct elfcopy *_ecp); +void set_shstrtab(struct elfcopy *_ecp); +void setup_phdr(struct elfcopy *_ecp); +void update_shdr(struct elfcopy *_ecp, int _update_link); + +#ifndef LIBELF_AR +int ac_detect_ar(int _ifd); +void ac_create_ar(struct elfcopy *_ecp, int _ifd, int _ofd); +#endif /* ! LIBELF_AR */ diff --git a/contrib/elftoolchain/elfcopy/main.c b/contrib/elftoolchain/elfcopy/main.c new file mode 100644 index 000000000000..a48aea589748 --- /dev/null +++ b/contrib/elftoolchain/elfcopy/main.c @@ -0,0 +1,1530 @@ +/*- + * Copyright (c) 2007-2013 Kai Wang + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <sys/param.h> +#include <sys/stat.h> + +#include <err.h> +#include <errno.h> +#include <fcntl.h> +#include <getopt.h> +#include <libelftc.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include "elfcopy.h" + +ELFTC_VCSID("$Id: main.c 3174 2015-03-27 17:13:41Z emaste $"); + +enum options +{ + ECP_ADD_GNU_DEBUGLINK, + ECP_ADD_SECTION, + ECP_CHANGE_ADDR, + ECP_CHANGE_SEC_ADDR, + ECP_CHANGE_SEC_LMA, + ECP_CHANGE_SEC_VMA, + ECP_CHANGE_START, + ECP_CHANGE_WARN, + ECP_GAP_FILL, + ECP_GLOBALIZE_SYMBOL, + ECP_GLOBALIZE_SYMBOLS, + ECP_KEEP_SYMBOLS, + ECP_KEEP_GLOBAL_SYMBOLS, + ECP_LOCALIZE_HIDDEN, + ECP_LOCALIZE_SYMBOLS, + ECP_NO_CHANGE_WARN, + ECP_ONLY_DEBUG, + ECP_ONLY_DWO, + ECP_PAD_TO, + ECP_PREFIX_ALLOC, + ECP_PREFIX_SEC, + ECP_PREFIX_SYM, + ECP_REDEF_SYMBOL, + ECP_REDEF_SYMBOLS, + ECP_RENAME_SECTION, + ECP_SET_OSABI, + ECP_SET_SEC_FLAGS, + ECP_SET_START, + ECP_SREC_FORCE_S3, + ECP_SREC_LEN, + ECP_STRIP_DWO, + ECP_STRIP_SYMBOLS, + ECP_STRIP_UNNEEDED, + ECP_WEAKEN_ALL, + ECP_WEAKEN_SYMBOLS +}; + +static struct option mcs_longopts[] = +{ + { "help", no_argument, NULL, 'h' }, + { "version", no_argument, NULL, 'V' }, + { NULL, 0, NULL, 0 } +}; + +static struct option strip_longopts[] = +{ + {"discard-all", no_argument, NULL, 'x'}, + {"discard-locals", no_argument, NULL, 'X'}, + {"help", no_argument, NULL, 'h'}, + {"input-target", required_argument, NULL, 'I'}, + {"keep-symbol", required_argument, NULL, 'K'}, + {"only-keep-debug", no_argument, NULL, ECP_ONLY_DEBUG}, + {"output-file", required_argument, NULL, 'o'}, + {"output-target", required_argument, NULL, 'O'}, + {"preserve-dates", no_argument, NULL, 'p'}, + {"remove-section", required_argument, NULL, 'R'}, + {"strip-all", no_argument, NULL, 's'}, + {"strip-debug", no_argument, NULL, 'S'}, + {"strip-symbol", required_argument, NULL, 'N'}, + {"strip-unneeded", no_argument, NULL, ECP_STRIP_UNNEEDED}, + {"version", no_argument, NULL, 'V'}, + {"wildcard", no_argument, NULL, 'w'}, + {NULL, 0, NULL, 0} +}; + +static struct option elfcopy_longopts[] = +{ + {"add-gnu-debuglink", required_argument, NULL, ECP_ADD_GNU_DEBUGLINK}, + {"add-section", required_argument, NULL, ECP_ADD_SECTION}, + {"adjust-section-vma", required_argument, NULL, ECP_CHANGE_SEC_ADDR}, + {"adjust-vma", required_argument, NULL, ECP_CHANGE_ADDR}, + {"adjust-start", required_argument, NULL, ECP_CHANGE_START}, + {"adjust-warnings", no_argument, NULL, ECP_CHANGE_WARN}, + {"binary-architecture", required_argument, NULL, 'B'}, + {"change-addresses", required_argument, NULL, ECP_CHANGE_ADDR}, + {"change-section-address", required_argument, NULL, + ECP_CHANGE_SEC_ADDR}, + {"change-section-lma", required_argument, NULL, ECP_CHANGE_SEC_LMA}, + {"change-section-vma", required_argument, NULL, ECP_CHANGE_SEC_VMA}, + {"change-start", required_argument, NULL, ECP_CHANGE_START}, + {"change-warnings", no_argument, NULL, ECP_CHANGE_WARN}, + {"discard-all", no_argument, NULL, 'x'}, + {"discard-locals", no_argument, NULL, 'X'}, + {"extract-dwo", no_argument, NULL, ECP_ONLY_DWO}, + {"gap-fill", required_argument, NULL, ECP_GAP_FILL}, + {"globalize-symbol", required_argument, NULL, ECP_GLOBALIZE_SYMBOL}, + {"globalize-symbols", required_argument, NULL, ECP_GLOBALIZE_SYMBOLS}, + {"help", no_argument, NULL, 'h'}, + {"input-target", required_argument, NULL, 'I'}, + {"keep-symbol", required_argument, NULL, 'K'}, + {"keep-symbols", required_argument, NULL, ECP_KEEP_SYMBOLS}, + {"keep-global-symbol", required_argument, NULL, 'G'}, + {"keep-global-symbols", required_argument, NULL, + ECP_KEEP_GLOBAL_SYMBOLS}, + {"localize-hidden", no_argument, NULL, ECP_LOCALIZE_HIDDEN}, + {"localize-symbol", required_argument, NULL, 'L'}, + {"localize-symbols", required_argument, NULL, ECP_LOCALIZE_SYMBOLS}, + {"no-adjust-warnings", no_argument, NULL, ECP_NO_CHANGE_WARN}, + {"no-change-warnings", no_argument, NULL, ECP_NO_CHANGE_WARN}, + {"only-keep-debug", no_argument, NULL, ECP_ONLY_DEBUG}, + {"only-section", required_argument, NULL, 'j'}, + {"osabi", required_argument, NULL, ECP_SET_OSABI}, + {"output-target", required_argument, NULL, 'O'}, + {"pad-to", required_argument, NULL, ECP_PAD_TO}, + {"preserve-dates", no_argument, NULL, 'p'}, + {"prefix-alloc-sections", required_argument, NULL, ECP_PREFIX_ALLOC}, + {"prefix-sections", required_argument, NULL, ECP_PREFIX_SEC}, + {"prefix-symbols", required_argument, NULL, ECP_PREFIX_SYM}, + {"redefine-sym", required_argument, NULL, ECP_REDEF_SYMBOL}, + {"redefine-syms", required_argument, NULL, ECP_REDEF_SYMBOLS}, + {"remove-section", required_argument, NULL, 'R'}, + {"rename-section", required_argument, NULL, ECP_RENAME_SECTION}, + {"set-section-flags", required_argument, NULL, ECP_SET_SEC_FLAGS}, + {"set-start", required_argument, NULL, ECP_SET_START}, + {"srec-forceS3", no_argument, NULL, ECP_SREC_FORCE_S3}, + {"srec-len", required_argument, NULL, ECP_SREC_LEN}, + {"strip-all", no_argument, NULL, 'S'}, + {"strip-debug", no_argument, 0, 'g'}, + {"strip-dwo", no_argument, NULL, ECP_STRIP_DWO}, + {"strip-symbol", required_argument, NULL, 'N'}, + {"strip-symbols", required_argument, NULL, ECP_STRIP_SYMBOLS}, + {"strip-unneeded", no_argument, NULL, ECP_STRIP_UNNEEDED}, + {"version", no_argument, NULL, 'V'}, + {"weaken", no_argument, NULL, ECP_WEAKEN_ALL}, + {"weaken-symbol", required_argument, NULL, 'W'}, + {"weaken-symbols", required_argument, NULL, ECP_WEAKEN_SYMBOLS}, + {"wildcard", no_argument, NULL, 'w'}, + {NULL, 0, NULL, 0} +}; + +static struct { + const char *name; + int value; +} sec_flags[] = { + {"alloc", SF_ALLOC}, + {"load", SF_LOAD}, + {"noload", SF_NOLOAD}, + {"readonly", SF_READONLY}, + {"debug", SF_DEBUG}, + {"code", SF_CODE}, + {"data", SF_DATA}, + {"rom", SF_ROM}, + {"share", SF_SHARED}, + {"contents", SF_CONTENTS}, + {NULL, 0} +}; + +static struct { + const char *name; + int abi; +} osabis[] = { + {"sysv", ELFOSABI_SYSV}, + {"hpus", ELFOSABI_HPUX}, + {"netbsd", ELFOSABI_NETBSD}, + {"linux", ELFOSABI_LINUX}, + {"hurd", ELFOSABI_HURD}, + {"86open", ELFOSABI_86OPEN}, + {"solaris", ELFOSABI_SOLARIS}, + {"aix", ELFOSABI_AIX}, + {"irix", ELFOSABI_IRIX}, + {"freebsd", ELFOSABI_FREEBSD}, + {"tru64", ELFOSABI_TRU64}, + {"modesto", ELFOSABI_MODESTO}, + {"openbsd", ELFOSABI_OPENBSD}, + {"openvms", ELFOSABI_OPENVMS}, + {"nsk", ELFOSABI_NSK}, + {"arm", ELFOSABI_ARM}, + {"standalone", ELFOSABI_STANDALONE}, + {NULL, 0} +}; + +static int copy_from_tempfile(const char *src, const char *dst, + int infd, int *outfd, int in_place); +static void create_file(struct elfcopy *ecp, const char *src, + const char *dst); +static void elfcopy_main(struct elfcopy *ecp, int argc, char **argv); +static void elfcopy_usage(void); +static void mcs_main(struct elfcopy *ecp, int argc, char **argv); +static void mcs_usage(void); +static void parse_sec_address_op(struct elfcopy *ecp, int optnum, + const char *optname, char *s); +static void parse_sec_flags(struct sec_action *sac, char *s); +static void parse_symlist_file(struct elfcopy *ecp, const char *fn, + unsigned int op); +static void print_version(void); +static void set_input_target(struct elfcopy *ecp, const char *target_name); +static void set_osabi(struct elfcopy *ecp, const char *abi); +static void set_output_target(struct elfcopy *ecp, const char *target_name); +static void strip_main(struct elfcopy *ecp, int argc, char **argv); +static void strip_usage(void); + +/* + * An ELF object usually has a sturcture described by the + * diagram below. + * _____________ + * | | + * | NULL | <- always a SHT_NULL section + * |_____________| + * | | + * | .interp | + * |_____________| + * | | + * | ... | + * |_____________| + * | | + * | .text | + * |_____________| + * | | + * | ... | + * |_____________| + * | | + * | .comment | <- above(include) this: normal sections + * |_____________| + * | | + * | add sections| <- unloadable sections added by --add-section + * |_____________| + * | | + * | .shstrtab | <- section name string table + * |_____________| + * | | + * | shdrs | <- section header table + * |_____________| + * | | + * | .symtab | <- symbol table, if any + * |_____________| + * | | + * | .strtab | <- symbol name string table, if any + * |_____________| + * | | + * | .rel.text | <- relocation info for .o files. + * |_____________| + */ +void +create_elf(struct elfcopy *ecp) +{ + struct section *shtab; + GElf_Ehdr ieh; + GElf_Ehdr oeh; + size_t ishnum; + + ecp->flags |= SYMTAB_INTACT; + + /* Create EHDR. */ + if (gelf_getehdr(ecp->ein, &ieh) == NULL) + errx(EXIT_FAILURE, "gelf_getehdr() failed: %s", + elf_errmsg(-1)); + if ((ecp->iec = gelf_getclass(ecp->ein)) == ELFCLASSNONE) + errx(EXIT_FAILURE, "getclass() failed: %s", + elf_errmsg(-1)); + + if (ecp->oec == ELFCLASSNONE) + ecp->oec = ecp->iec; + if (ecp->oed == ELFDATANONE) + ecp->oed = ieh.e_ident[EI_DATA]; + + if (gelf_newehdr(ecp->eout, ecp->oec) == NULL) + errx(EXIT_FAILURE, "gelf_newehdr failed: %s", + elf_errmsg(-1)); + if (gelf_getehdr(ecp->eout, &oeh) == NULL) + errx(EXIT_FAILURE, "gelf_getehdr() failed: %s", + elf_errmsg(-1)); + + memcpy(oeh.e_ident, ieh.e_ident, sizeof(ieh.e_ident)); + oeh.e_ident[EI_CLASS] = ecp->oec; + oeh.e_ident[EI_DATA] = ecp->oed; + if (ecp->abi != -1) + oeh.e_ident[EI_OSABI] = ecp->abi; + oeh.e_flags = ieh.e_flags; + oeh.e_machine = ieh.e_machine; + oeh.e_type = ieh.e_type; + oeh.e_entry = ieh.e_entry; + oeh.e_version = ieh.e_version; + + if (ieh.e_type == ET_EXEC) + ecp->flags |= EXECUTABLE; + else if (ieh.e_type == ET_DYN) + ecp->flags |= DYNAMIC; + else if (ieh.e_type == ET_REL) + ecp->flags |= RELOCATABLE; + else + errx(EXIT_FAILURE, "unsupported e_type"); + + if (!elf_getshnum(ecp->ein, &ishnum)) + errx(EXIT_FAILURE, "elf_getshnum failed: %s", + elf_errmsg(-1)); + if (ishnum > 0 && (ecp->secndx = calloc(ishnum, + sizeof(*ecp->secndx))) == NULL) + err(EXIT_FAILURE, "calloc failed"); + + /* Read input object program header. */ + setup_phdr(ecp); + + /* + * Scan of input sections: we iterate through sections from input + * object, skip sections need to be stripped, allot Elf_Scn and + * create internal section structure for sections we want. + * (i.e., determine output sections) + */ + create_scn(ecp); + + /* Apply section address changes, if any. */ + adjust_addr(ecp); + + /* + * Determine if the symbol table needs to be changed based on + * command line options. + */ + if (ecp->strip == STRIP_DEBUG || + ecp->strip == STRIP_UNNEEDED || + ecp->flags & WEAKEN_ALL || + ecp->flags & LOCALIZE_HIDDEN || + ecp->flags & DISCARD_LOCAL || + ecp->flags & DISCARD_LLABEL || + ecp->prefix_sym != NULL || + !STAILQ_EMPTY(&ecp->v_symop)) + ecp->flags &= ~SYMTAB_INTACT; + + /* + * Create symbol table. Symbols are filtered or stripped according to + * command line args specified by user, and later updated for the new + * layout of sections in the output object. + */ + if ((ecp->flags & SYMTAB_EXIST) != 0) + create_symtab(ecp); + + /* + * First processing of output sections: at this stage we copy the + * content of each section from input to output object. Section + * content will be modified and printed (mcs) if need. Also content of + * relocation section probably will be filtered and updated according + * to symbol table changes. + */ + copy_content(ecp); + + /* + * Write the underlying ehdr. Note that it should be called + * before elf_setshstrndx() since it will overwrite e->e_shstrndx. + */ + if (gelf_update_ehdr(ecp->eout, &oeh) == 0) + errx(EXIT_FAILURE, "gelf_update_ehdr() failed: %s", + elf_errmsg(-1)); + + /* Generate section name string table (.shstrtab). */ + set_shstrtab(ecp); + + /* + * Second processing of output sections: Update section headers. + * At this stage we set name string index, update st_link and st_info + * for output sections. + */ + update_shdr(ecp, 1); + + /* Renew oeh to get the updated e_shstrndx. */ + if (gelf_getehdr(ecp->eout, &oeh) == NULL) + errx(EXIT_FAILURE, "gelf_getehdr() failed: %s", + elf_errmsg(-1)); + + /* + * Insert SHDR table into the internal section list as a "pseudo" + * section, so later it will get sorted and resynced just as "normal" + * sections. + */ + shtab = insert_shtab(ecp, 0); + + /* + * Resync section offsets in the output object. This is needed + * because probably sections are modified or new sections are added, + * as a result overlap/gap might appears. + */ + resync_sections(ecp); + + /* Store SHDR offset in EHDR. */ + oeh.e_shoff = shtab->off; + + /* Put program header table immediately after the Elf header. */ + if (ecp->ophnum > 0) { + oeh.e_phoff = gelf_fsize(ecp->eout, ELF_T_EHDR, 1, EV_CURRENT); + if (oeh.e_phoff == 0) + errx(EXIT_FAILURE, "gelf_fsize() failed: %s", + elf_errmsg(-1)); + } + + /* + * Update ELF object entry point if requested. + */ + if (ecp->change_addr != 0) + oeh.e_entry += ecp->change_addr; + if (ecp->flags & SET_START) + oeh.e_entry = ecp->set_start; + if (ecp->change_start != 0) + oeh.e_entry += ecp->change_start; + + /* + * Update ehdr again before we call elf_update(), since we + * modified e_shoff and e_phoff. + */ + if (gelf_update_ehdr(ecp->eout, &oeh) == 0) + errx(EXIT_FAILURE, "gelf_update_ehdr() failed: %s", + elf_errmsg(-1)); + + if (ecp->ophnum > 0) + copy_phdr(ecp); + + /* Write out the output elf object. */ + if (elf_update(ecp->eout, ELF_C_WRITE) < 0) + errx(EXIT_FAILURE, "elf_update() failed: %s", + elf_errmsg(-1)); + + /* Release allocated resource. */ + free_elf(ecp); +} + +void +free_elf(struct elfcopy *ecp) +{ + struct segment *seg, *seg_temp; + struct section *sec, *sec_temp; + + /* Free internal segment list. */ + if (!STAILQ_EMPTY(&ecp->v_seg)) { + STAILQ_FOREACH_SAFE(seg, &ecp->v_seg, seg_list, seg_temp) { + STAILQ_REMOVE(&ecp->v_seg, seg, segment, seg_list); + free(seg); + } + } + + /* Free symbol table buffers. */ + free_symtab(ecp); + + /* Free internal section list. */ + if (!TAILQ_EMPTY(&ecp->v_sec)) { + TAILQ_FOREACH_SAFE(sec, &ecp->v_sec, sec_list, sec_temp) { + TAILQ_REMOVE(&ecp->v_sec, sec, sec_list); + if (sec->buf != NULL) + free(sec->buf); + if (sec->newname != NULL) + free(sec->newname); + if (sec->pad != NULL) + free(sec->pad); + free(sec); + } + } +} + +/* Create a temporary file. */ +void +create_tempfile(char **fn, int *fd) +{ + const char *tmpdir; + char *cp, *tmpf; + size_t tlen, plen; + +#define _TEMPFILE "ecp.XXXXXXXX" +#define _TEMPFILEPATH "/tmp/ecp.XXXXXXXX" + + if (fn == NULL || fd == NULL) + return; + /* Repect TMPDIR environment variable. */ + tmpdir = getenv("TMPDIR"); + if (tmpdir != NULL && *tmpdir != '\0') { + tlen = strlen(tmpdir); + plen = strlen(_TEMPFILE); + tmpf = malloc(tlen + plen + 2); + if (tmpf == NULL) + err(EXIT_FAILURE, "malloc failed"); + strncpy(tmpf, tmpdir, tlen); + cp = &tmpf[tlen - 1]; + if (*cp++ != '/') + *cp++ = '/'; + strncpy(cp, _TEMPFILE, plen); + cp[plen] = '\0'; + } else { + tmpf = strdup(_TEMPFILEPATH); + if (tmpf == NULL) + err(EXIT_FAILURE, "strdup failed"); + } + if ((*fd = mkstemp(tmpf)) == -1) + err(EXIT_FAILURE, "mkstemp %s failed", tmpf); + if (fchmod(*fd, 0644) == -1) + err(EXIT_FAILURE, "fchmod %s failed", tmpf); + *fn = tmpf; + +#undef _TEMPFILE +#undef _TEMPFILEPATH +} + +/* + * Copy temporary file with path src and file descriptor infd to path dst. + * If in_place is set act as if editing the file in place, avoiding rename() + * to preserve hard and symbolic links. Output file remains open, with file + * descriptor returned in outfd. + */ +static int +copy_from_tempfile(const char *src, const char *dst, int infd, int *outfd, + int in_place) +{ + int tmpfd; + + /* + * First, check if we can use rename(). + */ + if (in_place == 0) { + if (rename(src, dst) >= 0) { + *outfd = infd; + return (0); + } else if (errno != EXDEV) + return (-1); + + /* + * If the rename() failed due to 'src' and 'dst' residing in + * two different file systems, invoke a helper function in + * libelftc to do the copy. + */ + + if (unlink(dst) < 0) + return (-1); + } + + if ((tmpfd = open(dst, O_CREAT | O_TRUNC | O_WRONLY, 0755)) < 0) + return (-1); + + if (elftc_copyfile(infd, tmpfd) < 0) + return (-1); + + /* + * Remove the temporary file from the file system + * namespace, and close its file descriptor. + */ + if (unlink(src) < 0) + return (-1); + + (void) close(infd); + + /* + * Return the file descriptor for the destination. + */ + *outfd = tmpfd; + + return (0); +} + +static void +create_file(struct elfcopy *ecp, const char *src, const char *dst) +{ + struct stat sb; + char *tempfile, *elftemp; + int efd, ifd, ofd, ofd0, tfd; + int in_place; + + tempfile = NULL; + + if (src == NULL) + errx(EXIT_FAILURE, "internal: src == NULL"); + if ((ifd = open(src, O_RDONLY)) == -1) + err(EXIT_FAILURE, "open %s failed", src); + + if (fstat(ifd, &sb) == -1) + err(EXIT_FAILURE, "fstat %s failed", src); + + if (dst == NULL) + create_tempfile(&tempfile, &ofd); + else + if ((ofd = open(dst, O_RDWR|O_CREAT, 0755)) == -1) + err(EXIT_FAILURE, "open %s failed", dst); + +#ifndef LIBELF_AR + /* Detect and process ar(1) archive using libarchive. */ + if (ac_detect_ar(ifd)) { + ac_create_ar(ecp, ifd, ofd); + goto copy_done; + } +#endif + + if (lseek(ifd, 0, SEEK_SET) < 0) + err(EXIT_FAILURE, "lseek failed"); + + /* + * If input object is not ELF file, convert it to an intermediate + * ELF object before processing. + */ + if (ecp->itf != ETF_ELF) { + create_tempfile(&elftemp, &efd); + if ((ecp->eout = elf_begin(efd, ELF_C_WRITE, NULL)) == NULL) + errx(EXIT_FAILURE, "elf_begin() failed: %s", + elf_errmsg(-1)); + elf_flagelf(ecp->eout, ELF_C_SET, ELF_F_LAYOUT); + if (ecp->itf == ETF_BINARY) + create_elf_from_binary(ecp, ifd, src); + else if (ecp->itf == ETF_IHEX) + create_elf_from_ihex(ecp, ifd); + else if (ecp->itf == ETF_SREC) + create_elf_from_srec(ecp, ifd); + else + errx(EXIT_FAILURE, "Internal: invalid target flavour"); + elf_end(ecp->eout); + + /* Open intermediate ELF object as new input object. */ + close(ifd); + if ((ifd = open(elftemp, O_RDONLY)) == -1) + err(EXIT_FAILURE, "open %s failed", src); + close(efd); + free(elftemp); + } + + if ((ecp->ein = elf_begin(ifd, ELF_C_READ, NULL)) == NULL) + errx(EXIT_FAILURE, "elf_begin() failed: %s", + elf_errmsg(-1)); + + switch (elf_kind(ecp->ein)) { + case ELF_K_NONE: + errx(EXIT_FAILURE, "file format not recognized"); + case ELF_K_ELF: + if ((ecp->eout = elf_begin(ofd, ELF_C_WRITE, NULL)) == NULL) + errx(EXIT_FAILURE, "elf_begin() failed: %s", + elf_errmsg(-1)); + + /* elfcopy(1) manage ELF layout by itself. */ + elf_flagelf(ecp->eout, ELF_C_SET, ELF_F_LAYOUT); + + /* + * Create output ELF object. + */ + create_elf(ecp); + elf_end(ecp->eout); + + /* + * Convert the output ELF object to binary/srec/ihex if need. + */ + if (ecp->otf != ETF_ELF) { + /* + * Create (another) tempfile for binary/srec/ihex + * output object. + */ + if (tempfile != NULL) { + if (unlink(tempfile) < 0) + err(EXIT_FAILURE, "unlink %s failed", + tempfile); + free(tempfile); + } + create_tempfile(&tempfile, &ofd0); + + + /* + * Rewind the file descriptor being processed. + */ + if (lseek(ofd, 0, SEEK_SET) < 0) + err(EXIT_FAILURE, + "lseek failed for the output object"); + + /* + * Call flavour-specific conversion routine. + */ + switch (ecp->otf) { + case ETF_BINARY: + create_binary(ofd, ofd0); + break; + case ETF_IHEX: + create_ihex(ofd, ofd0); + break; + case ETF_SREC: + create_srec(ecp, ofd, ofd0, + dst != NULL ? dst : src); + break; + default: + errx(EXIT_FAILURE, "Internal: unsupported" + " output flavour %d", ecp->oec); + } + + close(ofd); + ofd = ofd0; + } + + break; + + case ELF_K_AR: + /* XXX: Not yet supported. */ + break; + default: + errx(EXIT_FAILURE, "file format not supported"); + } + + elf_end(ecp->ein); + +#ifndef LIBELF_AR +copy_done: +#endif + + if (tempfile != NULL) { + in_place = 0; + if (dst == NULL) { + dst = src; + if (lstat(dst, &sb) != -1 && + (sb.st_nlink > 1 || S_ISLNK(sb.st_mode))) + in_place = 1; + } + + if (copy_from_tempfile(tempfile, dst, ofd, &tfd, in_place) < 0) + err(EXIT_FAILURE, "creation of %s failed", dst); + + free(tempfile); + tempfile = NULL; + + ofd = tfd; + } + + if (strcmp(dst, "/dev/null") && fchmod(ofd, sb.st_mode) == -1) + err(EXIT_FAILURE, "fchmod %s failed", dst); + + if ((ecp->flags & PRESERVE_DATE) && + elftc_set_timestamps(dst, &sb) < 0) + err(EXIT_FAILURE, "setting timestamps failed"); + + close(ifd); + close(ofd); +} + +static void +elfcopy_main(struct elfcopy *ecp, int argc, char **argv) +{ + struct sec_action *sac; + const char *infile, *outfile; + char *fn, *s; + int opt; + + while ((opt = getopt_long(argc, argv, "dB:gG:I:j:K:L:N:O:pR:s:SwW:xXV", + elfcopy_longopts, NULL)) != -1) { + switch(opt) { + case 'B': + /* ignored */ + break; + case 'R': + sac = lookup_sec_act(ecp, optarg, 1); + if (sac->copy != 0) + errx(EXIT_FAILURE, + "both copy and remove specified"); + sac->remove = 1; + ecp->flags |= SEC_REMOVE; + break; + case 'S': + ecp->strip = STRIP_ALL; + break; + case 'g': + ecp->strip = STRIP_DEBUG; + break; + case 'G': + ecp->flags |= KEEP_GLOBAL; + add_to_symop_list(ecp, optarg, NULL, SYMOP_KEEPG); + break; + case 'I': + case 's': + set_input_target(ecp, optarg); + break; + case 'j': + sac = lookup_sec_act(ecp, optarg, 1); + if (sac->remove != 0) + errx(EXIT_FAILURE, + "both copy and remove specified"); + sac->copy = 1; + ecp->flags |= SEC_COPY; + break; + case 'K': + add_to_symop_list(ecp, optarg, NULL, SYMOP_KEEP); + break; + case 'L': + add_to_symop_list(ecp, optarg, NULL, SYMOP_LOCALIZE); + break; + case 'N': + add_to_symop_list(ecp, optarg, NULL, SYMOP_STRIP); + break; + case 'O': + set_output_target(ecp, optarg); + break; + case 'p': + ecp->flags |= PRESERVE_DATE; + break; + case 'V': + print_version(); + break; + case 'w': + ecp->flags |= WILDCARD; + break; + case 'W': + add_to_symop_list(ecp, optarg, NULL, SYMOP_WEAKEN); + break; + case 'x': + ecp->flags |= DISCARD_LOCAL; + break; + case 'X': + ecp->flags |= DISCARD_LLABEL; + break; + case ECP_ADD_GNU_DEBUGLINK: + ecp->debuglink = optarg; + break; + case ECP_ADD_SECTION: + add_section(ecp, optarg); + break; + case ECP_CHANGE_ADDR: + ecp->change_addr = (int64_t) strtoll(optarg, NULL, 0); + break; + case ECP_CHANGE_SEC_ADDR: + parse_sec_address_op(ecp, opt, "--change-section-addr", + optarg); + break; + case ECP_CHANGE_SEC_LMA: + parse_sec_address_op(ecp, opt, "--change-section-lma", + optarg); + break; + case ECP_CHANGE_SEC_VMA: + parse_sec_address_op(ecp, opt, "--change-section-vma", + optarg); + break; + case ECP_CHANGE_START: + ecp->change_start = (int64_t) strtoll(optarg, NULL, 0); + break; + case ECP_CHANGE_WARN: + /* default */ + break; + case ECP_GAP_FILL: + ecp->fill = (uint8_t) strtoul(optarg, NULL, 0); + ecp->flags |= GAP_FILL; + break; + case ECP_GLOBALIZE_SYMBOL: + add_to_symop_list(ecp, optarg, NULL, SYMOP_GLOBALIZE); + break; + case ECP_GLOBALIZE_SYMBOLS: + parse_symlist_file(ecp, optarg, SYMOP_GLOBALIZE); + break; + case ECP_KEEP_SYMBOLS: + parse_symlist_file(ecp, optarg, SYMOP_KEEP); + break; + case ECP_KEEP_GLOBAL_SYMBOLS: + parse_symlist_file(ecp, optarg, SYMOP_KEEPG); + break; + case ECP_LOCALIZE_HIDDEN: + ecp->flags |= LOCALIZE_HIDDEN; + break; + case ECP_LOCALIZE_SYMBOLS: + parse_symlist_file(ecp, optarg, SYMOP_LOCALIZE); + break; + case ECP_NO_CHANGE_WARN: + ecp->flags |= NO_CHANGE_WARN; + break; + case ECP_ONLY_DEBUG: + ecp->strip = STRIP_NONDEBUG; + break; + case ECP_ONLY_DWO: + ecp->strip = STRIP_NONDWO; + break; + case ECP_PAD_TO: + ecp->pad_to = (uint64_t) strtoull(optarg, NULL, 0); + break; + case ECP_PREFIX_ALLOC: + ecp->prefix_alloc = optarg; + break; + case ECP_PREFIX_SEC: + ecp->prefix_sec = optarg; + break; + case ECP_PREFIX_SYM: + ecp->prefix_sym = optarg; + break; + case ECP_REDEF_SYMBOL: + if ((s = strchr(optarg, '=')) == NULL) + errx(EXIT_FAILURE, + "illegal format for --redefine-sym"); + *s++ = '\0'; + add_to_symop_list(ecp, optarg, s, SYMOP_REDEF); + break; + case ECP_REDEF_SYMBOLS: + parse_symlist_file(ecp, optarg, SYMOP_REDEF); + break; + case ECP_RENAME_SECTION: + if ((fn = strchr(optarg, '=')) == NULL) + errx(EXIT_FAILURE, + "illegal format for --rename-section"); + *fn++ = '\0'; + + /* Check for optional flags. */ + if ((s = strchr(fn, ',')) != NULL) + *s++ = '\0'; + + sac = lookup_sec_act(ecp, optarg, 1); + sac->rename = 1; + sac->newname = fn; + if (s != NULL) + parse_sec_flags(sac, s); + break; + case ECP_SET_OSABI: + set_osabi(ecp, optarg); + break; + case ECP_SET_SEC_FLAGS: + if ((s = strchr(optarg, '=')) == NULL) + errx(EXIT_FAILURE, + "illegal format for --set-section-flags"); + *s++ = '\0'; + sac = lookup_sec_act(ecp, optarg, 1); + parse_sec_flags(sac, s); + break; + case ECP_SET_START: + ecp->flags |= SET_START; + ecp->set_start = (uint64_t) strtoull(optarg, NULL, 0); + break; + case ECP_SREC_FORCE_S3: + ecp->flags |= SREC_FORCE_S3; + break; + case ECP_SREC_LEN: + ecp->flags |= SREC_FORCE_LEN; + ecp->srec_len = strtoul(optarg, NULL, 0); + break; + case ECP_STRIP_DWO: + ecp->strip = STRIP_DWO; + break; + case ECP_STRIP_SYMBOLS: + parse_symlist_file(ecp, optarg, SYMOP_STRIP); + break; + case ECP_STRIP_UNNEEDED: + ecp->strip = STRIP_UNNEEDED; + break; + case ECP_WEAKEN_ALL: + ecp->flags |= WEAKEN_ALL; + break; + case ECP_WEAKEN_SYMBOLS: + parse_symlist_file(ecp, optarg, SYMOP_WEAKEN); + break; + default: + elfcopy_usage(); + } + } + + if (optind == argc || optind + 2 < argc) + elfcopy_usage(); + + infile = argv[optind]; + outfile = NULL; + if (optind + 1 < argc) + outfile = argv[optind + 1]; + + create_file(ecp, infile, outfile); +} + +static void +mcs_main(struct elfcopy *ecp, int argc, char **argv) +{ + struct sec_action *sac; + const char *string; + int append, delete, compress, name, print; + int opt, i; + + append = delete = compress = name = print = 0; + string = NULL; + while ((opt = getopt_long(argc, argv, "a:cdhn:pV", mcs_longopts, + NULL)) != -1) { + switch(opt) { + case 'a': + append = 1; + string = optarg; /* XXX multiple -a not supported */ + break; + case 'c': + compress = 1; + break; + case 'd': + delete = 1; + break; + case 'n': + name = 1; + (void)lookup_sec_act(ecp, optarg, 1); + break; + case 'p': + print = 1; + break; + case 'V': + print_version(); + break; + case 'h': + default: + mcs_usage(); + } + } + + if (optind == argc) + mcs_usage(); + + /* Must specify one operation at least. */ + if (!append && !compress && !delete && !print) + mcs_usage(); + + /* + * If we are going to delete, ignore other operations. This is + * different from the Solaris implementation, which can print + * and delete a section at the same time, for example. Also, this + * implementation do not respect the order between operations that + * user specified, i.e., "mcs -pc a.out" equals to "mcs -cp a.out". + */ + if (delete) { + append = compress = print = 0; + ecp->flags |= SEC_REMOVE; + } + if (append) + ecp->flags |= SEC_APPEND; + if (compress) + ecp->flags |= SEC_COMPRESS; + if (print) + ecp->flags |= SEC_PRINT; + + /* .comment is the default section to operate on. */ + if (!name) + (void)lookup_sec_act(ecp, ".comment", 1); + + STAILQ_FOREACH(sac, &ecp->v_sac, sac_list) { + sac->append = append; + sac->compress = compress; + sac->print = print; + sac->remove = delete; + sac->string = string; + } + + for (i = optind; i < argc; i++) { + /* If only -p is specified, output to /dev/null */ + if (print && !append && !compress && !delete) + create_file(ecp, argv[i], "/dev/null"); + else + create_file(ecp, argv[i], NULL); + } +} + +static void +strip_main(struct elfcopy *ecp, int argc, char **argv) +{ + struct sec_action *sac; + const char *outfile; + int opt; + int i; + + outfile = NULL; + while ((opt = getopt_long(argc, argv, "hI:K:N:o:O:pR:sSdgVxXw", + strip_longopts, NULL)) != -1) { + switch(opt) { + case 'R': + sac = lookup_sec_act(ecp, optarg, 1); + sac->remove = 1; + ecp->flags |= SEC_REMOVE; + break; + case 's': + ecp->strip = STRIP_ALL; + break; + case 'S': + case 'g': + case 'd': + ecp->strip = STRIP_DEBUG; + break; + case 'I': + /* ignored */ + break; + case 'K': + add_to_symop_list(ecp, optarg, NULL, SYMOP_KEEP); + break; + case 'N': + add_to_symop_list(ecp, optarg, NULL, SYMOP_STRIP); + break; + case 'o': + outfile = optarg; + break; + case 'O': + set_output_target(ecp, optarg); + break; + case 'p': + ecp->flags |= PRESERVE_DATE; + break; + case 'V': + print_version(); + break; + case 'w': + ecp->flags |= WILDCARD; + break; + case 'x': + ecp->flags |= DISCARD_LOCAL; + break; + case 'X': + ecp->flags |= DISCARD_LLABEL; + break; + case ECP_ONLY_DEBUG: + ecp->strip = STRIP_NONDEBUG; + break; + case ECP_STRIP_UNNEEDED: + ecp->strip = STRIP_UNNEEDED; + break; + case 'h': + default: + strip_usage(); + } + } + + if (ecp->strip == 0 && + ((ecp->flags & DISCARD_LOCAL) == 0) && + ((ecp->flags & DISCARD_LLABEL) == 0) && + lookup_symop_list(ecp, NULL, SYMOP_STRIP) == NULL) + ecp->strip = STRIP_ALL; + if (optind == argc) + strip_usage(); + + for (i = optind; i < argc; i++) + create_file(ecp, argv[i], outfile); +} + +static void +parse_sec_flags(struct sec_action *sac, char *s) +{ + const char *flag; + int found, i; + + for (flag = strtok(s, ","); flag; flag = strtok(NULL, ",")) { + found = 0; + for (i = 0; sec_flags[i].name != NULL; i++) + if (strcasecmp(sec_flags[i].name, flag) == 0) { + sac->flags |= sec_flags[i].value; + found = 1; + break; + } + if (!found) + errx(EXIT_FAILURE, "unrecognized section flag %s", + flag); + } +} + +static void +parse_sec_address_op(struct elfcopy *ecp, int optnum, const char *optname, + char *s) +{ + struct sec_action *sac; + const char *name; + char *v; + char op; + + name = v = s; + do { + v++; + } while (*v != '\0' && *v != '=' && *v != '+' && *v != '-'); + if (*v == '\0' || *(v + 1) == '\0') + errx(EXIT_FAILURE, "invalid format for %s", optname); + op = *v; + *v++ = '\0'; + sac = lookup_sec_act(ecp, name, 1); + switch (op) { + case '=': + if (optnum == ECP_CHANGE_SEC_LMA || + optnum == ECP_CHANGE_SEC_ADDR) { + sac->setlma = 1; + sac->lma = (uint64_t) strtoull(v, NULL, 0); + } + if (optnum == ECP_CHANGE_SEC_VMA || + optnum == ECP_CHANGE_SEC_ADDR) { + sac->setvma = 1; + sac->vma = (uint64_t) strtoull(v, NULL, 0); + } + break; + case '+': + if (optnum == ECP_CHANGE_SEC_LMA || + optnum == ECP_CHANGE_SEC_ADDR) + sac->lma_adjust = (int64_t) strtoll(v, NULL, 0); + if (optnum == ECP_CHANGE_SEC_VMA || + optnum == ECP_CHANGE_SEC_ADDR) + sac->vma_adjust = (int64_t) strtoll(v, NULL, 0); + break; + case '-': + if (optnum == ECP_CHANGE_SEC_LMA || + optnum == ECP_CHANGE_SEC_ADDR) + sac->lma_adjust = (int64_t) -strtoll(v, NULL, 0); + if (optnum == ECP_CHANGE_SEC_VMA || + optnum == ECP_CHANGE_SEC_ADDR) + sac->vma_adjust = (int64_t) -strtoll(v, NULL, 0); + break; + default: + break; + } +} + +static void +parse_symlist_file(struct elfcopy *ecp, const char *fn, unsigned int op) +{ + struct symfile *sf; + struct stat sb; + FILE *fp; + char *data, *p, *line, *end, *e, *n; + + if (stat(fn, &sb) == -1) + err(EXIT_FAILURE, "stat %s failed", fn); + + /* Check if we already read and processed this file. */ + STAILQ_FOREACH(sf, &ecp->v_symfile, symfile_list) { + if (sf->dev == sb.st_dev && sf->ino == sb.st_ino) + goto process_symfile; + } + + if ((fp = fopen(fn, "r")) == NULL) + err(EXIT_FAILURE, "can not open %s", fn); + if ((data = malloc(sb.st_size + 1)) == NULL) + err(EXIT_FAILURE, "malloc failed"); + if (fread(data, 1, sb.st_size, fp) == 0 || ferror(fp)) + err(EXIT_FAILURE, "fread failed"); + fclose(fp); + data[sb.st_size] = '\0'; + + if ((sf = calloc(1, sizeof(*sf))) == NULL) + err(EXIT_FAILURE, "malloc failed"); + sf->dev = sb.st_dev; + sf->ino = sb.st_ino; + sf->size = sb.st_size + 1; + sf->data = data; + +process_symfile: + + /* + * Basically what we do here is to convert EOL to '\0', and remove + * leading and trailing whitespaces for each line. + */ + + end = sf->data + sf->size; + line = NULL; + for(p = sf->data; p < end; p++) { + if ((*p == '\t' || *p == ' ') && line == NULL) + continue; + if (*p == '\r' || *p == '\n' || *p == '\0') { + *p = '\0'; + if (line == NULL) + continue; + + /* Skip comment. */ + if (*line == '#') { + line = NULL; + continue; + } + + e = p - 1; + while(e != line && (*e == '\t' || *e == ' ')) + *e-- = '\0'; + if (op != SYMOP_REDEF) + add_to_symop_list(ecp, line, NULL, op); + else { + if (strlen(line) < 3) + errx(EXIT_FAILURE, + "illegal format for" + " --redefine-sym"); + for(n = line + 1; n < e; n++) { + if (*n == ' ' || *n == '\t') { + while(*n == ' ' || *n == '\t') + *n++ = '\0'; + break; + } + } + if (n >= e) + errx(EXIT_FAILURE, + "illegal format for" + " --redefine-sym"); + add_to_symop_list(ecp, line, n, op); + } + line = NULL; + continue; + } + + if (line == NULL) + line = p; + } +} + +static void +set_input_target(struct elfcopy *ecp, const char *target_name) +{ + Elftc_Bfd_Target *tgt; + + if ((tgt = elftc_bfd_find_target(target_name)) == NULL) + errx(EXIT_FAILURE, "%s: invalid target name", target_name); + ecp->itf = elftc_bfd_target_flavor(tgt); +} + +static void +set_output_target(struct elfcopy *ecp, const char *target_name) +{ + Elftc_Bfd_Target *tgt; + + if ((tgt = elftc_bfd_find_target(target_name)) == NULL) + errx(EXIT_FAILURE, "%s: invalid target name", target_name); + ecp->otf = elftc_bfd_target_flavor(tgt); + if (ecp->otf == ETF_ELF) { + ecp->oec = elftc_bfd_target_class(tgt); + ecp->oed = elftc_bfd_target_byteorder(tgt); + ecp->oem = elftc_bfd_target_machine(tgt); + } + ecp->otgt = target_name; +} + +static void +set_osabi(struct elfcopy *ecp, const char *abi) +{ + int i, found; + + found = 0; + for (i = 0; osabis[i].name != NULL; i++) + if (strcasecmp(osabis[i].name, abi) == 0) { + ecp->abi = osabis[i].abi; + found = 1; + break; + } + if (!found) + errx(EXIT_FAILURE, "unrecognized OSABI %s", abi); +} + +#define ELFCOPY_USAGE_MESSAGE "\ +Usage: %s [options] infile [outfile]\n\ + Transform an ELF object.\n\n\ + Options:\n\ + -d | -g | --strip-debug Remove debugging information from the output.\n\ + -j SECTION | --only-section=SECTION\n\ + Copy only the named section to the output.\n\ + -p | --preserve-dates Preserve access and modification times.\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\ + -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\ + -R NAME | --remove-section=NAME\n\ + Remove the named section.\n\ + -S | --strip-all Remove all symbol and relocation information\n\ + from the output.\n\ + -V | --version Print a version identifier and exit.\n\ + -W SYM | --weaken-symbol=SYM Mark symbol SYM as weak in the output.\n\ + -X | --discard-locals Do not copy compiler generated symbols to\n\ + the output.\n\ + --add-section NAME=FILE Add the contents of FILE to the ELF object as\n\ + a new section named NAME.\n\ + --adjust-section-vma SECTION{=,+,-}VAL | \\\n\ + --change-section-address SECTION{=,+,-}VAL\n\ + Set or adjust the VMA and the LMA of the\n\ + named section by VAL.\n\ + --adjust-start=INCR | --change-start=INCR\n\ + Add INCR to the start address for the ELF\n\ + object.\n\ + --adjust-vma=INCR | --change-addresses=INCR\n\ + Increase the VMA and LMA of all sections by\n\ + INCR.\n\ + --adjust-warning | --change-warnings\n\ + Issue warnings for non-existent sections.\n\ + --change-section-lma SECTION{=,+,-}VAL\n\ + Set or adjust the LMA address of the named\n\ + section by VAL.\n\ + --change-section-vma SECTION{=,+,-}VAL\n\ + Set or adjust the VMA address of the named\n\ + section by VAL.\n\ + --gap-fill=VAL Fill the gaps between sections with bytes\n\ + of value VAL.\n\ + --localize-hidden Make all hidden symbols local to the output\n\ + file.\n\ + --no-adjust-warning| --no-change-warnings\n\ + Do not issue warnings for non-existent\n\ + sections.\n\ + --only-keep-debug Copy only debugging information.\n\ + --output-target=FORMAT Use the specified format for the output.\n\ + --pad-to=ADDRESS Pad the output object upto the given address.\n\ + --prefix-alloc-sections=STRING\n\ + Prefix the section names of all the allocated\n\ + sections with STRING.\n\ + --prefix-sections=STRING Prefix the section names of all the sections\n\ + with STRING.\n\ + --prefix-symbols=STRING Prefix the symbol names of all the symbols\n\ + with STRING.\n\ + --rename-section OLDNAME=NEWNAME[,FLAGS]\n\ + Rename and optionally change section flags.\n\ + --set-section-flags SECTION=FLAGS\n\ + Set section flags for the named section.\n\ + Supported flags are: 'alloc', 'code',\n\ + 'contents', 'data', 'debug', 'load',\n\ + 'noload', 'readonly', 'rom', and 'shared'.\n\ + --set-start=ADDRESS Set the start address of the ELF object.\n\ + --srec-forceS3 Only generate S3 S-Records.\n\ + --srec-len=LEN Set the maximum length of a S-Record line.\n\ + --strip-unneeded Do not copy relocation information.\n" + +static void +elfcopy_usage(void) +{ + (void) fprintf(stderr, ELFCOPY_USAGE_MESSAGE, ELFTC_GETPROGNAME()); + exit(EXIT_FAILURE); +} + +#define MCS_USAGE_MESSAGE "\ +Usage: %s [options] file...\n\ + Manipulate the comment section in an ELF object.\n\n\ + Options:\n\ + -a STRING Append 'STRING' to the comment section.\n\ + -c Remove duplicate entries from the comment section.\n\ + -d Delete the comment section.\n\ + -h | --help Print a help message and exit.\n\ + -n NAME Operate on the ELF section with name 'NAME'.\n\ + -p Print the contents of the comment section.\n\ + -V | --version Print a version identifier and exit.\n" + +static void +mcs_usage(void) +{ + (void) fprintf(stderr, MCS_USAGE_MESSAGE, ELFTC_GETPROGNAME()); + exit(EXIT_FAILURE); +} + +#define STRIP_USAGE_MESSAGE "\ +Usage: %s [options] file...\n\ + Discard information from ELF objects.\n\n\ + Options:\n\ + -d | -g | -S | --strip-debug Remove debugging symbols.\n\ + -h | --help Print a help message.\n\ + --only-keep-debug Keep debugging information only.\n\ + -p | --preserve-dates Preserve access and modification times.\n\ + -s | --strip-all Remove all symbols.\n\ + --strip-unneeded Remove symbols not needed for relocation\n\ + processing.\n\ + -w | --wildcard Use shell-style patterns to name symbols.\n\ + -x | --discard-all Discard all non-global symbols.\n\ + -I TGT| --input-target=TGT (Accepted, but ignored).\n\ + -K SYM | --keep-symbol=SYM Keep symbol 'SYM' in the output.\n\ + -N SYM | --strip-symbol=SYM Remove symbol 'SYM' from the output.\n\ + -O TGT | --output-target=TGT Set the output file format to 'TGT'.\n\ + -R SEC | --remove-section=SEC Remove the section named 'SEC'.\n\ + -V | --version Print a version identifier and exit.\n\ + -X | --discard-locals Remove compiler-generated local symbols.\n" + +static void +strip_usage(void) +{ + (void) fprintf(stderr, STRIP_USAGE_MESSAGE, ELFTC_GETPROGNAME()); + exit(EXIT_FAILURE); +} + +static void +print_version(void) +{ + (void) printf("%s (%s)\n", ELFTC_GETPROGNAME(), elftc_version()); + exit(EXIT_SUCCESS); +} + +int +main(int argc, char **argv) +{ + struct elfcopy *ecp; + + if (elf_version(EV_CURRENT) == EV_NONE) + errx(EXIT_FAILURE, "ELF library initialization failed: %s", + elf_errmsg(-1)); + + ecp = calloc(1, sizeof(*ecp)); + if (ecp == NULL) + err(EXIT_FAILURE, "calloc failed"); + memset(ecp, 0, sizeof(*ecp)); + + ecp->itf = ecp->otf = ETF_ELF; + ecp->iec = ecp->oec = ELFCLASSNONE; + ecp->oed = ELFDATANONE; + ecp->abi = -1; + /* There is always an empty section. */ + ecp->nos = 1; + ecp->fill = 0; + + STAILQ_INIT(&ecp->v_seg); + STAILQ_INIT(&ecp->v_sac); + STAILQ_INIT(&ecp->v_sadd); + STAILQ_INIT(&ecp->v_symop); + STAILQ_INIT(&ecp->v_symfile); + STAILQ_INIT(&ecp->v_arobj); + TAILQ_INIT(&ecp->v_sec); + + if ((ecp->progname = ELFTC_GETPROGNAME()) == NULL) + ecp->progname = "elfcopy"; + + if (strcmp(ecp->progname, "strip") == 0) + strip_main(ecp, argc, argv); + else if (strcmp(ecp->progname, "mcs") == 0) + mcs_main(ecp, argc, argv); + else + elfcopy_main(ecp, argc, argv); + + free_sec_add(ecp); + free_sec_act(ecp); + free(ecp); + + exit(EXIT_SUCCESS); +} diff --git a/contrib/elftoolchain/elfcopy/mcs.1 b/contrib/elftoolchain/elfcopy/mcs.1 new file mode 100644 index 000000000000..edbafb7b88a7 --- /dev/null +++ b/contrib/elftoolchain/elfcopy/mcs.1 @@ -0,0 +1,125 @@ +.\" Copyright (c) 2011 Joseph Koshy. All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY JOSEPH KOSHY ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL JOSEPH KOSHY BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (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: mcs.1 2247 2011-11-29 08:41:34Z jkoshy $ +.\" +.Dd November 29, 2011 +.Os +.Dt MCS 1 +.Sh NAME +.Nm mcs +.Nd manipulate the comment section of an ELF object +.Sh SYNOPSIS +.Nm +.Op Fl a Ar string +.Op Fl c +.Op Fl n Ar name +.Op Fl p +.Ar +.Nm +.Fl d +.Op Fl n Ar name +.Ar +.Nm +.Fl h | Fl -help +.Nm +.Fl V | Fl -version +.Sh DESCRIPTION +The +.Nm +utility is used to manipulate comment sections in an ELF object. +If a command-line argument +.Ar file +names an +.Xr ar 1 +archive, then +.Nm +will operate on the ELF objects contained in the archive. +.Pp +By default +.Nm +operates on the ELF section named +.Dq .comment . +This may be changed using the +.Fl n +option. +.Pp +The +.Nm +utility supports the following options: +.Bl -tag -width ".Fl a Ar string" +.It Fl a Ar string +Append the text in +.Ar string +to the comment section. +This option may be specified multiple times. +.It Fl c +Compress the comment section by removing duplicate entries. +.It Fl d +Delete the comment section from the ELF object. +.It Fl h | Fl -help +Display a usage message and exit. +.It Fl n Ar name +Operate on the section named +.Ar name . +.It Fl p +Print the contents of the comment section. +This step is taken after actions specified by the +.Fl a +and +.Fl c +options (if any) are completed. +.It Fl V | Fl -version +Print a version identifier and exit. +.El +.Sh COMPATIBILITY +The behavior of the +.Nm +utility differs from its SVR4 counterpart in the following ways: +.Bl -bullet -compact +.It +If the +.Fl d +option is specified, it causes any +.Fl a , +.Fl c +and +.Fl p +options present to be ignored. +.It +The order of options +.Fl a , +.Fl c , +.Fl d , +and +.Fl p +on the command line is not significant. +.El +.Sh DIAGNOSTICS +.Ex -std +.Sh SEE ALSO +.Xr ar 1 , +.Xr elfcopy 1 , +.Xr ld 1 , +.Xr nm 1 , +.Xr strip 1 diff --git a/contrib/elftoolchain/elfcopy/sections.c b/contrib/elftoolchain/elfcopy/sections.c new file mode 100644 index 000000000000..7c43a3577f2f --- /dev/null +++ b/contrib/elftoolchain/elfcopy/sections.c @@ -0,0 +1,1573 @@ +/*- + * Copyright (c) 2007-2011,2014 Kai Wang + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <sys/param.h> +#include <sys/stat.h> +#include <err.h> +#include <libgen.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "elfcopy.h" + +ELFTC_VCSID("$Id: sections.c 3185 2015-04-11 08:56:34Z kaiwang27 $"); + +static void add_gnu_debuglink(struct elfcopy *ecp); +static uint32_t calc_crc32(const char *p, size_t len, uint32_t crc); +static void check_section_rename(struct elfcopy *ecp, struct section *s); +static void filter_reloc(struct elfcopy *ecp, struct section *s); +static int get_section_flags(struct elfcopy *ecp, const char *name); +static void insert_sections(struct elfcopy *ecp); +static void insert_to_strtab(struct section *t, const char *s); +static int is_append_section(struct elfcopy *ecp, const char *name); +static int is_compress_section(struct elfcopy *ecp, const char *name); +static int is_debug_section(const char *name); +static int is_dwo_section(const char *name); +static int is_modify_section(struct elfcopy *ecp, const char *name); +static int is_print_section(struct elfcopy *ecp, const char *name); +static int lookup_string(struct section *t, const char *s); +static void modify_section(struct elfcopy *ecp, struct section *s); +static void pad_section(struct elfcopy *ecp, struct section *s); +static void print_data(const char *d, size_t sz); +static void print_section(struct section *s); +static void *read_section(struct section *s, size_t *size); +static void update_reloc(struct elfcopy *ecp, struct section *s); + +int +is_remove_section(struct elfcopy *ecp, const char *name) +{ + + /* Always keep section name table */ + if (strcmp(name, ".shstrtab") == 0) + return 0; + if (strcmp(name, ".symtab") == 0 || + strcmp(name, ".strtab") == 0) { + if (ecp->strip == STRIP_ALL && lookup_symop_list( + ecp, NULL, SYMOP_KEEP) == NULL) + return (1); + else + return (0); + } + + if (ecp->strip == STRIP_DWO && is_dwo_section(name)) + return (1); + if (ecp->strip == STRIP_NONDWO && !is_dwo_section(name)) + return (1); + + if (is_debug_section(name)) { + if (ecp->strip == STRIP_ALL || + ecp->strip == STRIP_DEBUG || + ecp->strip == STRIP_UNNEEDED || + (ecp->flags & DISCARD_LOCAL)) + return (1); + if (ecp->strip == STRIP_NONDEBUG) + return (0); + } + + if ((ecp->flags & SEC_REMOVE) || (ecp->flags & SEC_COPY)) { + struct sec_action *sac; + + sac = lookup_sec_act(ecp, name, 0); + if ((ecp->flags & SEC_REMOVE) && sac != NULL && sac->remove) + return (1); + if ((ecp->flags & SEC_COPY) && (sac == NULL || !sac->copy)) + return (1); + } + + return (0); +} + +/* + * Relocation section needs to be removed if the section it applies to + * will be removed. + */ +int +is_remove_reloc_sec(struct elfcopy *ecp, uint32_t sh_info) +{ + const char *name; + GElf_Shdr ish; + Elf_Scn *is; + size_t indx; + int elferr; + + if (elf_getshstrndx(ecp->ein, &indx) == 0) + errx(EXIT_FAILURE, "elf_getshstrndx failed: %s", + elf_errmsg(-1)); + + is = NULL; + while ((is = elf_nextscn(ecp->ein, is)) != NULL) { + if (sh_info == elf_ndxscn(is)) { + if (gelf_getshdr(is, &ish) == NULL) + errx(EXIT_FAILURE, "gelf_getshdr failed: %s", + elf_errmsg(-1)); + if ((name = elf_strptr(ecp->ein, indx, ish.sh_name)) == + NULL) + errx(EXIT_FAILURE, "elf_strptr failed: %s", + elf_errmsg(-1)); + if (is_remove_section(ecp, name)) + return (1); + else + return (0); + } + } + elferr = elf_errno(); + if (elferr != 0) + errx(EXIT_FAILURE, "elf_nextscn failed: %s", + elf_errmsg(elferr)); + + /* Remove reloc section if we can't find the target section. */ + return (1); +} + +static int +is_append_section(struct elfcopy *ecp, const char *name) +{ + struct sec_action *sac; + + sac = lookup_sec_act(ecp, name, 0); + if (sac != NULL && sac->append != 0 && sac->string != NULL) + return (1); + + return (0); +} + +static int +is_compress_section(struct elfcopy *ecp, const char *name) +{ + struct sec_action *sac; + + sac = lookup_sec_act(ecp, name, 0); + if (sac != NULL && sac->compress != 0) + return (1); + + return (0); +} + +static void +check_section_rename(struct elfcopy *ecp, struct section *s) +{ + struct sec_action *sac; + char *prefix; + size_t namelen; + + if (s->pseudo) + return; + + sac = lookup_sec_act(ecp, s->name, 0); + if (sac != NULL && sac->rename) + s->name = sac->newname; + + if (!strcmp(s->name, ".symtab") || + !strcmp(s->name, ".strtab") || + !strcmp(s->name, ".shstrtab")) + return; + + prefix = NULL; + if (s->loadable && ecp->prefix_alloc != NULL) + prefix = ecp->prefix_alloc; + else if (ecp->prefix_sec != NULL) + prefix = ecp->prefix_sec; + + if (prefix != NULL) { + namelen = strlen(s->name) + strlen(prefix) + 1; + if ((s->newname = malloc(namelen)) == NULL) + err(EXIT_FAILURE, "malloc failed"); + snprintf(s->newname, namelen, "%s%s", prefix, s->name); + s->name = s->newname; + } +} + +static int +get_section_flags(struct elfcopy *ecp, const char *name) +{ + struct sec_action *sac; + + sac = lookup_sec_act(ecp, name, 0); + if (sac != NULL && sac->flags) + return sac->flags; + + return (0); +} + +/* + * Determine whether the section are debugging section. + * According to libbfd, debugging sections are recognized + * only by name. + */ +static int +is_debug_section(const char *name) +{ + const char *dbg_sec[] = { + ".debug", + ".gnu.linkonce.wi.", + ".line", + ".stab", + NULL + }; + const char **p; + + for(p = dbg_sec; *p; p++) { + if (strncmp(name, *p, strlen(*p)) == 0) + return (1); + } + + return (0); +} + +static int +is_dwo_section(const char *name) +{ + size_t len; + + if ((len = strlen(name)) > 4 && strcmp(name + len - 4, ".dwo") == 0) + return (1); + return (0); +} + +static int +is_print_section(struct elfcopy *ecp, const char *name) +{ + struct sec_action *sac; + + sac = lookup_sec_act(ecp, name, 0); + if (sac != NULL && sac->print != 0) + return (1); + + return (0); +} + +static int +is_modify_section(struct elfcopy *ecp, const char *name) +{ + + if (is_append_section(ecp, name) || + is_compress_section(ecp, name)) + return (1); + + return (0); +} + +struct sec_action* +lookup_sec_act(struct elfcopy *ecp, const char *name, int add) +{ + struct sec_action *sac; + + if (name == NULL) + return NULL; + + STAILQ_FOREACH(sac, &ecp->v_sac, sac_list) { + if (strcmp(name, sac->name) == 0) + return sac; + } + + if (add == 0) + return NULL; + + if ((sac = malloc(sizeof(*sac))) == NULL) + errx(EXIT_FAILURE, "not enough memory"); + memset(sac, 0, sizeof(*sac)); + sac->name = name; + STAILQ_INSERT_TAIL(&ecp->v_sac, sac, sac_list); + + return (sac); +} + +void +free_sec_act(struct elfcopy *ecp) +{ + struct sec_action *sac, *sac_temp; + + STAILQ_FOREACH_SAFE(sac, &ecp->v_sac, sac_list, sac_temp) { + STAILQ_REMOVE(&ecp->v_sac, sac, sec_action, sac_list); + free(sac); + } +} + +void +insert_to_sec_list(struct elfcopy *ecp, struct section *sec, int tail) +{ + struct section *s; + + if (!tail) { + TAILQ_FOREACH(s, &ecp->v_sec, sec_list) { + if (sec->off < s->off) { + TAILQ_INSERT_BEFORE(s, sec, sec_list); + goto inc_nos; + } + } + } + + TAILQ_INSERT_TAIL(&ecp->v_sec, sec, sec_list); + +inc_nos: + if (sec->pseudo == 0) + ecp->nos++; +} + +/* + * First step of section creation: create scn and internal section + * structure, discard sections to be removed. + */ +void +create_scn(struct elfcopy *ecp) +{ + struct section *s; + const char *name; + Elf_Scn *is; + GElf_Shdr ish; + size_t indx; + uint64_t oldndx, newndx; + int elferr, sec_flags; + + /* + * Insert a pseudo section that contains the ELF header + * and program header. Used as reference for section offset + * or load address adjustment. + */ + if ((s = calloc(1, sizeof(*s))) == NULL) + err(EXIT_FAILURE, "calloc failed"); + s->off = 0; + s->sz = gelf_fsize(ecp->eout, ELF_T_EHDR, 1, EV_CURRENT) + + gelf_fsize(ecp->eout, ELF_T_PHDR, ecp->ophnum, EV_CURRENT); + s->align = 1; + s->pseudo = 1; + s->loadable = add_to_inseg_list(ecp, s); + insert_to_sec_list(ecp, s, 0); + + /* Create internal .shstrtab section. */ + init_shstrtab(ecp); + + if (elf_getshstrndx(ecp->ein, &indx) == 0) + errx(EXIT_FAILURE, "elf_getshstrndx failed: %s", + elf_errmsg(-1)); + + is = NULL; + while ((is = elf_nextscn(ecp->ein, is)) != NULL) { + if (gelf_getshdr(is, &ish) == NULL) + errx(EXIT_FAILURE, "219 gelf_getshdr failed: %s", + elf_errmsg(-1)); + if ((name = elf_strptr(ecp->ein, indx, ish.sh_name)) == NULL) + errx(EXIT_FAILURE, "elf_strptr failed: %s", + elf_errmsg(-1)); + + /* Skip sections to be removed. */ + if (is_remove_section(ecp, name)) + continue; + + /* + * Relocation section need to be remove if the section + * it applies will be removed. + */ + if (ish.sh_type == SHT_REL || ish.sh_type == SHT_RELA) + if (ish.sh_info != 0 && + is_remove_reloc_sec(ecp, ish.sh_info)) + continue; + + /* + * Section groups should be removed if symbol table will + * be removed. (section group's signature stored in symbol + * table) + */ + if (ish.sh_type == SHT_GROUP && ecp->strip == STRIP_ALL) + continue; + + /* Get section flags set by user. */ + sec_flags = get_section_flags(ecp, name); + + /* Create internal section object. */ + if (strcmp(name, ".shstrtab") != 0) { + if ((s = calloc(1, sizeof(*s))) == NULL) + err(EXIT_FAILURE, "calloc failed"); + s->name = name; + s->is = is; + s->off = ish.sh_offset; + s->sz = ish.sh_size; + s->align = ish.sh_addralign; + s->type = ish.sh_type; + s->vma = ish.sh_addr; + + /* + * Search program headers to determine whether section + * is loadable, but if user explicitly set section flags + * while neither "load" nor "alloc" is set, we make the + * section unloadable. + */ + if (sec_flags && + (sec_flags & (SF_LOAD | SF_ALLOC)) == 0) + s->loadable = 0; + else + s->loadable = add_to_inseg_list(ecp, s); + } else { + /* Assuming .shstrtab is "unloadable". */ + s = ecp->shstrtab; + s->off = ish.sh_offset; + } + + oldndx = newndx = SHN_UNDEF; + if (strcmp(name, ".symtab") != 0 && + strcmp(name, ".strtab") != 0) { + if (!strcmp(name, ".shstrtab")) { + /* + * Add sections specified by --add-section and + * gnu debuglink. we want these sections have + * smaller index than .shstrtab section. + */ + if (ecp->debuglink != NULL) + add_gnu_debuglink(ecp); + if (ecp->flags & SEC_ADD) + insert_sections(ecp); + } + if ((s->os = elf_newscn(ecp->eout)) == NULL) + errx(EXIT_FAILURE, "elf_newscn failed: %s", + elf_errmsg(-1)); + if ((newndx = elf_ndxscn(s->os)) == SHN_UNDEF) + errx(EXIT_FAILURE, "elf_ndxscn failed: %s", + elf_errmsg(-1)); + } + if ((oldndx = elf_ndxscn(is)) == SHN_UNDEF) + errx(EXIT_FAILURE, "elf_ndxscn failed: %s", + elf_errmsg(-1)); + if (oldndx != SHN_UNDEF && newndx != SHN_UNDEF) + ecp->secndx[oldndx] = newndx; + + /* + * 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. + */ + if (ecp->strip == STRIP_NONDEBUG && (ish.sh_flags & SHF_ALLOC)) + s->type = SHT_NOBITS; + + check_section_rename(ecp, s); + + /* create section header based on input object. */ + if (strcmp(name, ".symtab") != 0 && + strcmp(name, ".strtab") != 0 && + strcmp(name, ".shstrtab") != 0) + copy_shdr(ecp, s, NULL, 0, sec_flags); + + if (strcmp(name, ".symtab") == 0) { + ecp->flags |= SYMTAB_EXIST; + ecp->symtab = s; + } + if (strcmp(name, ".strtab") == 0) + ecp->strtab = s; + + insert_to_sec_list(ecp, s, 0); + } + elferr = elf_errno(); + if (elferr != 0) + errx(EXIT_FAILURE, "elf_nextscn failed: %s", + elf_errmsg(elferr)); +} + +struct section * +insert_shtab(struct elfcopy *ecp, int tail) +{ + struct section *s, *shtab; + GElf_Ehdr ieh; + int nsecs; + + /* + * Treat section header table as a "pseudo" section, insert it + * into section list, so later it will get sorted and resynced + * just as normal sections. + */ + if ((shtab = calloc(1, sizeof(*shtab))) == NULL) + errx(EXIT_FAILURE, "calloc failed"); + if (!tail) { + /* + * "shoff" of input object is used as a hint for section + * resync later. + */ + if (gelf_getehdr(ecp->ein, &ieh) == NULL) + errx(EXIT_FAILURE, "gelf_getehdr() failed: %s", + elf_errmsg(-1)); + shtab->off = ieh.e_shoff; + } else + shtab->off = 0; + /* Calculate number of sections in the output object. */ + nsecs = 0; + TAILQ_FOREACH(s, &ecp->v_sec, sec_list) { + if (!s->pseudo) + nsecs++; + } + /* Remember there is always a null section, so we +1 here. */ + shtab->sz = gelf_fsize(ecp->eout, ELF_T_SHDR, nsecs + 1, EV_CURRENT); + if (shtab->sz == 0) + errx(EXIT_FAILURE, "gelf_fsize() failed: %s", elf_errmsg(-1)); + shtab->align = (ecp->oec == ELFCLASS32 ? 4 : 8); + shtab->loadable = 0; + shtab->pseudo = 1; + insert_to_sec_list(ecp, shtab, tail); + + return (shtab); +} + +void +copy_content(struct elfcopy *ecp) +{ + struct section *s; + + TAILQ_FOREACH(s, &ecp->v_sec, sec_list) { + /* Skip pseudo section. */ + if (s->pseudo) + continue; + + /* Skip special sections. */ + if (strcmp(s->name, ".symtab") == 0 || + strcmp(s->name, ".strtab") == 0 || + strcmp(s->name, ".shstrtab") == 0) + continue; + + /* + * If strip action is STRIP_ALL, relocation info need + * to be stripped. Skip filtering otherwisw. + */ + if (ecp->strip == STRIP_ALL && + (s->type == SHT_REL || s->type == SHT_RELA)) + filter_reloc(ecp, s); + + if (is_modify_section(ecp, s->name)) + modify_section(ecp, s); + + copy_data(s); + + /* + * If symbol table is modified, relocation info might + * need update, as symbol index may have changed. + */ + if ((ecp->flags & SYMTAB_INTACT) == 0 && + (ecp->flags & SYMTAB_EXIST) && + (s->type == SHT_REL || s->type == SHT_RELA)) + update_reloc(ecp, s); + + if (is_print_section(ecp, s->name)) + print_section(s); + } +} + +/* + * Filter relocation entries, only keep those entries whose + * symbol is in the keep list. + */ +static void +filter_reloc(struct elfcopy *ecp, struct section *s) +{ + const char *name; + GElf_Shdr ish; + GElf_Rel rel; + GElf_Rela rela; + Elf32_Rel *rel32; + Elf64_Rel *rel64; + Elf32_Rela *rela32; + Elf64_Rela *rela64; + Elf_Data *id; + uint64_t cap, n, nrels; + int elferr, i; + + if (gelf_getshdr(s->is, &ish) == NULL) + errx(EXIT_FAILURE, "gelf_getehdr() failed: %s", + elf_errmsg(-1)); + + /* We don't want to touch relocation info for dynamic symbols. */ + if ((ecp->flags & SYMTAB_EXIST) == 0) { + if (ish.sh_link == 0 || ecp->secndx[ish.sh_link] == 0) { + /* + * This reloc section applies to the symbol table + * that was stripped, so discard whole section. + */ + s->nocopy = 1; + s->sz = 0; + } + return; + } else { + /* Symbol table exist, check if index equals. */ + if (ish.sh_link != elf_ndxscn(ecp->symtab->is)) + return; + } + +#define COPYREL(REL, SZ) do { \ + if (nrels == 0) { \ + if ((REL##SZ = malloc(cap * \ + sizeof(Elf##SZ##_Rel))) == NULL) \ + err(EXIT_FAILURE, "malloc failed"); \ + } \ + if (nrels >= cap) { \ + cap *= 2; \ + if ((REL##SZ = realloc(REL##SZ, cap * \ + sizeof(Elf##SZ##_Rel))) == NULL) \ + err(EXIT_FAILURE, "realloc failed"); \ + } \ + REL##SZ[nrels].r_offset = REL.r_offset; \ + REL##SZ[nrels].r_info = REL.r_info; \ + if (s->type == SHT_RELA) \ + rela##SZ[nrels].r_addend = rela.r_addend; \ + nrels++; \ +} while (0) + + nrels = 0; + cap = 4; /* keep list is usually small. */ + rel32 = NULL; + rel64 = NULL; + rela32 = NULL; + rela64 = NULL; + if ((id = elf_getdata(s->is, NULL)) == NULL) + errx(EXIT_FAILURE, "elf_getdata() failed: %s", + elf_errmsg(-1)); + n = ish.sh_size / ish.sh_entsize; + for(i = 0; (uint64_t)i < n; i++) { + if (s->type == SHT_REL) { + if (gelf_getrel(id, i, &rel) != &rel) + errx(EXIT_FAILURE, "gelf_getrel failed: %s", + elf_errmsg(-1)); + } else { + if (gelf_getrela(id, i, &rela) != &rela) + errx(EXIT_FAILURE, "gelf_getrel failed: %s", + elf_errmsg(-1)); + } + name = elf_strptr(ecp->ein, elf_ndxscn(ecp->strtab->is), + GELF_R_SYM(rel.r_info)); + if (name == NULL) + errx(EXIT_FAILURE, "elf_strptr failed: %s", + elf_errmsg(-1)); + if (lookup_symop_list(ecp, name, SYMOP_KEEP) != NULL) { + if (ecp->oec == ELFCLASS32) { + if (s->type == SHT_REL) + COPYREL(rel, 32); + else + COPYREL(rela, 32); + } else { + if (s->type == SHT_REL) + COPYREL(rel, 64); + else + COPYREL(rela, 64); + } + } + } + elferr = elf_errno(); + if (elferr != 0) + errx(EXIT_FAILURE, "elf_getdata() failed: %s", + elf_errmsg(elferr)); + + if (ecp->oec == ELFCLASS32) { + if (s->type == SHT_REL) + s->buf = rel32; + else + s->buf = rela32; + } else { + if (s->type == SHT_REL) + s->buf = rel64; + else + s->buf = rela64; + } + s->sz = gelf_fsize(ecp->eout, (s->type == SHT_REL ? ELF_T_REL : + ELF_T_RELA), nrels, EV_CURRENT); + s->nocopy = 1; +} + +static void +update_reloc(struct elfcopy *ecp, struct section *s) +{ + GElf_Shdr osh; + GElf_Rel rel; + GElf_Rela rela; + Elf_Data *od; + uint64_t n; + int i; + +#define UPDATEREL(REL) do { \ + if (gelf_get##REL(od, i, &REL) != &REL) \ + errx(EXIT_FAILURE, "gelf_get##REL failed: %s", \ + elf_errmsg(-1)); \ + REL.r_info = GELF_R_INFO(ecp->symndx[GELF_R_SYM(REL.r_info)], \ + GELF_R_TYPE(REL.r_info)); \ + if (!gelf_update_##REL(od, i, &REL)) \ + errx(EXIT_FAILURE, "gelf_update_##REL failed: %s", \ + elf_errmsg(-1)); \ +} while(0) + + if (s->sz == 0) + return; + if (gelf_getshdr(s->os, &osh) == NULL) + errx(EXIT_FAILURE, "gelf_getehdr() failed: %s", + elf_errmsg(-1)); + /* Only process .symtab reloc info. */ + if (osh.sh_link != elf_ndxscn(ecp->symtab->is)) + return; + if ((od = elf_getdata(s->os, NULL)) == NULL) + errx(EXIT_FAILURE, "elf_getdata() failed: %s", + elf_errmsg(-1)); + n = osh.sh_size / osh.sh_entsize; + for(i = 0; (uint64_t)i < n; i++) { + if (s->type == SHT_REL) + UPDATEREL(rel); + else + UPDATEREL(rela); + } +} + +static void +pad_section(struct elfcopy *ecp, struct section *s) +{ + GElf_Shdr osh; + Elf_Data *od; + + if (s == NULL || s->pad_sz == 0) + return; + + if ((s->pad = malloc(s->pad_sz)) == NULL) + err(EXIT_FAILURE, "malloc failed"); + memset(s->pad, ecp->fill, s->pad_sz); + + /* Create a new Elf_Data to contain the padding bytes. */ + if ((od = elf_newdata(s->os)) == NULL) + errx(EXIT_FAILURE, "elf_newdata() failed: %s", + elf_errmsg(-1)); + od->d_align = 1; + od->d_off = s->sz; + od->d_buf = s->pad; + od->d_type = ELF_T_BYTE; + od->d_size = s->pad_sz; + od->d_version = EV_CURRENT; + + /* Update section header. */ + if (gelf_getshdr(s->os, &osh) == NULL) + errx(EXIT_FAILURE, "gelf_getshdr() failed: %s", + elf_errmsg(-1)); + osh.sh_size = s->sz + s->pad_sz; + if (!gelf_update_shdr(s->os, &osh)) + errx(EXIT_FAILURE, "elf_update_shdr failed: %s", + elf_errmsg(-1)); +} + +void +resync_sections(struct elfcopy *ecp) +{ + struct section *s, *ps; + GElf_Shdr osh; + uint64_t off; + int first; + + ps = NULL; + first = 1; + off = 0; + TAILQ_FOREACH(s, &ecp->v_sec, sec_list) { + if (first) { + off = s->off; + first = 0; + } + + /* + * Ignore TLS sections with load address 0 and without + * content. We don't need to adjust their file offset or + * VMA, only the size matters. + */ + if (s->seg_tls != NULL && s->type == SHT_NOBITS && + s->off == 0) + continue; + + /* Align section offset. */ + if (s->align == 0) + s->align = 1; + if (off <= s->off) { + if (!s->loadable) + s->off = roundup(off, s->align); + } else { + if (s->loadable) + warnx("moving loadable section %s, " + "is this intentional?", s->name); + s->off = roundup(off, s->align); + } + + /* Calculate next section offset. */ + off = s->off; + if (s->pseudo || (s->type != SHT_NOBITS && s->type != SHT_NULL)) + off += s->sz; + + if (s->pseudo) { + ps = NULL; + continue; + } + + /* Count padding bytes added through --pad-to. */ + if (s->pad_sz > 0) + off += s->pad_sz; + + /* Update section header accordingly. */ + if (gelf_getshdr(s->os, &osh) == NULL) + errx(EXIT_FAILURE, "gelf_getshdr() failed: %s", + elf_errmsg(-1)); + osh.sh_addr = s->vma; + osh.sh_offset = s->off; + osh.sh_size = s->sz; + if (!gelf_update_shdr(s->os, &osh)) + errx(EXIT_FAILURE, "elf_update_shdr failed: %s", + elf_errmsg(-1)); + + /* Add padding for previous section, if need. */ + if (ps != NULL) { + if (ps->pad_sz > 0) { + /* Apply padding added by --pad-to. */ + pad_section(ecp, ps); + } else if ((ecp->flags & GAP_FILL) && + (ps->off + ps->sz < s->off)) { + /* + * Fill the gap between sections by padding + * the section with lower address. + */ + ps->pad_sz = s->off - (ps->off + ps->sz); + pad_section(ecp, ps); + } + } + + ps = s; + } + + /* Pad the last section, if need. */ + if (ps != NULL && ps->pad_sz > 0) + pad_section(ecp, ps); +} + +static void +modify_section(struct elfcopy *ecp, struct section *s) +{ + struct sec_action *sac; + size_t srcsz, dstsz, p, len; + char *b, *c, *d, *src, *end; + int dupe; + + src = read_section(s, &srcsz); + if (src == NULL || srcsz == 0) { + /* For empty section, we proceed if we need to append. */ + if (!is_append_section(ecp, s->name)) + return; + } + + /* Allocate buffer needed for new section data. */ + dstsz = srcsz; + if (is_append_section(ecp, s->name)) { + sac = lookup_sec_act(ecp, s->name, 0); + dstsz += strlen(sac->string) + 1; + } + if ((b = malloc(dstsz)) == NULL) + err(EXIT_FAILURE, "malloc failed"); + s->buf = b; + + /* Compress section. */ + p = 0; + if (is_compress_section(ecp, s->name)) { + end = src + srcsz; + for(c = src; c < end;) { + len = 0; + while(c + len < end && c[len] != '\0') + len++; + if (c + len == end) { + /* XXX should we warn here? */ + strncpy(&b[p], c, len); + p += len; + break; + } + dupe = 0; + for (d = b; d < b + p; ) { + if (strcmp(d, c) == 0) { + dupe = 1; + break; + } + d += strlen(d) + 1; + } + if (!dupe) { + strncpy(&b[p], c, len); + b[p + len] = '\0'; + p += len + 1; + } + c += len + 1; + } + } else { + memcpy(b, src, srcsz); + p += srcsz; + } + + /* Append section. */ + if (is_append_section(ecp, s->name)) { + sac = lookup_sec_act(ecp, s->name, 0); + len = strlen(sac->string); + strncpy(&b[p], sac->string, len); + b[p + len] = '\0'; + p += len + 1; + } + + s->sz = p; + s->nocopy = 1; +} + +static void +print_data(const char *d, size_t sz) +{ + const char *c; + + for (c = d; c < d + sz; c++) { + if (*c == '\0') + putchar('\n'); + else + putchar(*c); + } +} + +static void +print_section(struct section *s) +{ + Elf_Data *id; + int elferr; + + if (s->buf != NULL && s->sz > 0) { + print_data(s->buf, s->sz); + } else { + id = NULL; + while ((id = elf_getdata(s->is, id)) != NULL) + print_data(id->d_buf, id->d_size); + elferr = elf_errno(); + if (elferr != 0) + errx(EXIT_FAILURE, "elf_getdata() failed: %s", + elf_errmsg(elferr)); + } + putchar('\n'); +} + +static void * +read_section(struct section *s, size_t *size) +{ + Elf_Data *id; + char *b; + size_t sz; + int elferr; + + sz = 0; + b = NULL; + id = NULL; + while ((id = elf_getdata(s->is, id)) != NULL) { + if (b == NULL) + b = malloc(id->d_size); + else + b = malloc(sz + id->d_size); + if (b == NULL) + err(EXIT_FAILURE, "malloc or realloc failed"); + + memcpy(&b[sz], id->d_buf, id->d_size); + sz += id->d_size; + } + elferr = elf_errno(); + if (elferr != 0) + errx(EXIT_FAILURE, "elf_getdata() failed: %s", + elf_errmsg(elferr)); + + *size = sz; + + return (b); +} + +void +copy_shdr(struct elfcopy *ecp, struct section *s, const char *name, int copy, + int sec_flags) +{ + GElf_Shdr ish, osh; + + if (gelf_getshdr(s->is, &ish) == NULL) + errx(EXIT_FAILURE, "526 gelf_getshdr() failed: %s", + elf_errmsg(-1)); + if (gelf_getshdr(s->os, &osh) == NULL) + errx(EXIT_FAILURE, "529 gelf_getshdr() failed: %s", + elf_errmsg(-1)); + + if (copy) + (void) memcpy(&osh, &ish, sizeof(ish)); + else { + osh.sh_type = s->type; + osh.sh_addr = s->vma; + osh.sh_offset = s->off; + osh.sh_size = s->sz; + osh.sh_link = ish.sh_link; + osh.sh_info = ish.sh_info; + osh.sh_addralign = s->align; + osh.sh_entsize = ish.sh_entsize; + + if (sec_flags) { + osh.sh_flags = 0; + if (sec_flags & SF_ALLOC) { + osh.sh_flags |= SHF_ALLOC; + if (!s->loadable) + warnx("set SHF_ALLOC flag for " + "unloadable section %s", + s->name); + } + if ((sec_flags & SF_READONLY) == 0) + osh.sh_flags |= SHF_WRITE; + if (sec_flags & SF_CODE) + osh.sh_flags |= SHF_EXECINSTR; + } else + osh.sh_flags = ish.sh_flags; + } + + if (name == NULL) + add_to_shstrtab(ecp, s->name); + else + add_to_shstrtab(ecp, name); + + if (!gelf_update_shdr(s->os, &osh)) + errx(EXIT_FAILURE, "elf_update_shdr failed: %s", + elf_errmsg(-1)); +} + +void +copy_data(struct section *s) +{ + Elf_Data *id, *od; + int elferr; + + if (s->nocopy && s->buf == NULL) + return; + + if ((id = elf_getdata(s->is, NULL)) == NULL) { + elferr = elf_errno(); + if (elferr != 0) + errx(EXIT_FAILURE, "elf_getdata() failed: %s", + elf_errmsg(elferr)); + return; + } + + if ((od = elf_newdata(s->os)) == NULL) + errx(EXIT_FAILURE, "elf_newdata() failed: %s", + elf_errmsg(-1)); + + if (s->nocopy) { + /* Use s->buf as content if s->nocopy is set. */ + od->d_align = id->d_align; + od->d_off = 0; + od->d_buf = s->buf; + od->d_type = id->d_type; + od->d_size = s->sz; + od->d_version = id->d_version; + } else { + od->d_align = id->d_align; + od->d_off = id->d_off; + od->d_buf = id->d_buf; + od->d_type = id->d_type; + od->d_size = id->d_size; + od->d_version = id->d_version; + } + + /* + * Alignment Fixup. libelf does not allow the alignment for + * Elf_Data descriptor to be set to 0. In this case we workaround + * it by setting the alignment to 1. + * + * According to the ELF ABI, alignment 0 and 1 has the same + * meaning: the section has no alignment constraints. + */ + if (od->d_align == 0) + od->d_align = 1; +} + +struct section * +create_external_section(struct elfcopy *ecp, const char *name, char *newname, + void *buf, uint64_t size, uint64_t off, uint64_t stype, Elf_Type dtype, + uint64_t flags, uint64_t align, uint64_t vma, int loadable) +{ + struct section *s; + Elf_Scn *os; + Elf_Data *od; + GElf_Shdr osh; + + if ((os = elf_newscn(ecp->eout)) == NULL) + errx(EXIT_FAILURE, "elf_newscn() failed: %s", + elf_errmsg(-1)); + if ((s = calloc(1, sizeof(*s))) == NULL) + err(EXIT_FAILURE, "calloc failed"); + s->name = name; + s->newname = newname; /* needs to be free()'ed */ + s->off = off; + s->sz = size; + s->vma = vma; + s->align = align; + s->loadable = loadable; + s->is = NULL; + s->os = os; + s->type = stype; + s->nocopy = 1; + insert_to_sec_list(ecp, s, 1); + + if (gelf_getshdr(os, &osh) == NULL) + errx(EXIT_FAILURE, "gelf_getshdr() failed: %s", + elf_errmsg(-1)); + osh.sh_flags = flags; + osh.sh_type = s->type; + osh.sh_addr = s->vma; + osh.sh_addralign = s->align; + if (!gelf_update_shdr(os, &osh)) + errx(EXIT_FAILURE, "gelf_update_shdr() failed: %s", + elf_errmsg(-1)); + add_to_shstrtab(ecp, name); + + if (buf != NULL && size != 0) { + if ((od = elf_newdata(os)) == NULL) + errx(EXIT_FAILURE, "elf_newdata() failed: %s", + elf_errmsg(-1)); + od->d_align = align; + od->d_off = 0; + od->d_buf = buf; + od->d_size = size; + od->d_type = dtype; + od->d_version = EV_CURRENT; + } + + /* + * Clear SYMTAB_INTACT, as we probably need to update/add new + * STT_SECTION symbols into the symbol table. + */ + ecp->flags &= ~SYMTAB_INTACT; + + return (s); +} + +/* + * Insert sections specified by --add-section to the end of section list. + */ +static void +insert_sections(struct elfcopy *ecp) +{ + struct sec_add *sa; + struct section *s; + size_t off; + + /* Put these sections in the end of current list. */ + off = 0; + TAILQ_FOREACH(s, &ecp->v_sec, sec_list) { + if (s->type != SHT_NOBITS && s->type != SHT_NULL) + off = s->off + s->sz; + else + off = s->off; + } + + STAILQ_FOREACH(sa, &ecp->v_sadd, sadd_list) { + + /* TODO: Add section header vma/lma, flag changes here */ + + (void) create_external_section(ecp, sa->name, NULL, sa->content, + sa->size, off, SHT_PROGBITS, ELF_T_BYTE, 0, 1, 0, 0); + } +} + +void +add_to_shstrtab(struct elfcopy *ecp, const char *name) +{ + struct section *s; + + s = ecp->shstrtab; + insert_to_strtab(s, name); +} + +void +update_shdr(struct elfcopy *ecp, int update_link) +{ + struct section *s; + GElf_Shdr osh; + int elferr; + + TAILQ_FOREACH(s, &ecp->v_sec, sec_list) { + if (s->pseudo) + continue; + + if (gelf_getshdr(s->os, &osh) == NULL) + errx(EXIT_FAILURE, "668 gelf_getshdr failed: %s", + elf_errmsg(-1)); + + /* Find section name in string table and set sh_name. */ + osh.sh_name = lookup_string(ecp->shstrtab, s->name); + + /* + * sh_link needs to be updated, since the index of the + * linked section might have changed. + */ + if (update_link && osh.sh_link != 0) + osh.sh_link = ecp->secndx[osh.sh_link]; + + /* + * sh_info of relocation section links to the section to which + * its relocation info applies. So it may need update as well. + */ + if ((s->type == SHT_REL || s->type == SHT_RELA) && + osh.sh_info != 0) + osh.sh_info = ecp->secndx[osh.sh_info]; + + /* + * sh_info of SHT_GROUP section needs to point to the correct + * string in the symbol table. + */ + if (s->type == SHT_GROUP && (ecp->flags & SYMTAB_EXIST) && + (ecp->flags & SYMTAB_INTACT) == 0) + osh.sh_info = ecp->symndx[osh.sh_info]; + + if (!gelf_update_shdr(s->os, &osh)) + errx(EXIT_FAILURE, "gelf_update_shdr() failed: %s", + elf_errmsg(-1)); + } + elferr = elf_errno(); + if (elferr != 0) + errx(EXIT_FAILURE, "elf_nextscn failed: %s", + elf_errmsg(elferr)); +} + +void +init_shstrtab(struct elfcopy *ecp) +{ + struct section *s; + + if ((ecp->shstrtab = calloc(1, sizeof(*ecp->shstrtab))) == NULL) + err(EXIT_FAILURE, "calloc failed"); + s = ecp->shstrtab; + s->name = ".shstrtab"; + s->is = NULL; + s->sz = 0; + s->align = 1; + s->loadable = 0; + s->type = SHT_STRTAB; + s->vma = 0; + + insert_to_strtab(s, ""); + insert_to_strtab(s, ".symtab"); + insert_to_strtab(s, ".strtab"); + insert_to_strtab(s, ".shstrtab"); +} + +void +set_shstrtab(struct elfcopy *ecp) +{ + struct section *s; + Elf_Data *data; + GElf_Shdr sh; + + s = ecp->shstrtab; + + if (gelf_getshdr(s->os, &sh) == NULL) + errx(EXIT_FAILURE, "692 gelf_getshdr() failed: %s", + elf_errmsg(-1)); + sh.sh_addr = 0; + sh.sh_addralign = 1; + sh.sh_offset = s->off; + sh.sh_type = SHT_STRTAB; + sh.sh_flags = 0; + sh.sh_entsize = 0; + sh.sh_info = 0; + sh.sh_link = 0; + + if ((data = elf_newdata(s->os)) == NULL) + errx(EXIT_FAILURE, "elf_newdata() failed: %s", + elf_errmsg(-1)); + + /* + * If we don't have a symbol table, skip those a few bytes + * which are reserved for this in the beginning of shstrtab. + */ + if (!(ecp->flags & SYMTAB_EXIST)) { + s->sz -= sizeof(".symtab\0.strtab"); + memmove(s->buf, (char *)s->buf + sizeof(".symtab\0.strtab"), + s->sz); + } + + sh.sh_size = s->sz; + if (!gelf_update_shdr(s->os, &sh)) + errx(EXIT_FAILURE, "gelf_update_shdr() failed: %s", + elf_errmsg(-1)); + + data->d_align = 1; + data->d_buf = s->buf; + data->d_size = s->sz; + data->d_off = 0; + data->d_type = ELF_T_BYTE; + data->d_version = EV_CURRENT; + + if (!elf_setshstrndx(ecp->eout, elf_ndxscn(s->os))) + errx(EXIT_FAILURE, "elf_setshstrndx() failed: %s", + elf_errmsg(-1)); +} + +void +add_section(struct elfcopy *ecp, const char *arg) +{ + struct sec_add *sa; + struct stat sb; + const char *s, *fn; + FILE *fp; + int len; + + if ((s = strchr(arg, '=')) == NULL) + errx(EXIT_FAILURE, + "illegal format for --add-section option"); + if ((sa = malloc(sizeof(*sa))) == NULL) + err(EXIT_FAILURE, "malloc failed"); + + len = s - arg; + if ((sa->name = malloc(len + 1)) == NULL) + err(EXIT_FAILURE, "malloc failed"); + strncpy(sa->name, arg, len); + sa->name[len] = '\0'; + + fn = s + 1; + if (stat(fn, &sb) == -1) + err(EXIT_FAILURE, "stat failed"); + sa->size = sb.st_size; + if ((sa->content = malloc(sa->size)) == NULL) + err(EXIT_FAILURE, "malloc failed"); + if ((fp = fopen(fn, "r")) == NULL) + err(EXIT_FAILURE, "can not open %s", fn); + if (fread(sa->content, 1, sa->size, fp) == 0 || + ferror(fp)) + err(EXIT_FAILURE, "fread failed"); + fclose(fp); + + STAILQ_INSERT_TAIL(&ecp->v_sadd, sa, sadd_list); + ecp->flags |= SEC_ADD; +} + +void +free_sec_add(struct elfcopy *ecp) +{ + struct sec_add *sa, *sa_temp; + + STAILQ_FOREACH_SAFE(sa, &ecp->v_sadd, sadd_list, sa_temp) { + STAILQ_REMOVE(&ecp->v_sadd, sa, sec_add, sadd_list); + free(sa->name); + free(sa->content); + free(sa); + } +} + +static void +add_gnu_debuglink(struct elfcopy *ecp) +{ + struct sec_add *sa; + struct stat sb; + FILE *fp; + char *fnbase, *buf; + int crc_off; + int crc; + + if (ecp->debuglink == NULL) + return; + + /* Read debug file content. */ + if ((sa = malloc(sizeof(*sa))) == NULL) + err(EXIT_FAILURE, "malloc failed"); + if ((sa->name = strdup(".gnu_debuglink")) == NULL) + err(EXIT_FAILURE, "strdup failed"); + if (stat(ecp->debuglink, &sb) == -1) + err(EXIT_FAILURE, "stat failed"); + if ((buf = malloc(sb.st_size)) == NULL) + err(EXIT_FAILURE, "malloc failed"); + if ((fp = fopen(ecp->debuglink, "r")) == NULL) + err(EXIT_FAILURE, "can not open %s", ecp->debuglink); + if (fread(buf, 1, sb.st_size, fp) == 0 || + ferror(fp)) + err(EXIT_FAILURE, "fread failed"); + fclose(fp); + + /* Calculate crc checksum. */ + crc = calc_crc32(buf, sb.st_size, 0xFFFFFFFF); + free(buf); + + /* Calculate section size and the offset to store crc checksum. */ + if ((fnbase = basename(ecp->debuglink)) == NULL) + err(EXIT_FAILURE, "basename failed"); + crc_off = roundup(strlen(fnbase) + 1, 4); + sa->size = crc_off + 4; + + /* Section content. */ + if ((sa->content = calloc(1, sa->size)) == NULL) + err(EXIT_FAILURE, "malloc failed"); + strncpy(sa->content, fnbase, strlen(fnbase)); + if (ecp->oed == ELFDATA2LSB) { + sa->content[crc_off] = crc & 0xFF; + sa->content[crc_off + 1] = (crc >> 8) & 0xFF; + sa->content[crc_off + 2] = (crc >> 16) & 0xFF; + sa->content[crc_off + 3] = crc >> 24; + } else { + sa->content[crc_off] = crc >> 24; + sa->content[crc_off + 1] = (crc >> 16) & 0xFF; + sa->content[crc_off + 2] = (crc >> 8) & 0xFF; + sa->content[crc_off + 3] = crc & 0xFF; + } + + STAILQ_INSERT_TAIL(&ecp->v_sadd, sa, sadd_list); + ecp->flags |= SEC_ADD; +} + +static void +insert_to_strtab(struct section *t, const char *s) +{ + const char *r; + char *b, *c; + size_t len, slen; + int append; + + if (t->sz == 0) { + t->cap = 512; + if ((t->buf = malloc(t->cap)) == NULL) + err(EXIT_FAILURE, "malloc failed"); + } + + slen = strlen(s); + append = 0; + b = t->buf; + for (c = b; c < b + t->sz;) { + len = strlen(c); + if (!append && len >= slen) { + r = c + (len - slen); + if (strcmp(r, s) == 0) + return; + } else if (len < slen && len != 0) { + r = s + (slen - len); + if (strcmp(c, r) == 0) { + t->sz -= len + 1; + memmove(c, c + len + 1, t->sz - (c - b)); + append = 1; + continue; + } + } + c += len + 1; + } + + while (t->sz + slen + 1 >= t->cap) { + t->cap *= 2; + if ((t->buf = realloc(t->buf, t->cap)) == NULL) + err(EXIT_FAILURE, "realloc failed"); + } + b = t->buf; + strncpy(&b[t->sz], s, slen); + b[t->sz + slen] = '\0'; + t->sz += slen + 1; +} + +static int +lookup_string(struct section *t, const char *s) +{ + const char *b, *c, *r; + size_t len, slen; + + slen = strlen(s); + b = t->buf; + for (c = b; c < b + t->sz;) { + len = strlen(c); + if (len >= slen) { + r = c + (len - slen); + if (strcmp(r, s) == 0) + return (r - b); + } + c += len + 1; + } + + return (-1); +} + +static uint32_t crctable[256] = +{ + 0x00000000L, 0x77073096L, 0xEE0E612CL, 0x990951BAL, + 0x076DC419L, 0x706AF48FL, 0xE963A535L, 0x9E6495A3L, + 0x0EDB8832L, 0x79DCB8A4L, 0xE0D5E91EL, 0x97D2D988L, + 0x09B64C2BL, 0x7EB17CBDL, 0xE7B82D07L, 0x90BF1D91L, + 0x1DB71064L, 0x6AB020F2L, 0xF3B97148L, 0x84BE41DEL, + 0x1ADAD47DL, 0x6DDDE4EBL, 0xF4D4B551L, 0x83D385C7L, + 0x136C9856L, 0x646BA8C0L, 0xFD62F97AL, 0x8A65C9ECL, + 0x14015C4FL, 0x63066CD9L, 0xFA0F3D63L, 0x8D080DF5L, + 0x3B6E20C8L, 0x4C69105EL, 0xD56041E4L, 0xA2677172L, + 0x3C03E4D1L, 0x4B04D447L, 0xD20D85FDL, 0xA50AB56BL, + 0x35B5A8FAL, 0x42B2986CL, 0xDBBBC9D6L, 0xACBCF940L, + 0x32D86CE3L, 0x45DF5C75L, 0xDCD60DCFL, 0xABD13D59L, + 0x26D930ACL, 0x51DE003AL, 0xC8D75180L, 0xBFD06116L, + 0x21B4F4B5L, 0x56B3C423L, 0xCFBA9599L, 0xB8BDA50FL, + 0x2802B89EL, 0x5F058808L, 0xC60CD9B2L, 0xB10BE924L, + 0x2F6F7C87L, 0x58684C11L, 0xC1611DABL, 0xB6662D3DL, + 0x76DC4190L, 0x01DB7106L, 0x98D220BCL, 0xEFD5102AL, + 0x71B18589L, 0x06B6B51FL, 0x9FBFE4A5L, 0xE8B8D433L, + 0x7807C9A2L, 0x0F00F934L, 0x9609A88EL, 0xE10E9818L, + 0x7F6A0DBBL, 0x086D3D2DL, 0x91646C97L, 0xE6635C01L, + 0x6B6B51F4L, 0x1C6C6162L, 0x856530D8L, 0xF262004EL, + 0x6C0695EDL, 0x1B01A57BL, 0x8208F4C1L, 0xF50FC457L, + 0x65B0D9C6L, 0x12B7E950L, 0x8BBEB8EAL, 0xFCB9887CL, + 0x62DD1DDFL, 0x15DA2D49L, 0x8CD37CF3L, 0xFBD44C65L, + 0x4DB26158L, 0x3AB551CEL, 0xA3BC0074L, 0xD4BB30E2L, + 0x4ADFA541L, 0x3DD895D7L, 0xA4D1C46DL, 0xD3D6F4FBL, + 0x4369E96AL, 0x346ED9FCL, 0xAD678846L, 0xDA60B8D0L, + 0x44042D73L, 0x33031DE5L, 0xAA0A4C5FL, 0xDD0D7CC9L, + 0x5005713CL, 0x270241AAL, 0xBE0B1010L, 0xC90C2086L, + 0x5768B525L, 0x206F85B3L, 0xB966D409L, 0xCE61E49FL, + 0x5EDEF90EL, 0x29D9C998L, 0xB0D09822L, 0xC7D7A8B4L, + 0x59B33D17L, 0x2EB40D81L, 0xB7BD5C3BL, 0xC0BA6CADL, + 0xEDB88320L, 0x9ABFB3B6L, 0x03B6E20CL, 0x74B1D29AL, + 0xEAD54739L, 0x9DD277AFL, 0x04DB2615L, 0x73DC1683L, + 0xE3630B12L, 0x94643B84L, 0x0D6D6A3EL, 0x7A6A5AA8L, + 0xE40ECF0BL, 0x9309FF9DL, 0x0A00AE27L, 0x7D079EB1L, + 0xF00F9344L, 0x8708A3D2L, 0x1E01F268L, 0x6906C2FEL, + 0xF762575DL, 0x806567CBL, 0x196C3671L, 0x6E6B06E7L, + 0xFED41B76L, 0x89D32BE0L, 0x10DA7A5AL, 0x67DD4ACCL, + 0xF9B9DF6FL, 0x8EBEEFF9L, 0x17B7BE43L, 0x60B08ED5L, + 0xD6D6A3E8L, 0xA1D1937EL, 0x38D8C2C4L, 0x4FDFF252L, + 0xD1BB67F1L, 0xA6BC5767L, 0x3FB506DDL, 0x48B2364BL, + 0xD80D2BDAL, 0xAF0A1B4CL, 0x36034AF6L, 0x41047A60L, + 0xDF60EFC3L, 0xA867DF55L, 0x316E8EEFL, 0x4669BE79L, + 0xCB61B38CL, 0xBC66831AL, 0x256FD2A0L, 0x5268E236L, + 0xCC0C7795L, 0xBB0B4703L, 0x220216B9L, 0x5505262FL, + 0xC5BA3BBEL, 0xB2BD0B28L, 0x2BB45A92L, 0x5CB36A04L, + 0xC2D7FFA7L, 0xB5D0CF31L, 0x2CD99E8BL, 0x5BDEAE1DL, + 0x9B64C2B0L, 0xEC63F226L, 0x756AA39CL, 0x026D930AL, + 0x9C0906A9L, 0xEB0E363FL, 0x72076785L, 0x05005713L, + 0x95BF4A82L, 0xE2B87A14L, 0x7BB12BAEL, 0x0CB61B38L, + 0x92D28E9BL, 0xE5D5BE0DL, 0x7CDCEFB7L, 0x0BDBDF21L, + 0x86D3D2D4L, 0xF1D4E242L, 0x68DDB3F8L, 0x1FDA836EL, + 0x81BE16CDL, 0xF6B9265BL, 0x6FB077E1L, 0x18B74777L, + 0x88085AE6L, 0xFF0F6A70L, 0x66063BCAL, 0x11010B5CL, + 0x8F659EFFL, 0xF862AE69L, 0x616BFFD3L, 0x166CCF45L, + 0xA00AE278L, 0xD70DD2EEL, 0x4E048354L, 0x3903B3C2L, + 0xA7672661L, 0xD06016F7L, 0x4969474DL, 0x3E6E77DBL, + 0xAED16A4AL, 0xD9D65ADCL, 0x40DF0B66L, 0x37D83BF0L, + 0xA9BCAE53L, 0xDEBB9EC5L, 0x47B2CF7FL, 0x30B5FFE9L, + 0xBDBDF21CL, 0xCABAC28AL, 0x53B39330L, 0x24B4A3A6L, + 0xBAD03605L, 0xCDD70693L, 0x54DE5729L, 0x23D967BFL, + 0xB3667A2EL, 0xC4614AB8L, 0x5D681B02L, 0x2A6F2B94L, + 0xB40BBE37L, 0xC30C8EA1L, 0x5A05DF1BL, 0x2D02EF8DL +}; + +static uint32_t +calc_crc32(const char *p, size_t len, uint32_t crc) +{ + uint32_t i; + + for (i = 0; i < len; i++) { + crc = crctable[(crc ^ *p++) & 0xFFL] ^ (crc >> 8); + } + + return (crc ^ 0xFFFFFFFF); +} diff --git a/contrib/elftoolchain/elfcopy/segments.c b/contrib/elftoolchain/elfcopy/segments.c new file mode 100644 index 000000000000..1e271a6f4347 --- /dev/null +++ b/contrib/elftoolchain/elfcopy/segments.c @@ -0,0 +1,495 @@ +/*- + * Copyright (c) 2007-2010,2012 Kai Wang + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <sys/queue.h> +#include <err.h> +#include <gelf.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "elfcopy.h" + +ELFTC_VCSID("$Id: segments.c 3196 2015-05-12 17:33:48Z emaste $"); + +static void insert_to_inseg_list(struct segment *seg, struct section *sec); + +/* + * elfcopy's segment handling is relatively simpler and less powerful than + * libbfd. Program headers are modified or copied from input to output objects, + * but never re-generated. As a result, if the input object has incorrect + * program headers, the output object's program headers will remain incorrect + * or become even worse. + */ + +/* + * Check whether a section is "loadable". If so, add it to the + * corresponding segment list(s) and return 1. + */ +int +add_to_inseg_list(struct elfcopy *ecp, struct section *s) +{ + struct segment *seg; + int loadable; + + if (ecp->ophnum == 0) + return (0); + + /* + * Segment is a different view of an ELF object. One segment can + * contain one or more sections, and one section can be included + * in one or more segments, or not included in any segment at all. + * We call those sections which can be found in one or more segments + * "loadable" sections, and call the rest "unloadable" sections. + * We keep track of "loadable" sections in their containing + * segment(s)' v_sec queue. These information are later used to + * recalculate the extents of segments, when sections are removed, + * for example. + */ + loadable = 0; + STAILQ_FOREACH(seg, &ecp->v_seg, seg_list) { + if (s->off < seg->off || (s->vma < seg->addr && !s->pseudo)) + continue; + 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; + + insert_to_inseg_list(seg, s); + if (seg->type == PT_LOAD) + s->seg = seg; + else if (seg->type == PT_TLS) + s->seg_tls = seg; + s->lma = seg->addr + (s->off - seg->off); + loadable = 1; + } + + return (loadable); +} + +void +adjust_addr(struct elfcopy *ecp) +{ + struct section *s, *s0; + struct segment *seg; + struct sec_action *sac; + uint64_t dl, lma, start, end; + int found, i; + + /* + * Apply VMA and global LMA changes in the first iteration. + */ + TAILQ_FOREACH(s, &ecp->v_sec, sec_list) { + + /* Only adjust loadable section's address. */ + if (!s->loadable || s->seg == NULL) + continue; + + /* Apply global LMA adjustment. */ + if (ecp->change_addr != 0) + s->lma += ecp->change_addr; + + if (!s->pseudo) { + /* Apply global VMA adjustment. */ + if (ecp->change_addr != 0) + s->vma += ecp->change_addr; + + /* Apply section VMA adjustment. */ + sac = lookup_sec_act(ecp, s->name, 0); + if (sac == NULL) + continue; + if (sac->setvma) + s->vma = sac->vma; + if (sac->vma_adjust != 0) + s->vma += sac->vma_adjust; + } + } + + /* + * Apply sections LMA change in the second iteration. + */ + TAILQ_FOREACH(s, &ecp->v_sec, sec_list) { + + /* Only adjust loadable section's LMA. */ + if (!s->loadable || s->seg == NULL) + continue; + + /* + * Check if there is a LMA change request for this + * section. + */ + sac = lookup_sec_act(ecp, s->name, 0); + if (sac == NULL) + continue; + if (!sac->setlma && sac->lma_adjust == 0) + continue; + lma = s->lma; + if (sac->setlma) + lma = sac->lma; + if (sac->lma_adjust != 0) + lma += sac->lma_adjust; + if (lma == s->lma) + continue; + + /* + * Check if the LMA change is viable. + * + * 1. Check if the new LMA is properly aligned accroding to + * section alignment. + * + * 2. Compute the new extent of segment that contains this + * section, make sure it doesn't overlap with other + * segments. + */ +#ifdef DEBUG + printf("LMA for section %s: %#jx\n", s->name, lma); +#endif + + if (lma % s->align != 0) + errx(EXIT_FAILURE, "The load address %#jx for " + "section %s is not aligned to %ju", + (uintmax_t) lma, s->name, s->align); + + if (lma < s->lma) { + /* Move section to lower address. */ + if (lma < s->lma - s->seg->addr) + errx(EXIT_FAILURE, "Not enough space to move " + "section %s load address to %#jx", s->name, + (uintmax_t) lma); + start = lma - (s->lma - s->seg->addr); + if (s == s->seg->v_sec[s->seg->nsec - 1]) + end = start + s->seg->msz; + else + end = s->seg->addr + s->seg->msz; + + } else { + /* Move section to upper address. */ + if (s == s->seg->v_sec[0]) + start = lma; + else + start = s->seg->addr; + end = lma + (s->seg->addr + s->seg->msz - s->lma); + if (end < start) + errx(EXIT_FAILURE, "Not enough space to move " + "section %s load address to %#jx", s->name, + (uintmax_t) lma); + } + +#ifdef DEBUG + printf("new extent for segment containing %s: (%#jx,%#jx)\n", + s->name, start, end); +#endif + + STAILQ_FOREACH(seg, &ecp->v_seg, seg_list) { + if (seg == s->seg || seg->type != PT_LOAD) + continue; + if (start > seg->addr + seg->msz) + continue; + if (end < seg->addr) + continue; + errx(EXIT_FAILURE, "The extent of segment containing " + "section %s overlaps with segment(%#jx,%#jx)", + s->name, seg->addr, seg->addr + seg->msz); + } + + /* + * Update section LMA and file offset. + */ + + if (lma < s->lma) { + /* + * To move a section to lower load address, we decrease + * the load addresses of the section and all the + * sections that are before it, and we increase the + * file offsets of all the sections that are after it. + */ + dl = s->lma - lma; + for (i = 0; i < s->seg->nsec; i++) { + s0 = s->seg->v_sec[i]; + s0->lma -= dl; +#ifdef DEBUG + printf("section %s LMA set to %#jx\n", + s0->name, (uintmax_t) s0->lma); +#endif + if (s0 == s) + break; + } + for (i = i + 1; i < s->seg->nsec; i++) { + s0 = s->seg->v_sec[i]; + s0->off += dl; +#ifdef DEBUG + printf("section %s offset set to %#jx\n", + s0->name, (uintmax_t) s0->off); +#endif + } + } else { + /* + * To move a section to upper load address, we increase + * the load addresses of the section and all the + * sections that are after it, and we increase the + * their file offsets too unless the section in question + * is the first in its containing segment. + */ + dl = lma - s->lma; + for (i = 0; i < s->seg->nsec; i++) + if (s->seg->v_sec[i] == s) + break; + if (i >= s->seg->nsec) + errx(EXIT_FAILURE, "Internal: section `%s' not" + " found in its containing segement", + s->name); + for (; i < s->seg->nsec; i++) { + s0 = s->seg->v_sec[i]; + s0->lma += dl; +#ifdef DEBUG + printf("section %s LMA set to %#jx\n", + s0->name, (uintmax_t) s0->lma); +#endif + if (s != s->seg->v_sec[0]) { + s0->off += dl; +#ifdef DEBUG + printf("section %s offset set to %#jx\n", + s0->name, (uintmax_t) s0->off); +#endif + } + } + } + } + + /* + * Apply load address padding. + */ + + if (ecp->pad_to != 0) { + + /* + * Find the section with highest load address. + */ + + s = NULL; + STAILQ_FOREACH(seg, &ecp->v_seg, seg_list) { + if (seg->type != PT_LOAD) + continue; + for (i = seg->nsec - 1; i >= 0; i--) + if (seg->v_sec[i]->type != SHT_NOBITS) + break; + if (i < 0) + continue; + if (s == NULL) + s = seg->v_sec[i]; + else { + s0 = seg->v_sec[i]; + if (s0->lma > s->lma) + s = s0; + } + } + + if (s == NULL) + goto issue_warn; + + /* No need to pad if the pad_to address is lower. */ + if (ecp->pad_to <= s->lma + s->sz) + goto issue_warn; + + s->pad_sz = ecp->pad_to - (s->lma + s->sz); +#ifdef DEBUG + printf("pad section %s load to address %#jx by %#jx\n", s->name, + (uintmax_t) ecp->pad_to, (uintmax_t) s->pad_sz); +#endif + } + +issue_warn: + + /* + * Issue a warning if there are VMA/LMA adjust requests for + * some nonexistent sections. + */ + if ((ecp->flags & NO_CHANGE_WARN) == 0) { + STAILQ_FOREACH(sac, &ecp->v_sac, sac_list) { + if (!sac->setvma && !sac->setlma && + !sac->vma_adjust && !sac->lma_adjust) + continue; + found = 0; + TAILQ_FOREACH(s, &ecp->v_sec, sec_list) { + if (s->pseudo || s->name == NULL) + continue; + if (!strcmp(s->name, sac->name)) { + found = 1; + break; + } + } + if (!found) + warnx("cannot find section `%s'", sac->name); + } + } +} + +static void +insert_to_inseg_list(struct segment *seg, struct section *sec) +{ + struct section *s; + int i; + + seg->nsec++; + seg->v_sec = realloc(seg->v_sec, seg->nsec * sizeof(*seg->v_sec)); + if (seg->v_sec == NULL) + err(EXIT_FAILURE, "realloc failed"); + + /* + * Sort the section in order of offset. + */ + + for (i = seg->nsec - 1; i > 0; i--) { + s = seg->v_sec[i - 1]; + if (sec->off >= s->off) { + seg->v_sec[i] = sec; + break; + } else + seg->v_sec[i] = s; + } + if (i == 0) + seg->v_sec[0] = sec; +} + +void +setup_phdr(struct elfcopy *ecp) +{ + struct segment *seg; + GElf_Phdr iphdr; + size_t iphnum; + int i; + + if (elf_getphnum(ecp->ein, &iphnum) == 0) + errx(EXIT_FAILURE, "elf_getphnum failed: %s", + elf_errmsg(-1)); + + ecp->ophnum = ecp->iphnum = iphnum; + if (iphnum == 0) + return; + + /* If --only-keep-debug is specified, discard all program headers. */ + if (ecp->strip == STRIP_NONDEBUG) { + ecp->ophnum = 0; + return; + } + + for (i = 0; (size_t)i < iphnum; i++) { + if (gelf_getphdr(ecp->ein, i, &iphdr) != &iphdr) + errx(EXIT_FAILURE, "gelf_getphdr failed: %s", + elf_errmsg(-1)); + if ((seg = calloc(1, sizeof(*seg))) == NULL) + err(EXIT_FAILURE, "calloc failed"); + seg->addr = iphdr.p_vaddr; + seg->off = iphdr.p_offset; + seg->fsz = iphdr.p_filesz; + seg->msz = iphdr.p_memsz; + seg->type = iphdr.p_type; + STAILQ_INSERT_TAIL(&ecp->v_seg, seg, seg_list); + } +} + +void +copy_phdr(struct elfcopy *ecp) +{ + struct segment *seg; + struct section *s; + GElf_Phdr iphdr, ophdr; + int i; + + STAILQ_FOREACH(seg, &ecp->v_seg, seg_list) { + if (seg->type == PT_PHDR) { + if (!TAILQ_EMPTY(&ecp->v_sec)) { + s = TAILQ_FIRST(&ecp->v_sec); + if (s->pseudo) + seg->addr = s->lma + + gelf_fsize(ecp->eout, ELF_T_EHDR, + 1, EV_CURRENT); + } + seg->fsz = seg->msz = gelf_fsize(ecp->eout, ELF_T_PHDR, + ecp->ophnum, EV_CURRENT); + continue; + } + + seg->fsz = seg->msz = 0; + for (i = 0; i < seg->nsec; i++) { + s = seg->v_sec[i]; + seg->msz = s->vma + s->sz - seg->addr; + if (s->type != SHT_NOBITS) + seg->fsz = s->off + s->sz - seg->off; + } + } + + /* + * Allocate space for program headers, note that libelf keep + * track of the number in internal variable, and a call to + * elf_update is needed to update e_phnum of ehdr. + */ + if (gelf_newphdr(ecp->eout, ecp->ophnum) == NULL) + errx(EXIT_FAILURE, "gelf_newphdr() failed: %s", + elf_errmsg(-1)); + + /* + * This elf_update() call is to update the e_phnum field in + * ehdr. It's necessary because later we will call gelf_getphdr(), + * which does sanity check by comparing ndx argument with e_phnum. + */ + if (elf_update(ecp->eout, ELF_C_NULL) < 0) + errx(EXIT_FAILURE, "elf_update() failed: %s", elf_errmsg(-1)); + + /* + * iphnum == ophnum, since we don't remove program headers even if + * they no longer contain sections. + */ + i = 0; + STAILQ_FOREACH(seg, &ecp->v_seg, seg_list) { + if (i >= ecp->iphnum) + break; + if (gelf_getphdr(ecp->ein, i, &iphdr) != &iphdr) + errx(EXIT_FAILURE, "gelf_getphdr failed: %s", + elf_errmsg(-1)); + if (gelf_getphdr(ecp->eout, i, &ophdr) != &ophdr) + errx(EXIT_FAILURE, "gelf_getphdr failed: %s", + elf_errmsg(-1)); + + ophdr.p_type = iphdr.p_type; + ophdr.p_vaddr = seg->addr; + ophdr.p_paddr = seg->addr; + ophdr.p_flags = iphdr.p_flags; + ophdr.p_align = iphdr.p_align; + ophdr.p_offset = seg->off; + ophdr.p_filesz = seg->fsz; + ophdr.p_memsz = seg->msz; + if (!gelf_update_phdr(ecp->eout, i, &ophdr)) + err(EXIT_FAILURE, "gelf_update_phdr failed :%s", + elf_errmsg(-1)); + + i++; + } +} diff --git a/contrib/elftoolchain/elfcopy/strip.1 b/contrib/elftoolchain/elfcopy/strip.1 new file mode 100644 index 000000000000..e07affbb9440 --- /dev/null +++ b/contrib/elftoolchain/elfcopy/strip.1 @@ -0,0 +1,132 @@ +.\" Copyright (c) 2011 Joseph Koshy. All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY JOSEPH KOSHY ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL JOSEPH KOSHY BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (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: strip.1 2069 2011-10-26 15:53:48Z jkoshy $ +.\" +.Dd September 17, 2011 +.Os +.Dt STRIP 1 +.Sh NAME +.Nm strip +.Nd discard information from ELF objects +.Sh SYNOPSIS +.Nm +.Op Fl d | Fl g | Fl S | Fl -strip-debug +.Op Fl h | Fl -help +.Op Fl -only-keep-debug +.Op Fl o Ar outputfile | Fl -output-file= Ns Ar outputfile +.Op Fl p | Fl -preserve-dates +.Op Fl s | Fl -strip-all +.Op Fl -strip-unneeded +.Op Fl w | Fl -wildcard +.Op Fl x | Fl -discard-all +.Op Fl I Ar format | Fl -input-target= Ns Ar format +.Op Fl K Ar symbol | Fl -keep-symbol= Ns Ar symbol +.Op Fl N Ar symbol | Fl -strip-symbol= Ns Ar symbol +.Op Fl O Ar format | Fl -output-target= Ns Ar format +.Op Fl R Ar sectionname | Fl -remove-section= Ns Ar sectionname +.Op Fl V | Fl -version +.Op Fl X | Fl -discard-locals +.Ar +.Sh DESCRIPTION +The +.Nm +utility is used to discard information from ELF objects. +.Pp +The +.Nm +utility supports the following options: +.Bl -tag -width indent +.It Fl d | Fl g | Fl S | Fl -strip-debug +Remove debugging symbols only. +.It Fl h | Fl -help +Print a help message and exit. +.It Fl -only-keep-debug +Remove all content except that which would be used for debugging. +.It Fl o Ar outputfile | Fl -output-file= Ns Ar outputfile +Write the stripped object to file +.Ar outputfile . +The default behaviour is to modify objects in place. +.It Fl p | Fl -preserve-dates +Preserve the object's access and modification times. +.It Fl s | Fl -strip-all +Remove all symbols. +.It Fl -strip-unneeded +Remove all symbols not needed for further relocation processing. +.It Fl w | Fl -wildcard +Use shell-style patterns to name symbols. +The following meta-characters are recognized in patterns: +.Bl -tag -width "...." -compact +.It Li ! +If this is the first character of the pattern, invert the sense of the +pattern match. +.It Li * +Matches any string of characters in a symbol name. +.It Li ? +Matches zero or one character in a symbol name. +.It Li [ +Mark the start of a character class. +.It Li \e +Remove the special meaning of the next character in the pattern. +.It Li ] +Mark the end of a character class. +.El +.It Fl x | Fl -discard-all +Discard all non-global symbols. +.It Fl I Ar format | Fl -input-target= Ns Ar format +These options are accepted, but are ignored. +.It Fl K Ar symbol | Fl -keep-symbol= Ns Ar symbol +Keep the symbol +.Ar symbol +even if it would otherwise be stripped. +This option may be specified multiple times. +.It Fl N Ar symbol | Fl -strip-symbol= Ns Ar symbol +Remove the symbol +.Ar symbol +even if it would otherwise have been kept. +This option may be specified multiple times. +.It Fl O Ar format | Fl -output-target= Ns Ar format +Set the output file format to +.Ar format . +For the full list of supported formats, please see the documentation +for function +.Xr elftc_bfd_find_target 3 . +.It Fl R Ar sectionname | Fl -remove-section= Ns Ar sectionname +Remove the section named by the argument +.Ar sectionname . +This option may be specified multiple times. +.It Fl V | Fl -version +Print a version identifier and exit. +.It Fl X | Fl -discard-locals +Remove compiler-generated local symbols. +.El +.Sh DIAGNOSTICS +.Ex -std +.Sh SEE ALSO +.Xr ar 1 , +.Xr elfcopy 1 , +.Xr ld 1 , +.Xr mcs 1 , +.Xr elf 3 , +.Xr elftc_bfd_find_target 3 , +.Xr fnmatch 3 diff --git a/contrib/elftoolchain/elfcopy/symbols.c b/contrib/elftoolchain/elfcopy/symbols.c new file mode 100644 index 000000000000..4a7d38a10812 --- /dev/null +++ b/contrib/elftoolchain/elfcopy/symbols.c @@ -0,0 +1,1097 @@ +/*- + * Copyright (c) 2007-2013 Kai Wang + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <sys/param.h> +#include <err.h> +#include <fnmatch.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "elfcopy.h" + +ELFTC_VCSID("$Id: symbols.c 3191 2015-05-04 17:07:01Z jkoshy $"); + +/* Symbol table buffer structure. */ +struct symbuf { + Elf32_Sym *l32; /* 32bit local symbol */ + Elf32_Sym *g32; /* 32bit global symbol */ + Elf64_Sym *l64; /* 64bit local symbol */ + Elf64_Sym *g64; /* 64bit global symbol */ + size_t ngs, nls; /* number of each kind */ + size_t gcap, lcap; /* buffer capacities. */ +}; + +struct sthash { + LIST_ENTRY(sthash) sh_next; + size_t sh_off; +}; +typedef LIST_HEAD(,sthash) hash_head; +#define STHASHSIZE 65536 + +struct strimpl { + char *buf; /* string table */ + size_t sz; /* entries */ + size_t cap; /* buffer capacity */ + hash_head hash[STHASHSIZE]; +}; + + +/* String table buffer structure. */ +struct strbuf { + struct strimpl l; /* local symbols */ + struct strimpl g; /* global symbols */ +}; + +static int is_debug_symbol(unsigned char st_info); +static int is_global_symbol(unsigned char st_info); +static int is_local_symbol(unsigned char st_info); +static int is_local_label(const char *name); +static int is_needed_symbol(struct elfcopy *ecp, int i, GElf_Sym *s); +static int is_remove_symbol(struct elfcopy *ecp, size_t sc, int i, + GElf_Sym *s, const char *name); +static int is_weak_symbol(unsigned char st_info); +static int lookup_exact_string(hash_head *hash, const char *buf, + const char *s); +static int generate_symbols(struct elfcopy *ecp); +static void mark_symbols(struct elfcopy *ecp, size_t sc); +static int match_wildcard(const char *name, const char *pattern); +uint32_t str_hash(const char *s); + +/* Convenient bit vector operation macros. */ +#define BIT_SET(v, n) (v[(n)>>3] |= 1U << ((n) & 7)) +#define BIT_CLR(v, n) (v[(n)>>3] &= ~(1U << ((n) & 7))) +#define BIT_ISSET(v, n) (v[(n)>>3] & (1U << ((n) & 7))) + +static int +is_debug_symbol(unsigned char st_info) +{ + + if (GELF_ST_TYPE(st_info) == STT_SECTION || + GELF_ST_TYPE(st_info) == STT_FILE) + return (1); + + return (0); +} + +static int +is_global_symbol(unsigned char st_info) +{ + + if (GELF_ST_BIND(st_info) == STB_GLOBAL) + return (1); + + return (0); +} + +static int +is_weak_symbol(unsigned char st_info) +{ + + if (GELF_ST_BIND(st_info) == STB_WEAK) + return (1); + + return (0); +} + +static int +is_local_symbol(unsigned char st_info) +{ + + if (GELF_ST_BIND(st_info) == STB_LOCAL) + return (1); + + return (0); +} + +static int +is_hidden_symbol(unsigned char st_other) +{ + + if (GELF_ST_VISIBILITY(st_other) == STV_HIDDEN || + GELF_ST_VISIBILITY(st_other) == STV_INTERNAL) + return (1); + + return (0); +} + +static int +is_local_label(const char *name) +{ + + /* Compiler generated local symbols that start with .L */ + if (name[0] == '.' && name[1] == 'L') + return (1); + + return (0); +} + +/* + * Symbols related to relocation are needed. + */ +static int +is_needed_symbol(struct elfcopy *ecp, int i, GElf_Sym *s) +{ + + /* If symbol involves relocation, it is needed. */ + if (BIT_ISSET(ecp->v_rel, i)) + return (1); + + /* + * For relocatable files (.o files), global and weak symbols + * are needed. + */ + if (ecp->flags & RELOCATABLE) { + if (is_global_symbol(s->st_info) || is_weak_symbol(s->st_info)) + return (1); + } + + return (0); +} + +static int +is_remove_symbol(struct elfcopy *ecp, size_t sc, int i, GElf_Sym *s, + const char *name) +{ + GElf_Sym sym0 = { + 0, /* st_name */ + 0, /* st_value */ + 0, /* st_size */ + 0, /* st_info */ + 0, /* st_other */ + SHN_UNDEF, /* st_shndx */ + }; + + if (lookup_symop_list(ecp, name, SYMOP_KEEP) != NULL) + return (0); + + if (lookup_symop_list(ecp, name, SYMOP_STRIP) != NULL) + return (1); + + /* + * Keep the first symbol if it is the special reserved symbol. + * XXX Should we generate one if it's missing? + */ + if (i == 0 && !memcmp(s, &sym0, sizeof(GElf_Sym))) + return (0); + + /* Remove the symbol if the section it refers to was removed. */ + if (s->st_shndx != SHN_UNDEF && s->st_shndx < SHN_LORESERVE && + ecp->secndx[s->st_shndx] == 0) + return (1); + + if (ecp->strip == STRIP_ALL) + return (1); + + if (ecp->v_rel == NULL) + mark_symbols(ecp, sc); + + if (is_needed_symbol(ecp, i, s)) + return (0); + + if (ecp->strip == STRIP_UNNEEDED) + return (1); + + if ((ecp->flags & DISCARD_LOCAL) && is_local_symbol(s->st_info) && + !is_debug_symbol(s->st_info)) + return (1); + + if ((ecp->flags & DISCARD_LLABEL) && is_local_symbol(s->st_info) && + !is_debug_symbol(s->st_info) && is_local_label(name)) + return (1); + + if (ecp->strip == STRIP_DEBUG && is_debug_symbol(s->st_info)) + return (1); + + return (0); +} + +/* + * Mark symbols refered by relocation entries. + */ +static void +mark_symbols(struct elfcopy *ecp, size_t sc) +{ + const char *name; + Elf_Data *d; + Elf_Scn *s; + GElf_Rel r; + GElf_Rela ra; + GElf_Shdr sh; + size_t n, indx; + int elferr, i, len; + + ecp->v_rel = calloc((sc + 7) / 8, 1); + if (ecp->v_rel == NULL) + err(EXIT_FAILURE, "calloc failed"); + + if (elf_getshstrndx(ecp->ein, &indx) == 0) + errx(EXIT_FAILURE, "elf_getshstrndx failed: %s", + elf_errmsg(-1)); + + s = NULL; + while ((s = elf_nextscn(ecp->ein, s)) != NULL) { + if (gelf_getshdr(s, &sh) != &sh) + errx(EXIT_FAILURE, "elf_getshdr failed: %s", + elf_errmsg(-1)); + + if (sh.sh_type != SHT_REL && sh.sh_type != SHT_RELA) + continue; + + /* + * Skip if this reloc section won't appear in the + * output object. + */ + if ((name = elf_strptr(ecp->ein, indx, sh.sh_name)) == NULL) + errx(EXIT_FAILURE, "elf_strptr failed: %s", + elf_errmsg(-1)); + if (is_remove_section(ecp, name) || + is_remove_reloc_sec(ecp, sh.sh_info)) + continue; + + /* Skip if it's not for .symtab */ + if (sh.sh_link != elf_ndxscn(ecp->symtab->is)) + continue; + + d = NULL; + n = 0; + while (n < sh.sh_size && (d = elf_getdata(s, d)) != NULL) { + len = d->d_size / sh.sh_entsize; + for (i = 0; i < len; i++) { + if (sh.sh_type == SHT_REL) { + if (gelf_getrel(d, i, &r) != &r) + errx(EXIT_FAILURE, + "elf_getrel failed: %s", + elf_errmsg(-1)); + n = GELF_R_SYM(r.r_info); + } else { + if (gelf_getrela(d, i, &ra) != &ra) + errx(EXIT_FAILURE, + "elf_getrela failed: %s", + elf_errmsg(-1)); + n = GELF_R_SYM(ra.r_info); + } + if (n > 0 && n < sc) + BIT_SET(ecp->v_rel, n); + else if (n != 0) + warnx("invalid symbox index"); + } + } + elferr = elf_errno(); + if (elferr != 0) + errx(EXIT_FAILURE, "elf_getdata failed: %s", + elf_errmsg(elferr)); + } + elferr = elf_errno(); + if (elferr != 0) + errx(EXIT_FAILURE, "elf_nextscn failed: %s", + elf_errmsg(elferr)); +} + +static int +generate_symbols(struct elfcopy *ecp) +{ + struct section *s; + struct symop *sp; + struct symbuf *sy_buf; + struct strbuf *st_buf; + const char *name; + char *newname; + unsigned char *gsym; + GElf_Shdr ish; + GElf_Sym sym; + Elf_Data* id; + Elf_Scn *is; + size_t ishstrndx, namelen, ndx, sc, symndx; + int ec, elferr, i; + + if (elf_getshstrndx(ecp->ein, &ishstrndx) == 0) + errx(EXIT_FAILURE, "elf_getshstrndx failed: %s", + elf_errmsg(-1)); + if ((ec = gelf_getclass(ecp->eout)) == ELFCLASSNONE) + errx(EXIT_FAILURE, "gelf_getclass failed: %s", + elf_errmsg(-1)); + + /* Create buffers for .symtab and .strtab. */ + if ((sy_buf = calloc(1, sizeof(*sy_buf))) == NULL) + err(EXIT_FAILURE, "calloc failed"); + if ((st_buf = calloc(1, sizeof(*st_buf))) == NULL) + err(EXIT_FAILURE, "calloc failed"); + sy_buf->gcap = sy_buf->lcap = 64; + st_buf->g.cap = 256; + st_buf->l.cap = 64; + st_buf->l.sz = 1; /* '\0' at start. */ + st_buf->g.sz = 0; + + ecp->symtab->sz = 0; + ecp->strtab->sz = 0; + ecp->symtab->buf = sy_buf; + ecp->strtab->buf = st_buf; + + /* + * Create bit vector v_secsym, which is used to mark sections + * that already have corresponding STT_SECTION symbols. + */ + ecp->v_secsym = calloc((ecp->nos + 7) / 8, 1); + if (ecp->v_secsym == NULL) + err(EXIT_FAILURE, "calloc failed"); + + /* Locate .strtab of input object. */ + symndx = 0; + name = NULL; + is = NULL; + while ((is = elf_nextscn(ecp->ein, is)) != NULL) { + if (gelf_getshdr(is, &ish) != &ish) + errx(EXIT_FAILURE, "elf_getshdr failed: %s", + elf_errmsg(-1)); + if ((name = elf_strptr(ecp->ein, ishstrndx, ish.sh_name)) == + NULL) + errx(EXIT_FAILURE, "elf_strptr failed: %s", + elf_errmsg(-1)); + if (strcmp(name, ".strtab") == 0) { + symndx = elf_ndxscn(is); + break; + } + } + elferr = elf_errno(); + if (elferr != 0) + errx(EXIT_FAILURE, "elf_nextscn failed: %s", + elf_errmsg(elferr)); + + /* Symbol table should exist if this function is called. */ + if (symndx == 0) { + warnx("can't find .strtab section"); + return (0); + } + + /* Locate .symtab of input object. */ + is = NULL; + while ((is = elf_nextscn(ecp->ein, is)) != NULL) { + if (gelf_getshdr(is, &ish) != &ish) + errx(EXIT_FAILURE, "elf_getshdr failed: %s", + elf_errmsg(-1)); + if ((name = elf_strptr(ecp->ein, ishstrndx, ish.sh_name)) == + NULL) + errx(EXIT_FAILURE, "elf_strptr failed: %s", + elf_errmsg(-1)); + if (strcmp(name, ".symtab") == 0) + break; + } + elferr = elf_errno(); + if (elferr != 0) + errx(EXIT_FAILURE, "elf_nextscn failed: %s", + elf_errmsg(elferr)); + if (is == NULL) + errx(EXIT_FAILURE, "can't find .strtab section"); + + /* + * Create bit vector gsym to mark global symbols, and symndx + * to keep track of symbol index changes from input object to + * output object, it is used by update_reloc() later to update + * relocation information. + */ + gsym = NULL; + sc = ish.sh_size / ish.sh_entsize; + if (sc > 0) { + ecp->symndx = calloc(sc, sizeof(*ecp->symndx)); + if (ecp->symndx == NULL) + err(EXIT_FAILURE, "calloc failed"); + gsym = calloc((sc + 7) / 8, sizeof(*gsym)); + if (gsym == NULL) + err(EXIT_FAILURE, "calloc failed"); + if ((id = elf_getdata(is, NULL)) == NULL) { + elferr = elf_errno(); + if (elferr != 0) + errx(EXIT_FAILURE, "elf_getdata failed: %s", + elf_errmsg(elferr)); + return (0); + } + } else + return (0); + + /* Copy/Filter each symbol. */ + for (i = 0; (size_t)i < sc; i++) { + if (gelf_getsym(id, i, &sym) != &sym) + errx(EXIT_FAILURE, "gelf_getsym failed: %s", + elf_errmsg(-1)); + if ((name = elf_strptr(ecp->ein, symndx, sym.st_name)) == NULL) + errx(EXIT_FAILURE, "elf_strptr failed: %s", + elf_errmsg(-1)); + + /* Symbol filtering. */ + if (is_remove_symbol(ecp, sc, i, &sym, name) != 0) + continue; + + /* Check if we need to change the binding of this symbol. */ + if (is_global_symbol(sym.st_info) || + is_weak_symbol(sym.st_info)) { + /* + * XXX Binutils objcopy does not weaken certain + * symbols. + */ + if (ecp->flags & WEAKEN_ALL || + lookup_symop_list(ecp, name, SYMOP_WEAKEN) != NULL) + sym.st_info = GELF_ST_INFO(STB_WEAK, + GELF_ST_TYPE(sym.st_info)); + /* Do not localize undefined symbols. */ + if (sym.st_shndx != SHN_UNDEF && + lookup_symop_list(ecp, name, SYMOP_LOCALIZE) != + NULL) + sym.st_info = GELF_ST_INFO(STB_LOCAL, + GELF_ST_TYPE(sym.st_info)); + if (ecp->flags & KEEP_GLOBAL && + sym.st_shndx != SHN_UNDEF && + lookup_symop_list(ecp, name, SYMOP_KEEPG) == NULL) + sym.st_info = GELF_ST_INFO(STB_LOCAL, + GELF_ST_TYPE(sym.st_info)); + if (ecp->flags & LOCALIZE_HIDDEN && + sym.st_shndx != SHN_UNDEF && + is_hidden_symbol(sym.st_other)) + sym.st_info = GELF_ST_INFO(STB_LOCAL, + GELF_ST_TYPE(sym.st_info)); + } else { + /* STB_LOCAL binding. */ + if (lookup_symop_list(ecp, name, SYMOP_GLOBALIZE) != + NULL) + sym.st_info = GELF_ST_INFO(STB_GLOBAL, + GELF_ST_TYPE(sym.st_info)); + /* XXX We should globalize weak symbol? */ + } + + /* Check if we need to rename this symbol. */ + if ((sp = lookup_symop_list(ecp, name, SYMOP_REDEF)) != NULL) + name = sp->newname; + + /* Check if we need to prefix the symbols. */ + newname = NULL; + if (ecp->prefix_sym != NULL && name != NULL && *name != '\0') { + namelen = strlen(name) + strlen(ecp->prefix_sym) + 1; + if ((newname = malloc(namelen)) == NULL) + err(EXIT_FAILURE, "malloc failed"); + snprintf(newname, namelen, "%s%s", ecp->prefix_sym, + name); + name = newname; + } + + /* Copy symbol, mark global/weak symbol and add to index map. */ + if (is_global_symbol(sym.st_info) || + is_weak_symbol(sym.st_info)) { + BIT_SET(gsym, i); + ecp->symndx[i] = sy_buf->ngs; + } else + ecp->symndx[i] = sy_buf->nls; + add_to_symtab(ecp, name, sym.st_value, sym.st_size, + sym.st_shndx, sym.st_info, sym.st_other, 0); + + if (newname != NULL) + free(newname); + + /* + * If the symbol is a STT_SECTION symbol, mark the section + * it points to. + */ + if (GELF_ST_TYPE(sym.st_info) == STT_SECTION) + BIT_SET(ecp->v_secsym, ecp->secndx[sym.st_shndx]); + } + + /* + * Give up if there is no real symbols inside the table. + * XXX The logic here needs to be improved. We need to + * check if that only local symbol is the reserved symbol. + */ + if (sy_buf->nls <= 1 && sy_buf->ngs == 0) + return (0); + + /* + * Create STT_SECTION symbols for sections that do not already + * got one. However, we do not create STT_SECTION symbol for + * .symtab, .strtab, .shstrtab and reloc sec of relocatables. + */ + TAILQ_FOREACH(s, &ecp->v_sec, sec_list) { + if (s->pseudo) + continue; + if (strcmp(s->name, ".symtab") == 0 || + strcmp(s->name, ".strtab") == 0 || + strcmp(s->name, ".shstrtab") == 0) + continue; + if ((ecp->flags & RELOCATABLE) != 0 && + ((s->type == SHT_REL) || (s->type == SHT_RELA))) + continue; + + if ((ndx = elf_ndxscn(s->os)) == SHN_UNDEF) + errx(EXIT_FAILURE, "elf_ndxscn failed: %s", + elf_errmsg(-1)); + + if (!BIT_ISSET(ecp->v_secsym, ndx)) { + sym.st_name = 0; + sym.st_value = s->vma; + sym.st_size = 0; + sym.st_info = GELF_ST_INFO(STB_LOCAL, STT_SECTION); + /* + * Don't let add_to_symtab() touch sym.st_shndx. + * In this case, we know the index already. + */ + add_to_symtab(ecp, NULL, sym.st_value, sym.st_size, + ndx, sym.st_info, sym.st_other, 1); + } + } + + /* + * Update st_name and index map for global/weak symbols. Note that + * global/weak symbols are put after local symbols. + */ + if (gsym != NULL) { + for(i = 0; (size_t) i < sc; i++) { + if (!BIT_ISSET(gsym, i)) + continue; + + /* Update st_name. */ + if (ec == ELFCLASS32) + sy_buf->g32[ecp->symndx[i]].st_name += + st_buf->l.sz; + else + sy_buf->g64[ecp->symndx[i]].st_name += + st_buf->l.sz; + + /* Update index map. */ + ecp->symndx[i] += sy_buf->nls; + } + free(gsym); + } + + return (1); +} + +void +create_symtab(struct elfcopy *ecp) +{ + struct section *s, *sy, *st; + size_t maxndx, ndx; + + sy = ecp->symtab; + st = ecp->strtab; + + /* + * Set section index map for .symtab and .strtab. We need to set + * these map because otherwise symbols which refer to .symtab and + * .strtab will be removed by symbol filtering unconditionally. + * And we have to figure out scn index this way (instead of calling + * elf_ndxscn) because we can not create Elf_Scn before we're certain + * that .symtab and .strtab will exist in the output object. + */ + maxndx = 0; + TAILQ_FOREACH(s, &ecp->v_sec, sec_list) { + if (s->os == NULL) + continue; + if ((ndx = elf_ndxscn(s->os)) == SHN_UNDEF) + errx(EXIT_FAILURE, "elf_ndxscn failed: %s", + elf_errmsg(-1)); + if (ndx > maxndx) + maxndx = ndx; + } + ecp->secndx[elf_ndxscn(sy->is)] = maxndx + 1; + ecp->secndx[elf_ndxscn(st->is)] = maxndx + 2; + + /* + * Generate symbols for output object if SYMTAB_INTACT is not set. + * If there is no symbol in the input object or all the symbols are + * stripped, then free all the resouces allotted for symbol table, + * and clear SYMTAB_EXIST flag. + */ + if (((ecp->flags & SYMTAB_INTACT) == 0) && !generate_symbols(ecp)) { + TAILQ_REMOVE(&ecp->v_sec, ecp->symtab, sec_list); + TAILQ_REMOVE(&ecp->v_sec, ecp->strtab, sec_list); + free(ecp->symtab); + free(ecp->strtab); + ecp->symtab = NULL; + ecp->strtab = NULL; + ecp->flags &= ~SYMTAB_EXIST; + return; + } + + /* Create output Elf_Scn for .symtab and .strtab. */ + if ((sy->os = elf_newscn(ecp->eout)) == NULL || + (st->os = elf_newscn(ecp->eout)) == NULL) + errx(EXIT_FAILURE, "elf_newscn failed: %s", + elf_errmsg(-1)); + /* Update secndx anyway. */ + ecp->secndx[elf_ndxscn(sy->is)] = elf_ndxscn(sy->os); + ecp->secndx[elf_ndxscn(st->is)] = elf_ndxscn(st->os); + + /* + * Copy .symtab and .strtab section headers from input to output + * object to start with, these will be overridden later if need. + */ + copy_shdr(ecp, sy, ".symtab", 1, 0); + copy_shdr(ecp, st, ".strtab", 1, 0); + + /* Copy verbatim if symbol table is intact. */ + if (ecp->flags & SYMTAB_INTACT) { + copy_data(sy); + copy_data(st); + return; + } + + create_symtab_data(ecp); +} + +void +free_symtab(struct elfcopy *ecp) +{ + struct symbuf *sy_buf; + struct strbuf *st_buf; + struct sthash *sh, *shtmp; + int i; + + if (ecp->symtab != NULL && ecp->symtab->buf != NULL) { + sy_buf = ecp->symtab->buf; + if (sy_buf->l32 != NULL) + free(sy_buf->l32); + if (sy_buf->g32 != NULL) + free(sy_buf->g32); + if (sy_buf->l64 != NULL) + free(sy_buf->l64); + if (sy_buf->g64 != NULL) + free(sy_buf->g64); + } + + if (ecp->strtab != NULL && ecp->strtab->buf != NULL) { + st_buf = ecp->strtab->buf; + if (st_buf->l.buf != NULL) + free(st_buf->l.buf); + if (st_buf->g.buf != NULL) + free(st_buf->g.buf); + for (i = 0; i < STHASHSIZE; i++) { + LIST_FOREACH_SAFE(sh, &st_buf->l.hash[i], sh_next, + shtmp) { + LIST_REMOVE(sh, sh_next); + free(sh); + } + LIST_FOREACH_SAFE(sh, &st_buf->g.hash[i], sh_next, + shtmp) { + LIST_REMOVE(sh, sh_next); + free(sh); + } + } + } +} + +void +create_external_symtab(struct elfcopy *ecp) +{ + struct section *s; + struct symbuf *sy_buf; + struct strbuf *st_buf; + GElf_Shdr sh; + size_t ndx; + + if (ecp->oec == ELFCLASS32) + ecp->symtab = create_external_section(ecp, ".symtab", NULL, + NULL, 0, 0, SHT_SYMTAB, ELF_T_SYM, 0, 4, 0, 0); + else + ecp->symtab = create_external_section(ecp, ".symtab", NULL, + NULL, 0, 0, SHT_SYMTAB, ELF_T_SYM, 0, 8, 0, 0); + + ecp->strtab = create_external_section(ecp, ".strtab", NULL, NULL, 0, 0, + SHT_STRTAB, ELF_T_BYTE, 0, 1, 0, 0); + + /* Let sh_link field of .symtab section point to .strtab section. */ + if (gelf_getshdr(ecp->symtab->os, &sh) == NULL) + errx(EXIT_FAILURE, "gelf_getshdr() failed: %s", + elf_errmsg(-1)); + sh.sh_link = elf_ndxscn(ecp->strtab->os); + if (!gelf_update_shdr(ecp->symtab->os, &sh)) + errx(EXIT_FAILURE, "gelf_update_shdr() failed: %s", + elf_errmsg(-1)); + + /* Create buffers for .symtab and .strtab. */ + if ((sy_buf = calloc(1, sizeof(*sy_buf))) == NULL) + err(EXIT_FAILURE, "calloc failed"); + if ((st_buf = calloc(1, sizeof(*st_buf))) == NULL) + err(EXIT_FAILURE, "calloc failed"); + sy_buf->gcap = sy_buf->lcap = 64; + st_buf->g.cap = 256; + st_buf->l.cap = 64; + st_buf->l.sz = 1; /* '\0' at start. */ + st_buf->g.sz = 0; + + ecp->symtab->sz = 0; + ecp->strtab->sz = 0; + ecp->symtab->buf = sy_buf; + ecp->strtab->buf = st_buf; + + /* Always create the special symbol at the symtab beginning. */ + add_to_symtab(ecp, NULL, 0, 0, SHN_UNDEF, + ELF32_ST_INFO(STB_LOCAL, STT_NOTYPE), 0, 1); + + /* Create STT_SECTION symbols. */ + TAILQ_FOREACH(s, &ecp->v_sec, sec_list) { + if (s->pseudo) + continue; + if (strcmp(s->name, ".symtab") == 0 || + strcmp(s->name, ".strtab") == 0 || + strcmp(s->name, ".shstrtab") == 0) + continue; + (void) elf_errno(); + if ((ndx = elf_ndxscn(s->os)) == SHN_UNDEF) { + warnx("elf_ndxscn failed: %s", + elf_errmsg(-1)); + continue; + } + add_to_symtab(ecp, NULL, 0, 0, ndx, + GELF_ST_INFO(STB_LOCAL, STT_SECTION), 0, 1); + } +} + +void +add_to_symtab(struct elfcopy *ecp, const char *name, uint64_t st_value, + uint64_t st_size, uint16_t st_shndx, unsigned char st_info, + unsigned char st_other, int ndx_known) +{ + struct symbuf *sy_buf; + struct strbuf *st_buf; + struct sthash *sh; + uint32_t hash; + int pos; + + /* + * Convenient macro for copying global/local 32/64 bit symbols + * from input object to the buffer created for output object. + * It handles buffer growing, st_name calculating and st_shndx + * updating for symbols with non-special section index. + */ +#define _ADDSYM(B, SZ) do { \ + if (sy_buf->B##SZ == NULL) { \ + sy_buf->B##SZ = malloc(sy_buf->B##cap * \ + sizeof(Elf##SZ##_Sym)); \ + if (sy_buf->B##SZ == NULL) \ + err(EXIT_FAILURE, "malloc failed"); \ + } else if (sy_buf->n##B##s >= sy_buf->B##cap) { \ + sy_buf->B##cap *= 2; \ + sy_buf->B##SZ = realloc(sy_buf->B##SZ, sy_buf->B##cap * \ + sizeof(Elf##SZ##_Sym)); \ + if (sy_buf->B##SZ == NULL) \ + err(EXIT_FAILURE, "realloc failed"); \ + } \ + sy_buf->B##SZ[sy_buf->n##B##s].st_info = st_info; \ + sy_buf->B##SZ[sy_buf->n##B##s].st_other = st_other; \ + sy_buf->B##SZ[sy_buf->n##B##s].st_value = st_value; \ + sy_buf->B##SZ[sy_buf->n##B##s].st_size = st_size; \ + if (ndx_known) \ + sy_buf->B##SZ[sy_buf->n##B##s].st_shndx = st_shndx; \ + else if (st_shndx == SHN_UNDEF || st_shndx >= SHN_LORESERVE) \ + sy_buf->B##SZ[sy_buf->n##B##s].st_shndx = st_shndx; \ + else \ + sy_buf->B##SZ[sy_buf->n##B##s].st_shndx = \ + ecp->secndx[st_shndx]; \ + if (st_buf->B.buf == NULL) { \ + st_buf->B.buf = calloc(st_buf->B.cap, \ + sizeof(*st_buf->B.buf)); \ + if (st_buf->B.buf == NULL) \ + err(EXIT_FAILURE, "malloc failed"); \ + } \ + if (name != NULL && *name != '\0') { \ + pos = lookup_exact_string(st_buf->B.hash, st_buf->B.buf,\ + name); \ + if (pos != -1) \ + sy_buf->B##SZ[sy_buf->n##B##s].st_name = pos; \ + else { \ + sy_buf->B##SZ[sy_buf->n##B##s].st_name = \ + st_buf->B.sz; \ + while (st_buf->B.sz + strlen(name) >= \ + st_buf->B.cap - 1) { \ + st_buf->B.cap *= 2; \ + st_buf->B.buf = realloc(st_buf->B.buf, \ + st_buf->B.cap); \ + if (st_buf->B.buf == NULL) \ + err(EXIT_FAILURE, \ + "realloc failed"); \ + } \ + if ((sh = malloc(sizeof(*sh))) == NULL) \ + err(EXIT_FAILURE, "malloc failed"); \ + sh->sh_off = st_buf->B.sz; \ + hash = str_hash(name); \ + LIST_INSERT_HEAD(&st_buf->B.hash[hash], sh, \ + sh_next); \ + strncpy(&st_buf->B.buf[st_buf->B.sz], name, \ + strlen(name)); \ + st_buf->B.buf[st_buf->B.sz + strlen(name)] = '\0'; \ + st_buf->B.sz += strlen(name) + 1; \ + } \ + } else \ + sy_buf->B##SZ[sy_buf->n##B##s].st_name = 0; \ + sy_buf->n##B##s++; \ +} while (0) + + sy_buf = ecp->symtab->buf; + st_buf = ecp->strtab->buf; + + if (ecp->oec == ELFCLASS32) { + if (is_local_symbol(st_info)) + _ADDSYM(l, 32); + else + _ADDSYM(g, 32); + } else { + if (is_local_symbol(st_info)) + _ADDSYM(l, 64); + else + _ADDSYM(g, 64); + } + + /* Update section size. */ + ecp->symtab->sz = (sy_buf->nls + sy_buf->ngs) * + (ecp->oec == ELFCLASS32 ? sizeof(Elf32_Sym) : sizeof(Elf64_Sym)); + ecp->strtab->sz = st_buf->l.sz + st_buf->g.sz; + +#undef _ADDSYM +} + +void +finalize_external_symtab(struct elfcopy *ecp) +{ + struct symbuf *sy_buf; + struct strbuf *st_buf; + int i; + + /* + * Update st_name for global/weak symbols. (global/weak symbols + * are put after local symbols) + */ + sy_buf = ecp->symtab->buf; + st_buf = ecp->strtab->buf; + for (i = 0; (size_t) i < sy_buf->ngs; i++) { + if (ecp->oec == ELFCLASS32) + sy_buf->g32[i].st_name += st_buf->l.sz; + else + sy_buf->g64[i].st_name += st_buf->l.sz; + } +} + +void +create_symtab_data(struct elfcopy *ecp) +{ + struct section *sy, *st; + struct symbuf *sy_buf; + struct strbuf *st_buf; + Elf_Data *gsydata, *lsydata, *gstdata, *lstdata; + GElf_Shdr shy, sht; + + sy = ecp->symtab; + st = ecp->strtab; + + if (gelf_getshdr(sy->os, ­) == NULL) + errx(EXIT_FAILURE, "gelf_getshdr() failed: %s", + elf_errmsg(-1)); + if (gelf_getshdr(st->os, &sht) == NULL) + errx(EXIT_FAILURE, "gelf_getshdr() failed: %s", + elf_errmsg(-1)); + + /* + * Create two Elf_Data for .symtab section of output object, one + * for local symbols and another for global symbols. Note that + * local symbols appear first in the .symtab. + */ + sy_buf = sy->buf; + if (sy_buf->nls > 0) { + if ((lsydata = elf_newdata(sy->os)) == NULL) + errx(EXIT_FAILURE, "elf_newdata() failed: %s.", + elf_errmsg(-1)); + if (ecp->oec == ELFCLASS32) { + lsydata->d_align = 4; + lsydata->d_off = 0; + lsydata->d_buf = sy_buf->l32; + lsydata->d_size = sy_buf->nls * + sizeof(Elf32_Sym); + lsydata->d_type = ELF_T_SYM; + lsydata->d_version = EV_CURRENT; + } else { + lsydata->d_align = 8; + lsydata->d_off = 0; + lsydata->d_buf = sy_buf->l64; + lsydata->d_size = sy_buf->nls * + sizeof(Elf64_Sym); + lsydata->d_type = ELF_T_SYM; + lsydata->d_version = EV_CURRENT; + } + } + if (sy_buf->ngs > 0) { + if ((gsydata = elf_newdata(sy->os)) == NULL) + errx(EXIT_FAILURE, "elf_newdata() failed: %s.", + elf_errmsg(-1)); + if (ecp->oec == ELFCLASS32) { + gsydata->d_align = 4; + gsydata->d_off = sy_buf->nls * + sizeof(Elf32_Sym); + gsydata->d_buf = sy_buf->g32; + gsydata->d_size = sy_buf->ngs * + sizeof(Elf32_Sym); + gsydata->d_type = ELF_T_SYM; + gsydata->d_version = EV_CURRENT; + } else { + gsydata->d_align = 8; + gsydata->d_off = sy_buf->nls * + sizeof(Elf64_Sym); + gsydata->d_buf = sy_buf->g64; + gsydata->d_size = sy_buf->ngs * + sizeof(Elf64_Sym); + gsydata->d_type = ELF_T_SYM; + gsydata->d_version = EV_CURRENT; + } + } + + /* + * Create two Elf_Data for .strtab, one for local symbol name + * and another for globals. Same as .symtab, local symbol names + * appear first. + */ + st_buf = st->buf; + if ((lstdata = elf_newdata(st->os)) == NULL) + errx(EXIT_FAILURE, "elf_newdata() failed: %s.", + elf_errmsg(-1)); + lstdata->d_align = 1; + lstdata->d_off = 0; + lstdata->d_buf = st_buf->l.buf; + lstdata->d_size = st_buf->l.sz; + lstdata->d_type = ELF_T_BYTE; + lstdata->d_version = EV_CURRENT; + + if (st_buf->g.sz > 0) { + if ((gstdata = elf_newdata(st->os)) == NULL) + errx(EXIT_FAILURE, "elf_newdata() failed: %s.", + elf_errmsg(-1)); + gstdata->d_align = 1; + gstdata->d_off = lstdata->d_size; + gstdata->d_buf = st_buf->g.buf; + gstdata->d_size = st_buf->g.sz; + gstdata->d_type = ELF_T_BYTE; + gstdata->d_version = EV_CURRENT; + } + + shy.sh_addr = 0; + shy.sh_addralign = (ecp->oec == ELFCLASS32 ? 4 : 8); + shy.sh_size = sy->sz; + shy.sh_type = SHT_SYMTAB; + shy.sh_flags = 0; + shy.sh_entsize = gelf_fsize(ecp->eout, ELF_T_SYM, 1, + EV_CURRENT); + /* + * According to SYSV abi, here sh_info is one greater than + * the symbol table index of the last local symbol(binding + * STB_LOCAL). + */ + shy.sh_info = sy_buf->nls; + + sht.sh_addr = 0; + sht.sh_addralign = 1; + sht.sh_size = st->sz; + sht.sh_type = SHT_STRTAB; + sht.sh_flags = 0; + sht.sh_entsize = 0; + sht.sh_info = 0; + sht.sh_link = 0; + + if (!gelf_update_shdr(sy->os, ­)) + errx(EXIT_FAILURE, "gelf_update_shdr() failed: %s", + elf_errmsg(-1)); + if (!gelf_update_shdr(st->os, &sht)) + errx(EXIT_FAILURE, "gelf_update_shdr() failed: %s", + elf_errmsg(-1)); +} + +void +add_to_symop_list(struct elfcopy *ecp, const char *name, const char *newname, + unsigned int op) +{ + struct symop *s; + + if ((s = lookup_symop_list(ecp, name, ~0U)) == NULL) { + if ((s = calloc(1, sizeof(*s))) == NULL) + errx(EXIT_FAILURE, "not enough memory"); + s->name = name; + if (op == SYMOP_REDEF) + s->newname = newname; + } + + s->op |= op; + STAILQ_INSERT_TAIL(&ecp->v_symop, s, symop_list); +} + +static int +match_wildcard(const char *name, const char *pattern) +{ + int reverse, match; + + reverse = 0; + if (*pattern == '!') { + reverse = 1; + pattern++; + } + + match = 0; + if (!fnmatch(pattern, name, 0)) + match = 1; + + return (reverse ? !match : match); +} + +struct symop * +lookup_symop_list(struct elfcopy *ecp, const char *name, unsigned int op) +{ + struct symop *s; + + STAILQ_FOREACH(s, &ecp->v_symop, symop_list) { + if (name == NULL || !strcmp(name, s->name) || + ((ecp->flags & WILDCARD) && match_wildcard(name, s->name))) + if ((s->op & op) != 0) + return (s); + } + + return (NULL); +} + +static int +lookup_exact_string(hash_head *buckets, const char *buf, const char *s) +{ + struct sthash *sh; + uint32_t hash; + + hash = str_hash(s); + LIST_FOREACH(sh, &buckets[hash], sh_next) + if (strcmp(buf + sh->sh_off, s) == 0) + return sh->sh_off; + return (-1); +} + +uint32_t +str_hash(const char *s) +{ + uint32_t hash; + + for (hash = 2166136261UL; *s; s++) + hash = (hash ^ *s) * 16777619; + + return (hash & (STHASHSIZE - 1)); +} diff --git a/contrib/elftoolchain/libdwarf/Makefile b/contrib/elftoolchain/libdwarf/Makefile index 81b5657a2f72..d0a5443e6fb9 100644 --- a/contrib/elftoolchain/libdwarf/Makefile +++ b/contrib/elftoolchain/libdwarf/Makefile @@ -1,4 +1,4 @@ -# $Id: Makefile 2937 2013-04-27 04:48:23Z jkoshy $ +# $Id: Makefile 3097 2014-09-02 22:10:18Z kaiwang27 $ TOP= ${.CURDIR}/.. @@ -42,6 +42,7 @@ SRCS= \ dwarf_pubtypes.c \ dwarf_ranges.c \ dwarf_reloc.c \ + dwarf_sections.c \ dwarf_seterror.c \ dwarf_str.c \ dwarf_types.c \ @@ -115,6 +116,7 @@ MAN= dwarf.3 \ dwarf_add_weakname.3 \ dwarf_attr.3 \ dwarf_attrlist.3 \ + dwarf_attroffset.3 \ dwarf_attrval_signed.3 \ dwarf_child.3 \ dwarf_dealloc.3 \ @@ -154,6 +156,7 @@ MAN= dwarf.3 \ dwarf_get_cie_info.3 \ dwarf_get_cie_of_fde.3 \ dwarf_get_cu_die_offset.3 \ + dwarf_get_die_infotypes_flag.3 \ dwarf_get_elf.3 \ dwarf_get_fde_at_pc.3 \ dwarf_get_fde_info_for_all_regs.3 \ @@ -175,6 +178,7 @@ MAN= dwarf.3 \ dwarf_get_relocation_info.3 \ dwarf_get_relocation_info_count.3 \ dwarf_get_section_bytes.3 \ + dwarf_get_section_max_offsets.3 \ dwarf_get_str.3 \ dwarf_get_types.3 \ dwarf_get_vars.3 \ @@ -192,6 +196,7 @@ MAN= dwarf.3 \ dwarf_new_expr.3 \ dwarf_new_fde.3 \ dwarf_next_cu_header.3 \ + dwarf_next_types_section.3 \ dwarf_object_init.3 \ dwarf_producer_init.3 \ dwarf_producer_set_isa.3 \ @@ -220,7 +225,9 @@ MLINKS+= \ dwarf_attrval_signed.3 dwarf_attrval_string.3 \ dwarf_attrval_signed.3 dwarf_attrval_unsigned.3 \ dwarf_child.3 dwarf_offdie.3 \ + dwarf_child.3 dwarf_offdie_b.3 \ dwarf_child.3 dwarf_siblingof.3 \ + dwarf_child.3 dwarf_siblingof_b.3 \ dwarf_dealloc.3 dwarf_fde_cie_list_dealloc.3 \ dwarf_dealloc.3 dwarf_funcs_dealloc.3 \ dwarf_dealloc.3 dwarf_globals_dealloc.3 \ @@ -234,6 +241,7 @@ MLINKS+= \ dwarf_dieoffset.3 dwarf_die_CU_offset.3 \ dwarf_dieoffset.3 dwarf_die_CU_offset_range.3 \ dwarf_dieoffset.3 dwarf_get_cu_die_offset_given_cu_header_offset.3 \ + dwarf_dieoffset.3 dwarf_get_cu_die_offset_given_cu_header_offset_b.3 \ dwarf_finish.3 dwarf_object_finish.3 \ dwarf_formref.3 dwarf_global_formref.3 \ dwarf_formudata.3 dwarf_formsdata.3 \ @@ -273,6 +281,7 @@ MLINKS+= \ dwarf_get_pubtypes.3 dwarf_pubtype_name_offsets.3 \ dwarf_get_pubtypes.3 dwarf_pubtypename.3 \ dwarf_get_ranges.3 dwarf_get_ranges_a.3 \ + dwarf_get_section_max_offsets.3 dwarf_get_section_max_offsets_b.3 \ dwarf_get_types.3 dwarf_type_die_offset.3 \ dwarf_get_types.3 dwarf_type_cu_offset.3 \ dwarf_get_types.3 dwarf_type_name_offsets.3 \ @@ -291,6 +300,7 @@ MLINKS+= \ dwarf_highpc.3 dwarf_bitoffset.3 \ dwarf_highpc.3 dwarf_bitsize.3 \ dwarf_highpc.3 dwarf_bytesize.3 \ + dwarf_highpc.3 dwarf_highpc_b.3 \ dwarf_highpc.3 dwarf_lowpc.3 \ dwarf_highpc.3 dwarf_srclang.3 \ dwarf_lineno.3 dwarf_lineaddr.3 \ @@ -302,6 +312,9 @@ MLINKS+= \ dwarf_lineno.3 dwarf_line_srcfileno.3 \ dwarf_loclist.3 dwarf_loclist_n.3 \ dwarf_loclist_from_expr.3 dwarf_loclist_from_expr_a.3 \ + dwarf_loclist_from_expr.3 dwarf_loclist_from_expr_b.3 \ + dwarf_next_cu_header.3 dwarf_next_cu_header_b.3 \ + dwarf_next_cu_header.3 dwarf_next_cu_header_c.3 \ dwarf_producer_init.3 dwarf_producer_init_b.3 \ dwarf_seterrarg.3 dwarf_seterrhand.3 \ dwarf_set_frame_cfa_value.3 dwarf_set_frame_rule_initial_value.3 \ diff --git a/contrib/elftoolchain/libdwarf/Version.map b/contrib/elftoolchain/libdwarf/Version.map index 6e6548bd9732..669f70e44dac 100644 --- a/contrib/elftoolchain/libdwarf/Version.map +++ b/contrib/elftoolchain/libdwarf/Version.map @@ -1,4 +1,4 @@ -/* $Id: Version.map 2576 2012-09-13 09:16:11Z jkoshy $ */ +/* $Id: Version.map 3085 2014-09-02 22:08:23Z kaiwang27 $ */ R1.0 { global: @@ -39,6 +39,7 @@ global: dwarf_arrayorder; dwarf_attr; dwarf_attrlist; + dwarf_attroffset; dwarf_attrval_flag; dwarf_attrval_signed; dwarf_attrval_string; @@ -116,6 +117,8 @@ global: dwarf_get_cie_of_fde; dwarf_get_cu_die_offset; dwarf_get_cu_die_offset_given_cu_header_offset; + dwarf_get_cu_die_offset_given_cu_header_offset_b; + dwarf_get_die_infotypes_flag; dwarf_get_elf; dwarf_get_fde_at_pc; dwarf_get_fde_info_for_all_regs3; @@ -139,6 +142,8 @@ global: dwarf_get_relocation_info; dwarf_get_relocation_info_count; dwarf_get_section_bytes; + dwarf_get_section_max_offsets; + dwarf_get_section_max_offsets_b; dwarf_get_str; dwarf_get_types; dwarf_get_vars; @@ -152,6 +157,7 @@ global: dwarf_hasattr; dwarf_hasform; dwarf_highpc; + dwarf_highpc_b; dwarf_init; dwarf_line_srcfileno; dwarf_lineaddr; @@ -166,6 +172,7 @@ global: dwarf_loclist; dwarf_loclist_from_expr; dwarf_loclist_from_expr_a; + dwarf_loclist_from_expr_b; dwarf_loclist_n; dwarf_lowpc; dwarf_new_die; @@ -173,9 +180,12 @@ global: dwarf_new_fde; dwarf_next_cu_header; dwarf_next_cu_header_b; + dwarf_next_cu_header_c; + dwarf_next_types_section; dwarf_object_finish; dwarf_object_init; dwarf_offdie; + dwarf_offdie_b; dwarf_producer_finish; dwarf_producer_init; dwarf_producer_init_b; @@ -196,6 +206,7 @@ global: dwarf_seterrarg; dwarf_seterrhand; dwarf_siblingof; + dwarf_siblingof_b; dwarf_srcfiles; dwarf_srclang; dwarf_srclines; diff --git a/contrib/elftoolchain/libdwarf/_libdwarf.h b/contrib/elftoolchain/libdwarf/_libdwarf.h index 23f60e8ee267..e6eb496feed1 100644 --- a/contrib/elftoolchain/libdwarf/_libdwarf.h +++ b/contrib/elftoolchain/libdwarf/_libdwarf.h @@ -1,6 +1,6 @@ /*- * Copyright (c) 2007 John Birrell (jb@freebsd.org) - * Copyright (c) 2009-2011 Kai Wang + * Copyright (c) 2009-2014 Kai Wang * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -24,7 +24,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: _libdwarf.h 2075 2011-10-27 03:47:28Z jkoshy $ + * $Id: _libdwarf.h 3164 2015-02-19 01:20:12Z kaiwang27 $ */ #ifndef __LIBDWARF_H_ @@ -49,7 +49,7 @@ struct _libdwarf_globals { Dwarf_Handler errhand; Dwarf_Ptr errarg; - int applyrela; + int applyreloc; }; extern struct _libdwarf_globals _libdwarf; @@ -89,6 +89,7 @@ extern struct _libdwarf_globals _libdwarf; goto gen_fail; \ } while(0) +typedef struct _Dwarf_CU *Dwarf_CU; struct _Dwarf_AttrDef { uint64_t ad_attrib; /* DW_AT_XXX */ @@ -147,14 +148,6 @@ struct _Dwarf_Die { STAILQ_ENTRY(_Dwarf_Die) die_pro_next; /* Next die in pro-die list. */ }; -struct _Dwarf_Loclist { - Dwarf_Locdesc **ll_ldlist; /* Array of Locdesc pointer. */ - int ll_ldlen; /* Number of Locdesc. */ - Dwarf_Unsigned ll_offset; /* Offset in .debug_loc section. */ - Dwarf_Unsigned ll_length; /* Length (in bytes) of the loclist. */ - TAILQ_ENTRY(_Dwarf_Loclist) ll_next; /* Next loclist in list. */ -}; - struct _Dwarf_P_Expr_Entry { Dwarf_Loc ee_loc; /* Location expression. */ Dwarf_Unsigned ee_sym; /* Optional related reloc sym index. */ @@ -197,6 +190,7 @@ struct _Dwarf_LineInfo { Dwarf_Half li_version; /* Version of line info. */ Dwarf_Unsigned li_hdrlen; /* Length of line info header. */ Dwarf_Small li_minlen; /* Minimum instrutction length. */ + Dwarf_Small li_maxop; /* Maximum operations per inst. */ Dwarf_Small li_defstmt; /* Default value of is_stmt. */ int8_t li_lbase; /* Line base for special opcode. */ Dwarf_Small li_lrange; /* Line range for special opcode. */ @@ -265,6 +259,8 @@ struct _Dwarf_Cie { Dwarf_Half cie_version; /* CIE version. */ uint8_t *cie_augment; /* CIE augmentation (UTF-8). */ Dwarf_Unsigned cie_ehdata; /* Optional EH Data. */ + uint8_t cie_addrsize; /* Address size. (DWARF4) */ + uint8_t cie_segmentsize; /* Segment size. (DWARF4) */ Dwarf_Unsigned cie_caf; /* Code alignment factor. */ Dwarf_Signed cie_daf; /* Data alignment factor. */ Dwarf_Unsigned cie_ra; /* Return address register. */ @@ -333,11 +329,14 @@ struct _Dwarf_CU { uint64_t cu_lineno_offset; /* Offset into .debug_lineno. */ uint8_t cu_pointer_size;/* Number of bytes in pointer. */ uint8_t cu_dwarf_size; /* CU section dwarf size. */ + Dwarf_Sig8 cu_type_sig; /* Type unit's signature. */ + uint64_t cu_type_offset; /* Type unit's type offset. */ Dwarf_Off cu_next_offset; /* Offset to the next CU. */ uint64_t cu_1st_offset; /* First DIE offset. */ int cu_pass2; /* Two pass DIE traverse. */ Dwarf_LineInfo cu_lineinfo; /* Ptr to Dwarf_LineInfo. */ Dwarf_Abbrev cu_abbrev_hash; /* Abbrev hash table. */ + Dwarf_Bool cu_is_info; /* Compilation/type unit flag. */ STAILQ_ENTRY(_Dwarf_CU) cu_next; /* Next compilation unit. */ }; @@ -399,17 +398,21 @@ struct _Dwarf_Debug { Dwarf_Section *dbg_section; /* Dwarf section list. */ Dwarf_Section *dbg_info_sec; /* Pointer to info section. */ Dwarf_Off dbg_info_off; /* Current info section offset. */ + Dwarf_Section *dbg_types_sec; /* Pointer to type section. */ + Dwarf_Off dbg_types_off; /* Current types section offset. */ Dwarf_Unsigned dbg_seccnt; /* Total number of dwarf sections. */ int dbg_mode; /* Access mode. */ int dbg_pointer_size; /* Object address size. */ int dbg_offset_size; /* DWARF offset size. */ int dbg_info_loaded; /* Flag indicating all CU loaded. */ + int dbg_types_loaded; /* Flag indicating all TU loaded. */ Dwarf_Half dbg_machine; /* ELF machine architecture. */ Dwarf_Handler dbg_errhand; /* Error handler. */ Dwarf_Ptr dbg_errarg; /* Argument to the error handler. */ STAILQ_HEAD(, _Dwarf_CU) dbg_cu;/* List of compilation units. */ + STAILQ_HEAD(, _Dwarf_CU) dbg_tu;/* List of type units. */ Dwarf_CU dbg_cu_current; /* Ptr to the current CU. */ - TAILQ_HEAD(, _Dwarf_Loclist) dbg_loclist; /* List of location list. */ + Dwarf_CU dbg_tu_current; /* Ptr to the current TU. */ Dwarf_NameSec dbg_globals; /* Ptr to pubnames lookup section. */ Dwarf_NameSec dbg_pubtypes; /* Ptr to pubtypes lookup section. */ Dwarf_NameSec dbg_weaks; /* Ptr to weaknames lookup section. */ @@ -532,13 +535,15 @@ int _dwarf_elf_get_section_info(void *, Dwarf_Half, Dwarf_Obj_Access_Section *, int *); void _dwarf_expr_cleanup(Dwarf_P_Debug); int _dwarf_expr_into_block(Dwarf_P_Expr, Dwarf_Error *); +Dwarf_Section *_dwarf_find_next_types_section(Dwarf_Debug, Dwarf_Section *); Dwarf_Section *_dwarf_find_section(Dwarf_Debug, const char *); void _dwarf_frame_cleanup(Dwarf_Debug); int _dwarf_frame_fde_add_inst(Dwarf_P_Fde, Dwarf_Small, Dwarf_Unsigned, Dwarf_Unsigned, Dwarf_Error *); int _dwarf_frame_gen(Dwarf_P_Debug, Dwarf_Error *); -int _dwarf_frame_get_fop(Dwarf_Debug, uint8_t *, Dwarf_Unsigned, - Dwarf_Frame_Op **, Dwarf_Signed *, Dwarf_Error *); +int _dwarf_frame_get_fop(Dwarf_Debug, uint8_t, uint8_t *, + Dwarf_Unsigned, Dwarf_Frame_Op **, Dwarf_Signed *, + Dwarf_Error *); int _dwarf_frame_get_internal_table(Dwarf_Fde, Dwarf_Addr, Dwarf_Regtable3 **, Dwarf_Addr *, Dwarf_Error *); int _dwarf_frame_interal_table_init(Dwarf_Debug, Dwarf_Error *); @@ -553,9 +558,12 @@ Dwarf_Unsigned _dwarf_get_reloc_type(Dwarf_P_Debug, int); int _dwarf_get_reloc_size(Dwarf_Debug, Dwarf_Unsigned); void _dwarf_info_cleanup(Dwarf_Debug); int _dwarf_info_first_cu(Dwarf_Debug, Dwarf_Error *); +int _dwarf_info_first_tu(Dwarf_Debug, Dwarf_Error *); int _dwarf_info_gen(Dwarf_P_Debug, Dwarf_Error *); -int _dwarf_info_load(Dwarf_Debug, int, Dwarf_Error *); +int _dwarf_info_load(Dwarf_Debug, Dwarf_Bool, Dwarf_Bool, + Dwarf_Error *); int _dwarf_info_next_cu(Dwarf_Debug, Dwarf_Error *); +int _dwarf_info_next_tu(Dwarf_Debug, Dwarf_Error *); void _dwarf_info_pro_cleanup(Dwarf_P_Debug); int _dwarf_init(Dwarf_Debug, Dwarf_Unsigned, Dwarf_Handler, Dwarf_Ptr, Dwarf_Error *); @@ -563,20 +571,19 @@ int _dwarf_lineno_gen(Dwarf_P_Debug, Dwarf_Error *); int _dwarf_lineno_init(Dwarf_Die, uint64_t, Dwarf_Error *); void _dwarf_lineno_cleanup(Dwarf_LineInfo); void _dwarf_lineno_pro_cleanup(Dwarf_P_Debug); -int _dwarf_loc_fill_locdesc(Dwarf_Debug, Dwarf_Locdesc *, uint8_t *, - uint64_t, uint8_t, Dwarf_Error *); +int _dwarf_loc_fill_locdesc(Dwarf_Debug, Dwarf_Locdesc *, + uint8_t *, uint64_t, uint8_t, uint8_t, uint8_t, + Dwarf_Error *); int _dwarf_loc_fill_locexpr(Dwarf_Debug, Dwarf_Locdesc **, - uint8_t *, uint64_t, uint8_t, Dwarf_Error *); + uint8_t *, uint64_t, uint8_t, uint8_t, uint8_t, + Dwarf_Error *); int _dwarf_loc_add(Dwarf_Die, Dwarf_Attribute, Dwarf_Error *); int _dwarf_loc_expr_add_atom(Dwarf_Debug, uint8_t *, uint8_t *, Dwarf_Small, Dwarf_Unsigned, Dwarf_Unsigned, int *, Dwarf_Error *); int _dwarf_loclist_find(Dwarf_Debug, Dwarf_CU, uint64_t, - Dwarf_Loclist *, Dwarf_Error *); -void _dwarf_loclist_cleanup(Dwarf_Debug); -void _dwarf_loclist_free(Dwarf_Loclist); -int _dwarf_loclist_add(Dwarf_Debug, Dwarf_CU, uint64_t, - Dwarf_Loclist *, Dwarf_Error *); + Dwarf_Locdesc ***, Dwarf_Signed *, Dwarf_Unsigned *, + Dwarf_Error *); void _dwarf_macinfo_cleanup(Dwarf_Debug); int _dwarf_macinfo_gen(Dwarf_P_Debug, Dwarf_Error *); int _dwarf_macinfo_init(Dwarf_Debug, Dwarf_Error *); @@ -633,6 +640,7 @@ void _dwarf_strtab_cleanup(Dwarf_Debug); int _dwarf_strtab_gen(Dwarf_P_Debug, Dwarf_Error *); char *_dwarf_strtab_get_table(Dwarf_Debug); int _dwarf_strtab_init(Dwarf_Debug, Dwarf_Error *); +void _dwarf_type_unit_cleanup(Dwarf_Debug); void _dwarf_write_block(void *, uint64_t *, uint8_t *, uint64_t); int _dwarf_write_block_alloc(uint8_t **, uint64_t *, uint64_t *, uint8_t *, uint64_t, Dwarf_Error *); diff --git a/contrib/elftoolchain/libdwarf/dwarf.3 b/contrib/elftoolchain/libdwarf/dwarf.3 index b32299cd1a11..dbb417908094 100644 --- a/contrib/elftoolchain/libdwarf/dwarf.3 +++ b/contrib/elftoolchain/libdwarf/dwarf.3 @@ -21,9 +21,9 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: dwarf.3 2075 2011-10-27 03:47:28Z jkoshy $ +.\" $Id: dwarf.3 3195 2015-05-12 17:22:19Z emaste $ .\" -.Dd September 17, 2011 +.Dd December 21, 2014 .Os .Dt DWARF 3 .Sh NAME @@ -44,7 +44,7 @@ is defined by the DWARF standard, see .Xr dwarf 4 . .Pp The -.Xr DWARF 3 +.Xr DWARF 3 API has two parts: .Bl -bullet .It @@ -217,6 +217,8 @@ attribute. Retrieve an attribute descriptor. .It Fn dwarf_attrlist Retrieve attribute descriptors for a debugging information entry. +.It Fn dwarf_attroffset +Retrieve the section-relative offset of an attribute descriptor. .It Fn dwarf_attrval_flag Retrieve a .Dv DW_AT_FORM_flag @@ -309,10 +311,17 @@ Retrieve range information from an FDE descriptor. .El .It Compilation Units .Bl -tag -compact -.It Fn dwarf_get_cu_die_offset_given_cu_header_offset +.It Xo +.Fn dwarf_get_cu_die_offset_given_cu_header_offset , +.Fn dwarf_get_cu_die_offset_given_cu_header_offset_b +.Xc Retrieve the offset of the debugging information entry for a -compilation unit. -.It Fn dwarf_next_cu_header , Fn dwarf_next_cu_header_b +compilation or type unit. +.It Xo +.Fn dwarf_next_cu_header , +.Fn dwarf_next_cu_header_b , +.Fn dwarf_next_cu_header_c +.Xc Step through compilation units in a debug context. .El .It Debugging Information Entries @@ -329,16 +338,18 @@ Returns the attribute for a debugging information entry. .It Fn dwarf_dieoffset Retrieves the offset for a debugging information entry. -.It Fn dwarf_highpc +.It Fn dwarf_get_die_infotypes_flag +Indicate the originating section for a debugging information entry. +.It Fn dwarf_highpc , Fn dwarf_highpc_b Return the highest PC value for a debugging information entry. .It Fn dwarf_lowpc Return the lowest PC value for a debugging information entry. -.It Fn dwarf_offdie +.It Fn dwarf_offdie , Fn dwarf_offdie_b Retrieve a debugging information entry given an offset. -.It Fn dwarf_siblingof +.It Fn dwarf_siblingof , Fn dwarf_siblingof_b Retrieve the sibling descriptor for a debugging information entry. .It Fn dwarf_srclang -Retrive the source language attribute for a debugging information +Retrieve the source language attribute for a debugging information entry. .It Fn dwarf_tag Retrieve the tag for a debugging information entry. @@ -416,7 +427,11 @@ Return line number information for a compilation unit. Retrieve a location list entry. .It Fn dwarf_loclist , Fn dwarf_loclist_n Retrieve location expressions. -.It Fn dwarf_loclist_from_expr , Fn dwarf_loclist_from_expr_a +.It Xo +.Fn dwarf_loclist_from_expr , +.Fn dwarf_loclist_from_expr_a , +.Fn dwarf_loclist_from_expr_b +.Xc Translate a location expression into a location descriptor. .El .It Error Handling @@ -513,6 +528,10 @@ and .Bl -tag -compact .It Fn dwarf_get_pubtypes , Fn dwarf_get_types Retrieve descriptors for user-defined types. +.It Fn dwarf_next_types_section +Step through +.Dq \&.debug_types +sections in a debug context. .It Fn dwarf_pubtype_cu_offset , Fn dwarf_type_cu_offset Return the offset for the compilation unit for a type. .It Fn dwarf_pubtype_die_offset , Fn dwarf_type_die_offset @@ -699,9 +718,16 @@ addition to the per-debug context handlers supported by the SGI/GNU API, see the subsection .Sx Error Handling above. +.El +.Ss Extensions +The following APIs are extensions specific to this implementation: +.Bl -bullet -compact +.It +.Fn dwarf_attroffset .It -The following API is an extension: -.Fn dwarf_producer_set_isa . +.Fn dwarf_next_types_section +.It +.Fn dwarf_producer_set_isa .El .Sh SEE ALSO .Xr elf 3 @@ -716,12 +742,12 @@ The DWARF standard is defined by The DWARF(3) API originated at Silicon Graphics Inc. .Pp A BSD-licensed implementation of a subset of the API was written by -.An "John Birrell" Aq jb@FreeBSD.org +.An John Birrell Aq Mt jb@FreeBSD.org for the FreeBSD project. The implementation was subsequently revised and completed by -.An "Kai Wang" Aq kaiwang27@users.sourceforge.net . +.An Kai Wang Aq Mt kaiwang27@users.sourceforge.net . .Pp Manual pages for this implementation were written by -.An "Joseph Koshy" Aq jkoshy@users.sourceforge.net +.An Joseph Koshy Aq Mt jkoshy@users.sourceforge.net and -.An "Kai Wang" Aq kaiwang27@users.sourceforge.net . +.An Kai Wang Aq Mt kaiwang27@users.sourceforge.net . diff --git a/contrib/elftoolchain/libdwarf/dwarf.h b/contrib/elftoolchain/libdwarf/dwarf.h index b1a3e4ec9670..c79da96dbb47 100644 --- a/contrib/elftoolchain/libdwarf/dwarf.h +++ b/contrib/elftoolchain/libdwarf/dwarf.h @@ -23,7 +23,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: dwarf.h 2075 2011-10-27 03:47:28Z jkoshy $ + * $Id: dwarf.h 3052 2014-05-26 20:36:24Z kaiwang27 $ */ #ifndef _DWARF_H_ @@ -93,6 +93,19 @@ #define DW_TAG_lo_user 0x4080 #define DW_TAG_hi_user 0xffff +/* GNU extensions. */ +#define DW_TAG_format_label 0x4101 +#define DW_TAG_function_template 0x4102 +#define DW_TAG_class_template 0x4103 +#define DW_TAG_GNU_BINCL 0x4104 +#define DW_TAG_GNU_EINCL 0x4105 +#define DW_TAG_GNU_template_template_parameter 0x4106 +#define DW_TAG_GNU_template_template_param 0x4106 +#define DW_TAG_GNU_template_parameter_pack 0x4107 +#define DW_TAG_GNU_formal_parameter_pack 0x4108 +#define DW_TAG_GNU_call_site 0x4109 +#define DW_TAG_GNU_call_site_parameter 0x410a + #define DW_CHILDREN_no 0x00 #define DW_CHILDREN_yes 0x01 @@ -195,6 +208,32 @@ #define DW_AT_lo_user 0x2000 #define DW_AT_hi_user 0x3fff +/* GNU extensions. */ +#define DW_AT_sf_names 0x2101 +#define DW_AT_src_info 0x2102 +#define DW_AT_mac_info 0x2103 +#define DW_AT_src_coords 0x2104 +#define DW_AT_body_begin 0x2105 +#define DW_AT_body_end 0x2106 +#define DW_AT_GNU_vector 0x2107 +#define DW_AT_GNU_guarded_by 0x2108 +#define DW_AT_GNU_pt_guarded_by 0x2109 +#define DW_AT_GNU_guarded 0x210a +#define DW_AT_GNU_pt_guarded 0x210b +#define DW_AT_GNU_locks_excluded 0x210c +#define DW_AT_GNU_exclusive_locks_required 0x210d +#define DW_AT_GNU_shared_locks_required 0x210e +#define DW_AT_GNU_odr_signature 0x210f +#define DW_AT_GNU_template_name 0x2110 +#define DW_AT_GNU_call_site_value 0x2111 +#define DW_AT_GNU_call_site_data_value 0x2112 +#define DW_AT_GNU_call_site_target 0x2113 +#define DW_AT_GNU_call_site_target_clobbered 0x2114 +#define DW_AT_GNU_tail_call 0x2115 +#define DW_AT_GNU_all_tail_call_sites 0x2116 +#define DW_AT_GNU_all_call_sites 0x2117 +#define DW_AT_GNU_all_source_call_sites 0x2118 + #define DW_FORM_addr 0x01 #define DW_FORM_block2 0x03 #define DW_FORM_block4 0x04 @@ -220,6 +259,8 @@ #define DW_FORM_exprloc 0x18 #define DW_FORM_flag_present 0x19 #define DW_FORM_ref_sig8 0x20 +#define DW_FORM_GNU_ref_alt 0x1f20 +#define DW_FORM_GNU_strp_alt 0x1f21 #define DW_OP_addr 0x03 #define DW_OP_deref 0x06 @@ -376,9 +417,23 @@ #define DW_OP_implicit_value 0x9e #define DW_OP_stack_value 0x9f #define DW_OP_lo_user 0xe0 -#define DW_OP_GNU_push_tls_address 0xe0 #define DW_OP_hi_user 0xff +/* GNU extensions. */ +#define DW_OP_GNU_push_tls_address 0xe0 +#define DW_OP_GNU_uninit 0xf0 +#define DW_OP_GNU_encoded_addr 0xf1 +#define DW_OP_GNU_implicit_pointer 0xf2 +#define DW_OP_GNU_entry_value 0xf3 +#define DW_OP_GNU_const_type 0xf4 +#define DW_OP_GNU_regval_type 0xf5 +#define DW_OP_GNU_deref_type 0xf6 +#define DW_OP_GNU_convert 0xf7 +#define DW_OP_GNU_reinterpret 0xf9 +#define DW_OP_GNU_parameter_ref 0xfa +#define DW_OP_GNU_addr_index 0xfb +#define DW_OP_GNU_const_index 0xfc + #define DW_ATE_address 0x1 #define DW_ATE_boolean 0x2 #define DW_ATE_complex_float 0x3 diff --git a/contrib/elftoolchain/libdwarf/dwarf_add_line_entry.3 b/contrib/elftoolchain/libdwarf/dwarf_add_line_entry.3 index 22377176245a..16e807512a44 100644 --- a/contrib/elftoolchain/libdwarf/dwarf_add_line_entry.3 +++ b/contrib/elftoolchain/libdwarf/dwarf_add_line_entry.3 @@ -22,7 +22,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.\" $Id: dwarf_add_line_entry.3 2953 2013-06-30 20:21:38Z kaiwang27 $ +.\" $Id: dwarf_add_line_entry.3 3182 2015-04-10 16:08:10Z emaste $ .\" .Dd June 30, 2013 .Os @@ -66,7 +66,8 @@ Valid source file indices are those returned by the function .Pp Argument .Ar off -specifies a relocatable program address. The ELF symbol to be used +specifies a relocatable program address. +The ELF symbol to be used for relocation is set by a prior call to the function .Xr dwarf_lne_set_address 3 . .Pp diff --git a/contrib/elftoolchain/libdwarf/dwarf_attr.3 b/contrib/elftoolchain/libdwarf/dwarf_attr.3 index b1e30017a89a..9ba367be5f69 100644 --- a/contrib/elftoolchain/libdwarf/dwarf_attr.3 +++ b/contrib/elftoolchain/libdwarf/dwarf_attr.3 @@ -22,7 +22,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.\" $Id: dwarf_attr.3 2072 2011-10-27 03:26:49Z jkoshy $ +.\" $Id: dwarf_attr.3 3093 2014-09-02 22:09:40Z kaiwang27 $ .\" .Dd April 8, 2010 .Os @@ -113,6 +113,7 @@ in argument .Sh SEE ALSO .Xr dwarf 3 , .Xr dwarf_attrlist 3 , +.Xr dwarf_attroffset 3 , .Xr dwarf_hasattr 3 , .Xr dwarf_hasform 3 , .Xr dwarf_whatattr 3 , diff --git a/contrib/elftoolchain/libdwarf/dwarf_attr.c b/contrib/elftoolchain/libdwarf/dwarf_attr.c index ce40294c6b12..a081d7b93a8f 100644 --- a/contrib/elftoolchain/libdwarf/dwarf_attr.c +++ b/contrib/elftoolchain/libdwarf/dwarf_attr.c @@ -27,7 +27,7 @@ #include "_libdwarf.h" -ELFTC_VCSID("$Id: dwarf_attr.c 2072 2011-10-27 03:26:49Z jkoshy $"); +ELFTC_VCSID("$Id: dwarf_attr.c 3064 2014-06-06 19:35:55Z kaiwang27 $"); int dwarf_attr(Dwarf_Die die, Dwarf_Half attr, Dwarf_Attribute *atp, @@ -114,6 +114,23 @@ dwarf_hasattr(Dwarf_Die die, Dwarf_Half attr, Dwarf_Bool *ret_bool, } int +dwarf_attroffset(Dwarf_Attribute at, Dwarf_Off *ret_off, Dwarf_Error *error) +{ + Dwarf_Debug dbg; + + dbg = at != NULL ? at->at_die->die_dbg : NULL; + + if (at == NULL || ret_off == NULL) { + DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT); + return (DW_DLV_ERROR); + } + + *ret_off = at->at_offset; + + return (DW_DLV_OK); +} + +int dwarf_lowpc(Dwarf_Die die, Dwarf_Addr *ret_lowpc, Dwarf_Error *error) { Dwarf_Attribute at; @@ -139,8 +156,17 @@ dwarf_lowpc(Dwarf_Die die, Dwarf_Addr *ret_lowpc, Dwarf_Error *error) int dwarf_highpc(Dwarf_Die die, Dwarf_Addr *ret_highpc, Dwarf_Error *error) { + + return (dwarf_highpc_b(die, ret_highpc, NULL, NULL, error)); +} + +int +dwarf_highpc_b(Dwarf_Die die, Dwarf_Addr *ret_highpc, Dwarf_Half *ret_form, + enum Dwarf_Form_Class *ret_class, Dwarf_Error *error) +{ Dwarf_Attribute at; Dwarf_Debug dbg; + Dwarf_CU cu; dbg = die != NULL ? die->die_dbg : NULL; @@ -156,6 +182,17 @@ dwarf_highpc(Dwarf_Die die, Dwarf_Addr *ret_highpc, Dwarf_Error *error) *ret_highpc = at->u[0].u64; + if (ret_form != NULL) { + *ret_form = at->at_form; + } + + if (ret_class != NULL) { + cu = die->die_cu; + *ret_class = dwarf_get_form_class(cu->cu_version, + DW_AT_high_pc, cu->cu_length_size == 4 ? 4 : 8, + at->at_form); + } + return (DW_DLV_OK); } diff --git a/contrib/elftoolchain/libdwarf/dwarf_attroffset.3 b/contrib/elftoolchain/libdwarf/dwarf_attroffset.3 new file mode 100644 index 000000000000..af24cebab8e9 --- /dev/null +++ b/contrib/elftoolchain/libdwarf/dwarf_attroffset.3 @@ -0,0 +1,86 @@ +.\" Copyright (c) 2014 Kai Wang +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (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: dwarf_attroffset.3 3115 2014-12-20 18:26:46Z jkoshy $ +.\" +.Dd December 20, 2014 +.Os +.Dt DWARF_ATTROFFSET 3 +.Sh NAME +.Nm dwarf_attroffset +.Nd retrieve the section-relative offset of an attribute descriptor +.Sh LIBRARY +.Lb libdwarf +.Sh SYNOPSIS +.In libdwarf.h +.Ft int +.Fo dwarf_attroffset +.Fa "Dwarf_Attribute at" +.Fa "Dwarf_Off *ret_off" +.Fa "Dwarf_Error *err" +.Fc +.Sh DESCRIPTION +Function +.Fn dwarf_attroffset +retrieves the section-relative offset of the attribute descriptor +referenced by argument +.Ar at . +.Pp +Argument +.Ar ret_off +should point to a location that is to hold the returned +section-relative offset. +If argument +.Ar err +is non-NULL, it is used to return an error descriptor in case of an +error. +.Sh RETURN VALUES +On success, function +.Fn dwarf_attroffset +returns +.Dv DW_DLV_OK . +.Pp +In case of an error, it returns +.Dv DW_DLV_ERROR +and sets argument +.Ar err . +.Sh COMPATIBILITY +This function is an extension to the +.Xr DWARF 3 +API. +.Sh ERRORS +The +.Fn dwarf_attroffset +function may fail with the following errors: +.Bl -tag -width ".Bq Er DW_DLE_ARGUMENT" +.It Bq Er DW_DLE_ARGUMENT +Either of the arguments +.Ar at +or +.Ar ret_off +was NULL. +.El +.Sh SEE ALSO +.Xr dwarf 3 , +.Xr dwarf_attr 3 diff --git a/contrib/elftoolchain/libdwarf/dwarf_attrval.c b/contrib/elftoolchain/libdwarf/dwarf_attrval.c index 31d19f7b8b21..0dd38a491d84 100644 --- a/contrib/elftoolchain/libdwarf/dwarf_attrval.c +++ b/contrib/elftoolchain/libdwarf/dwarf_attrval.c @@ -26,7 +26,7 @@ #include "_libdwarf.h" -ELFTC_VCSID("$Id: dwarf_attrval.c 2072 2011-10-27 03:26:49Z jkoshy $"); +ELFTC_VCSID("$Id: dwarf_attrval.c 3159 2015-02-15 21:43:27Z emaste $"); int dwarf_attrval_flag(Dwarf_Die die, Dwarf_Half attr, Dwarf_Bool *valp, Dwarf_Error *err) @@ -125,6 +125,7 @@ dwarf_attrval_signed(Dwarf_Die die, Dwarf_Half attr, Dwarf_Signed *valp, Dwarf_E break; case DW_FORM_data4: *valp = (int32_t) at->u[0].s64; + break; case DW_FORM_data8: case DW_FORM_sdata: *valp = at->u[0].s64; diff --git a/contrib/elftoolchain/libdwarf/dwarf_attrval_signed.3 b/contrib/elftoolchain/libdwarf/dwarf_attrval_signed.3 index 3fd01d0c60f5..93d4ae0596ed 100644 --- a/contrib/elftoolchain/libdwarf/dwarf_attrval_signed.3 +++ b/contrib/elftoolchain/libdwarf/dwarf_attrval_signed.3 @@ -22,7 +22,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.\" $Id: dwarf_attrval_signed.3 2072 2011-10-27 03:26:49Z jkoshy $ +.\" $Id: dwarf_attrval_signed.3 2980 2014-01-21 20:15:54Z kaiwang27 $ .\" .Dd January 18, 2014 .Os diff --git a/contrib/elftoolchain/libdwarf/dwarf_child.3 b/contrib/elftoolchain/libdwarf/dwarf_child.3 index 57549c01c66e..1e32e58d4451 100644 --- a/contrib/elftoolchain/libdwarf/dwarf_child.3 +++ b/contrib/elftoolchain/libdwarf/dwarf_child.3 @@ -1,4 +1,4 @@ -.\" Copyright (c) 2010 Kai Wang +.\" Copyright (c) 2010,2014 Kai Wang .\" All rights reserved. .\" .\" Redistribution and use in source and binary forms, with or without @@ -22,15 +22,17 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.\" $Id: dwarf_child.3 2122 2011-11-09 15:35:14Z jkoshy $ +.\" $Id: dwarf_child.3 3127 2014-12-21 19:09:19Z jkoshy $ .\" -.Dd November 9, 2011 +.Dd December 21, 2014 .Os .Dt DWARF_CHILD 3 .Sh NAME .Nm dwarf_child , +.Nm dwarf_offdie , +.Nm dwarf_offdie_b , .Nm dwarf_siblingof , -.Nm dwarf_offdie +.Nm dwarf_siblingof_b .Nd retrieve DWARF Debugging Information Entry descriptors .Sh LIBRARY .Lb libdwarf @@ -39,6 +41,21 @@ .Ft int .Fn dwarf_child "Dwarf_Die die" "Dwarf_Die *ret_die" "Dwarf_Error *err" .Ft int +.Fo dwarf_offdie +.Fa "Dwarf_Debug dbg" +.Fa "Dwarf_Off offset" +.Fa "Dwarf_Die *ret_die" +.Fa "Dwarf_Error *err" +.Fc +.Ft int +.Fo dwarf_offdie_b +.Fa "Dwarf_Debug dbg" +.Fa "Dwarf_Off offset" +.Fa "Dwarf_Bool is_info" +.Fa "Dwarf_Die *ret_die" +.Fa "Dwarf_Error *err" +.Fc +.Ft int .Fo dwarf_siblingof .Fa "Dwarf_Debug dbg" .Fa "Dwarf_Die die" @@ -46,10 +63,11 @@ .Fa "Dwarf_Error *err" .Fc .Ft int -.Fo dwarf_offdie +.Fo dwarf_siblingof_b .Fa "Dwarf_Debug dbg" -.Fa "Dwarf_Off offset" +.Fa "Dwarf_Die die" .Fa "Dwarf_Die *ret_die" +.Fa "Dwarf_Bool is_info" .Fa "Dwarf_Error *err" .Fc .Sh DESCRIPTION @@ -92,6 +110,34 @@ may be used together to traverse the tree of debugging information entry descriptors for a compilation unit. .Pp Function +.Fn dwarf_siblingof_b +is identical to the function +.Fn dwarf_siblingof +except that it can retrieve the sibling descriptor from either the +current compilation unit or type unit. +If argument +.Ar is_info +is non-zero, the function behaves identically to function +.Fn dwarf_siblingof . +If argument +.Ar is_info +is zero, the descriptor referred by argument +.Ar die +should be associated with a debugging information entry in the +type unit. +The function will store the sibling of the descriptor in the location +pointed to by argument +.Ar ret_die . +If argument +.Ar is_info +is zero and argument +.Ar die +is +.Dv NULL , +the first debugging information entry descriptor for the +current type unit will be returned. +.Pp +Function .Fn dwarf_offdie retrieves the debugging information entry descriptor at global offset .Ar offset @@ -101,6 +147,31 @@ section of the object associated with argument .Ar dbg . The returned descriptor is written to the location pointed to by argument .Ar ret_die . +.Pp +Function +.Fn dwarf_offdie_b +is identical to the function +.Fn dwarf_offdie +except that it can retrieve the debugging information entry descriptor at +global offset +.Ar offset +from either of the +.Dq .debug_info +and +.Dq .debug_types +sections of the object associated with argument +.Ar dbg . +If argument +.Ar is_info +is non-zero, the function will retrieve the debugging information +entry from the +.Dq .debug_info +section, otherwise the function will retrieve the debugging +information entry from the +.Dq .debug_types +section. +The returned descriptor is written to the location pointed to by argument +.Ar ret_die . .Ss Memory Management The memory area used for the .Vt Dwarf_Die @@ -128,14 +199,18 @@ argument if it is not NULL. .It Bq Er DW_DLV_NO_ENTRY For functions -.Fn dwarf_child +.Fn dwarf_child , +.Fn dwarf_siblingof and -.Fn dwarf_siblingof , +.Fn dwarf_siblingof_b , the descriptor denoted by argument .Ar die did not have a child or sibling. -For function -.Fn dwarf_offdie , +.Pp +For functions +.Fn dwarf_offdie +and +.Fn dwarf_offdie_b , there was no debugging information entry at the offset specified by argument .Ar offset . @@ -199,4 +274,5 @@ do { .Sh SEE ALSO .Xr dwarf 3 , .Xr dwarf_errmsg 3 , +.Xr dwarf_get_die_infotypes_flag.3 , .Xr dwarf_next_cu_header 3 diff --git a/contrib/elftoolchain/libdwarf/dwarf_cu.c b/contrib/elftoolchain/libdwarf/dwarf_cu.c index c203dc2f1fc7..d103488d1098 100644 --- a/contrib/elftoolchain/libdwarf/dwarf_cu.c +++ b/contrib/elftoolchain/libdwarf/dwarf_cu.c @@ -1,5 +1,6 @@ /*- * Copyright (c) 2007 John Birrell (jb@freebsd.org) + * Copyright (c) 2014 Kai Wang * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -26,14 +27,15 @@ #include "_libdwarf.h" -ELFTC_VCSID("$Id: dwarf_cu.c 2072 2011-10-27 03:26:49Z jkoshy $"); +ELFTC_VCSID("$Id: dwarf_cu.c 3041 2014-05-18 15:11:03Z kaiwang27 $"); int -dwarf_next_cu_header_b(Dwarf_Debug dbg, Dwarf_Unsigned *cu_length, - Dwarf_Half *cu_version, Dwarf_Off *cu_abbrev_offset, - Dwarf_Half *cu_pointer_size, Dwarf_Half *cu_offset_size, - Dwarf_Half *cu_extension_size, Dwarf_Unsigned *cu_next_offset, - Dwarf_Error *error) +dwarf_next_cu_header_c(Dwarf_Debug dbg, Dwarf_Bool is_info, + Dwarf_Unsigned *cu_length, Dwarf_Half *cu_version, + Dwarf_Off *cu_abbrev_offset, Dwarf_Half *cu_pointer_size, + Dwarf_Half *cu_offset_size, Dwarf_Half *cu_extension_size, + Dwarf_Sig8 *type_signature, Dwarf_Unsigned *type_offset, + Dwarf_Unsigned *cu_next_offset, Dwarf_Error *error) { Dwarf_CU cu; int ret; @@ -43,10 +45,17 @@ dwarf_next_cu_header_b(Dwarf_Debug dbg, Dwarf_Unsigned *cu_length, return (DW_DLV_ERROR); } - if (dbg->dbg_cu_current == NULL) - ret = _dwarf_info_first_cu(dbg, error); - else - ret = _dwarf_info_next_cu(dbg, error); + if (is_info) { + if (dbg->dbg_cu_current == NULL) + ret = _dwarf_info_first_cu(dbg, error); + else + ret = _dwarf_info_next_cu(dbg, error); + } else { + if (dbg->dbg_tu_current == NULL) + ret = _dwarf_info_first_tu(dbg, error); + else + ret = _dwarf_info_next_tu(dbg, error); + } if (ret == DW_DLE_NO_ENTRY) { DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY); @@ -54,11 +63,19 @@ dwarf_next_cu_header_b(Dwarf_Debug dbg, Dwarf_Unsigned *cu_length, } else if (ret != DW_DLE_NONE) return (DW_DLV_ERROR); - if (dbg->dbg_cu_current == NULL) { - DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY); - return (DW_DLV_NO_ENTRY); + if (is_info) { + if (dbg->dbg_cu_current == NULL) { + DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY); + return (DW_DLV_NO_ENTRY); + } + cu = dbg->dbg_cu_current; + } else { + if (dbg->dbg_tu_current == NULL) { + DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY); + return (DW_DLV_NO_ENTRY); + } + cu = dbg->dbg_tu_current; } - cu = dbg->dbg_cu_current; if (cu_length) *cu_length = cu->cu_length; @@ -81,11 +98,32 @@ dwarf_next_cu_header_b(Dwarf_Debug dbg, Dwarf_Unsigned *cu_length, *cu_extension_size = 4; } if (cu_next_offset) - *cu_next_offset = dbg->dbg_cu_current->cu_next_offset; + *cu_next_offset = cu->cu_next_offset; + + if (!is_info) { + if (type_signature) + *type_signature = cu->cu_type_sig; + if (type_offset) + *type_offset = cu->cu_type_offset; + } return (DW_DLV_OK); } + +int +dwarf_next_cu_header_b(Dwarf_Debug dbg, Dwarf_Unsigned *cu_length, + Dwarf_Half *cu_version, Dwarf_Off *cu_abbrev_offset, + Dwarf_Half *cu_pointer_size, Dwarf_Half *cu_offset_size, + Dwarf_Half *cu_extension_size, Dwarf_Unsigned *cu_next_offset, + Dwarf_Error *error) +{ + + return (dwarf_next_cu_header_c(dbg, 1, cu_length, cu_version, + cu_abbrev_offset, cu_pointer_size, cu_offset_size, + cu_extension_size, NULL, NULL, cu_next_offset, error)); +} + int dwarf_next_cu_header(Dwarf_Debug dbg, Dwarf_Unsigned *cu_length, Dwarf_Half *cu_version, Dwarf_Off *cu_abbrev_offset, @@ -97,3 +135,27 @@ dwarf_next_cu_header(Dwarf_Debug dbg, Dwarf_Unsigned *cu_length, cu_abbrev_offset, cu_pointer_size, NULL, NULL, cu_next_offset, error)); } + +int +dwarf_next_types_section(Dwarf_Debug dbg, Dwarf_Error *error) +{ + + /* Free resource allocated for current .debug_types section. */ + _dwarf_type_unit_cleanup(dbg); + dbg->dbg_types_loaded = 0; + dbg->dbg_types_off = 0; + + /* Reset type unit pointer. */ + dbg->dbg_tu_current = NULL; + + /* Search for the next .debug_types section. */ + dbg->dbg_types_sec = _dwarf_find_next_types_section(dbg, + dbg->dbg_types_sec); + + if (dbg->dbg_types_sec == NULL) { + DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY); + return (DW_DLV_NO_ENTRY); + } + + return (DW_DLV_OK); +} diff --git a/contrib/elftoolchain/libdwarf/dwarf_def_macro.3 b/contrib/elftoolchain/libdwarf/dwarf_def_macro.3 index d4849b12d1ca..daa090f29f0a 100644 --- a/contrib/elftoolchain/libdwarf/dwarf_def_macro.3 +++ b/contrib/elftoolchain/libdwarf/dwarf_def_macro.3 @@ -22,7 +22,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.\" $Id: dwarf_def_macro.3 2122 2011-11-09 15:35:14Z jkoshy $ +.\" $Id: dwarf_def_macro.3 3182 2015-04-10 16:08:10Z emaste $ .\" .Dd November 9, 2011 .Os @@ -72,7 +72,7 @@ Argument .Ar value should point to a NUL-terminated string containing the value of the macro. -If the macro doesn't have a value, argument +If the macro does not have a value, argument .Ar value should be set to NULL. .Pp diff --git a/contrib/elftoolchain/libdwarf/dwarf_die.c b/contrib/elftoolchain/libdwarf/dwarf_die.c index 055d95209f07..de6351ac608f 100644 --- a/contrib/elftoolchain/libdwarf/dwarf_die.c +++ b/contrib/elftoolchain/libdwarf/dwarf_die.c @@ -1,6 +1,6 @@ /*- * Copyright (c) 2007 John Birrell (jb@freebsd.org) - * Copyright (c) 2009,2011 Kai Wang + * Copyright (c) 2009,2011,2014 Kai Wang * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -27,12 +27,13 @@ #include "_libdwarf.h" -ELFTC_VCSID("$Id: dwarf_die.c 2073 2011-10-27 03:30:47Z jkoshy $"); +ELFTC_VCSID("$Id: dwarf_die.c 3039 2014-05-18 15:10:56Z kaiwang27 $"); int dwarf_child(Dwarf_Die die, Dwarf_Die *ret_die, Dwarf_Error *error) { Dwarf_Debug dbg; + Dwarf_Section *ds; Dwarf_CU cu; int ret; @@ -48,9 +49,9 @@ dwarf_child(Dwarf_Die die, Dwarf_Die *ret_die, Dwarf_Error *error) dbg = die->die_dbg; cu = die->die_cu; - ret = _dwarf_die_parse(die->die_dbg, dbg->dbg_info_sec, cu, - cu->cu_dwarf_size, die->die_next_off, cu->cu_next_offset, - ret_die, 0, error); + ds = cu->cu_is_info ? dbg->dbg_info_sec : dbg->dbg_types_sec; + ret = _dwarf_die_parse(die->die_dbg, ds, cu, cu->cu_dwarf_size, + die->die_next_off, cu->cu_next_offset, ret_die, 0, error); if (ret == DW_DLE_NO_ENTRY) { DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY); @@ -62,11 +63,12 @@ dwarf_child(Dwarf_Die die, Dwarf_Die *ret_die, Dwarf_Error *error) } int -dwarf_siblingof(Dwarf_Debug dbg, Dwarf_Die die, Dwarf_Die *ret_die, - Dwarf_Error *error) +dwarf_siblingof_b(Dwarf_Debug dbg, Dwarf_Die die, Dwarf_Die *ret_die, + Dwarf_Bool is_info, Dwarf_Error *error) { Dwarf_CU cu; Dwarf_Attribute at; + Dwarf_Section *ds; uint64_t offset; int ret, search_sibling; @@ -75,15 +77,27 @@ dwarf_siblingof(Dwarf_Debug dbg, Dwarf_Die die, Dwarf_Die *ret_die, return (DW_DLV_ERROR); } - if ((cu = dbg->dbg_cu_current) == NULL) { + ds = is_info ? dbg->dbg_info_sec : dbg->dbg_types_sec; + cu = is_info ? dbg->dbg_cu_current : dbg->dbg_tu_current; + + if (cu == NULL) { DWARF_SET_ERROR(dbg, error, DW_DLE_DIE_NO_CU_CONTEXT); return (DW_DLV_ERROR); } /* Application requests the first DIE in this CU. */ if (die == NULL) - return (dwarf_offdie(dbg, cu->cu_1st_offset, ret_die, - error)); + return (dwarf_offdie_b(dbg, cu->cu_1st_offset, is_info, + ret_die, error)); + + /* + * Check if the `is_info' flag matches the debug section the + * DIE belongs to. + */ + if (is_info != die->die_cu->cu_is_info) { + DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT); + return (DW_DLV_ERROR); + } /* * If the DIE doesn't have any children, its sibling sits next @@ -108,9 +122,8 @@ dwarf_siblingof(Dwarf_Debug dbg, Dwarf_Die die, Dwarf_Die *ret_die, } } - ret = _dwarf_die_parse(die->die_dbg, dbg->dbg_info_sec, cu, - cu->cu_dwarf_size, offset, cu->cu_next_offset, ret_die, - search_sibling, error); + ret = _dwarf_die_parse(die->die_dbg, ds, cu, cu->cu_dwarf_size, offset, + cu->cu_next_offset, ret_die, search_sibling, error); if (ret == DW_DLE_NO_ENTRY) { DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY); @@ -121,21 +134,31 @@ dwarf_siblingof(Dwarf_Debug dbg, Dwarf_Die die, Dwarf_Die *ret_die, return (DW_DLV_OK); } + +int +dwarf_siblingof(Dwarf_Debug dbg, Dwarf_Die die, Dwarf_Die *ret_die, + Dwarf_Error *error) +{ + + return (dwarf_siblingof_b(dbg, die, ret_die, 1, error)); +} + static int -_dwarf_search_die_within_cu(Dwarf_Debug dbg, Dwarf_CU cu, Dwarf_Off offset, - Dwarf_Die *ret_die, Dwarf_Error *error) +_dwarf_search_die_within_cu(Dwarf_Debug dbg, Dwarf_Section *s, Dwarf_CU cu, + Dwarf_Off offset, Dwarf_Die *ret_die, Dwarf_Error *error) { assert(dbg != NULL && cu != NULL && ret_die != NULL); - return (_dwarf_die_parse(dbg, dbg->dbg_info_sec, cu, cu->cu_dwarf_size, + return (_dwarf_die_parse(dbg, s, cu, cu->cu_dwarf_size, offset, cu->cu_next_offset, ret_die, 0, error)); } int -dwarf_offdie(Dwarf_Debug dbg, Dwarf_Off offset, Dwarf_Die *ret_die, - Dwarf_Error *error) +dwarf_offdie_b(Dwarf_Debug dbg, Dwarf_Off offset, Dwarf_Bool is_info, + Dwarf_Die *ret_die, Dwarf_Error *error) { + Dwarf_Section *ds; Dwarf_CU cu; int ret; @@ -144,11 +167,13 @@ dwarf_offdie(Dwarf_Debug dbg, Dwarf_Off offset, Dwarf_Die *ret_die, return (DW_DLV_ERROR); } + ds = is_info ? dbg->dbg_info_sec : dbg->dbg_types_sec; + cu = is_info ? dbg->dbg_cu_current : dbg->dbg_tu_current; + /* First search the current CU. */ - if (dbg->dbg_cu_current != NULL) { - cu = dbg->dbg_cu_current; + if (cu != NULL) { if (offset > cu->cu_offset && offset < cu->cu_next_offset) { - ret = _dwarf_search_die_within_cu(dbg, cu, offset, + ret = _dwarf_search_die_within_cu(dbg, ds, cu, offset, ret_die, error); if (ret == DW_DLE_NO_ENTRY) { DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY); @@ -160,21 +185,38 @@ dwarf_offdie(Dwarf_Debug dbg, Dwarf_Off offset, Dwarf_Die *ret_die, } /* Search other CUs. */ - ret = _dwarf_info_load(dbg, 1, error); + ret = _dwarf_info_load(dbg, 1, is_info, error); if (ret != DW_DLE_NONE) return (DW_DLV_ERROR); - STAILQ_FOREACH(cu, &dbg->dbg_cu, cu_next) { - if (offset < cu->cu_offset || offset > cu->cu_next_offset) - continue; - ret = _dwarf_search_die_within_cu(dbg, cu, offset, - ret_die, error); - if (ret == DW_DLE_NO_ENTRY) { - DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY); - return (DW_DLV_NO_ENTRY); - } else if (ret != DW_DLE_NONE) - return (DW_DLV_ERROR); - return (DW_DLV_OK); + if (is_info) { + STAILQ_FOREACH(cu, &dbg->dbg_cu, cu_next) { + if (offset < cu->cu_offset || + offset > cu->cu_next_offset) + continue; + ret = _dwarf_search_die_within_cu(dbg, ds, cu, offset, + ret_die, error); + if (ret == DW_DLE_NO_ENTRY) { + DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY); + return (DW_DLV_NO_ENTRY); + } else if (ret != DW_DLE_NONE) + return (DW_DLV_ERROR); + return (DW_DLV_OK); + } + } else { + STAILQ_FOREACH(cu, &dbg->dbg_tu, cu_next) { + if (offset < cu->cu_offset || + offset > cu->cu_next_offset) + continue; + ret = _dwarf_search_die_within_cu(dbg, ds, cu, offset, + ret_die, error); + if (ret == DW_DLE_NO_ENTRY) { + DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY); + return (DW_DLV_NO_ENTRY); + } else if (ret != DW_DLE_NONE) + return (DW_DLV_ERROR); + return (DW_DLV_OK); + } } DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY); @@ -182,6 +224,14 @@ dwarf_offdie(Dwarf_Debug dbg, Dwarf_Off offset, Dwarf_Die *ret_die, } int +dwarf_offdie(Dwarf_Debug dbg, Dwarf_Off offset, Dwarf_Die *ret_die, + Dwarf_Error *error) +{ + + return (dwarf_offdie_b(dbg, offset, 1, ret_die, error)); +} + +int dwarf_tag(Dwarf_Die die, Dwarf_Half *tag, Dwarf_Error *error) { Dwarf_Debug dbg; @@ -293,9 +343,9 @@ dwarf_die_abbrev_code(Dwarf_Die die) } int -dwarf_get_cu_die_offset_given_cu_header_offset(Dwarf_Debug dbg, - Dwarf_Off in_cu_header_offset, Dwarf_Off *out_cu_die_offset, - Dwarf_Error *error) +dwarf_get_cu_die_offset_given_cu_header_offset_b(Dwarf_Debug dbg, + Dwarf_Off in_cu_header_offset, Dwarf_Bool is_info, + Dwarf_Off *out_cu_die_offset, Dwarf_Error *error) { Dwarf_CU cu; @@ -304,10 +354,19 @@ dwarf_get_cu_die_offset_given_cu_header_offset(Dwarf_Debug dbg, return (DW_DLV_ERROR); } - STAILQ_FOREACH(cu, &dbg->dbg_cu, cu_next) { - if (cu->cu_offset == in_cu_header_offset) { - *out_cu_die_offset = cu->cu_1st_offset; - break; + if (is_info) { + STAILQ_FOREACH(cu, &dbg->dbg_cu, cu_next) { + if (cu->cu_offset == in_cu_header_offset) { + *out_cu_die_offset = cu->cu_1st_offset; + break; + } + } + } else { + STAILQ_FOREACH(cu, &dbg->dbg_tu, cu_next) { + if (cu->cu_offset == in_cu_header_offset) { + *out_cu_die_offset = cu->cu_1st_offset; + break; + } } } @@ -320,6 +379,16 @@ dwarf_get_cu_die_offset_given_cu_header_offset(Dwarf_Debug dbg, } int +dwarf_get_cu_die_offset_given_cu_header_offset(Dwarf_Debug dbg, + Dwarf_Off in_cu_header_offset, Dwarf_Off *out_cu_die_offset, + Dwarf_Error *error) +{ + + return (dwarf_get_cu_die_offset_given_cu_header_offset_b(dbg, + in_cu_header_offset, 1, out_cu_die_offset, error)); +} + +int dwarf_get_address_size(Dwarf_Debug dbg, Dwarf_Half *addr_size, Dwarf_Error *error) { @@ -333,3 +402,12 @@ dwarf_get_address_size(Dwarf_Debug dbg, Dwarf_Half *addr_size, return (DW_DLV_OK); } + +Dwarf_Bool +dwarf_get_die_infotypes_flag(Dwarf_Die die) +{ + + assert(die != NULL); + + return (die->die_cu->cu_is_info); +} diff --git a/contrib/elftoolchain/libdwarf/dwarf_dieoffset.3 b/contrib/elftoolchain/libdwarf/dwarf_dieoffset.3 index 545bf2699c4d..00b0e71b8389 100644 --- a/contrib/elftoolchain/libdwarf/dwarf_dieoffset.3 +++ b/contrib/elftoolchain/libdwarf/dwarf_dieoffset.3 @@ -1,4 +1,4 @@ -.\" Copyright (c) 2010 Kai Wang +.\" Copyright (c) 2010,2014 Kai Wang .\" All rights reserved. .\" .\" Redistribution and use in source and binary forms, with or without @@ -22,16 +22,17 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.\" $Id: dwarf_dieoffset.3 2073 2011-10-27 03:30:47Z jkoshy $ +.\" $Id: dwarf_dieoffset.3 3129 2014-12-21 20:06:26Z jkoshy $ .\" -.Dd April 17, 2010 +.Dd December 21, 2014 .Os .Dt DWARF_DIEOFFSET 3 .Sh NAME .Nm dwarf_die_CU_offset , .Nm dwarf_die_CU_offset_range , .Nm dwarf_dieoffset , -.Nm dwarf_get_cu_die_offset_given_cu_header_offset +.Nm dwarf_get_cu_die_offset_given_cu_header_offset , +.Nm dwarf_get_cu_die_offset_given_cu_header_offset_b .Nd return offsets of DWARF debugging information entries .Sh LIBRARY .Lb libdwarf @@ -63,6 +64,14 @@ .Fa "Dwarf_Off *out_cu_die_offset" .Fa "Dwarf_Error *err" .Fc +.Ft int +.Fo dwarf_get_cu_die_offset_given_cu_header_offset_b +.Fa "Dwarf_Debug dbg" +.Fa "Dwarf_Off in_cu_header_offset" +.Fa "Dwarf_Bool is_info" +.Fa "Dwarf_Off *out_cu_die_offset" +.Fa "Dwarf_Error *err" +.Fc .Sh DESCRIPTION These functions are used to retrieve offsets for DWARF debugging information entries. @@ -114,7 +123,7 @@ an error. .Pp Function .Fn dwarf_get_cu_die_offset_given_cu_header_offset -returns the offset for the debugging information entry for a +returns the offset for the first debugging information entry for a compilation unit, given an offset to the header of the compilation unit. Argument @@ -131,8 +140,30 @@ If argument .Ar err is non-NULL, it will be used to return an error descriptor in case of an error. +.Pp +Function +.Fn dwarf_get_cu_die_offset_given_cu_header_offset_b +behaves identically to the function +.Fn dwarf_get_cu_die_offset_given_cu_header_offset +when the argument +.Ar is_info +is non-zero. +When the argument +.Ar is_info +is zero, function +.Fn dwarf_get_cu_die_offset_given_cu_header_offset_b +returns the offset for the first debugging information entry for a +type unit, given an offset to the header of the type unit in argument +.Ar in_cu_header_offset . +Argument +.Ar out_cu_die_offset +points to a location that will hold the returned offset. +If the argument +.Ar err +is non-NULL, it will be used to return an error descriptor in case of +an error. .Sh RETURN VALUES -On success, these functions returns +On success, these functions return .Dv DW_DLV_OK . In case of an error, these functions return .Dv DW_DLV_ERROR @@ -141,11 +172,13 @@ and set argument .Pp Function .Fn dwarf_get_cu_die_offset_given_cu_header_offset +and +.Fn dwarf_get_cu_die_offset_given_cu_header_offset_b returns .Dv DW_DLV_NO_ENTRY and sets argument .Ar err -if there is no compilation unit located at the +if there is no compilation or type unit located at the offset specified in argument .Ar in_cu_header_offset . .Sh ERRORS @@ -169,4 +202,5 @@ specified an unknown offset. .Sh SEE ALSO .Xr dwarf 3 , .Xr dwarf_next_cu_header 3 , -.Xr dwarf_offdie 3 +.Xr dwarf_offdie 3 , +.Xr dwarf_offdie_b 3 diff --git a/contrib/elftoolchain/libdwarf/dwarf_dump.c b/contrib/elftoolchain/libdwarf/dwarf_dump.c index 9b5d1f767e1a..3219fa4c0c6c 100644 --- a/contrib/elftoolchain/libdwarf/dwarf_dump.c +++ b/contrib/elftoolchain/libdwarf/dwarf_dump.c @@ -27,7 +27,7 @@ #include "_libdwarf.h" -ELFTC_VCSID("$Id: dwarf_dump.c 2073 2011-10-27 03:30:47Z jkoshy $"); +ELFTC_VCSID("$Id: dwarf_dump.c 3052 2014-05-26 20:36:24Z kaiwang27 $"); int dwarf_get_ACCESS_name(unsigned access, const char **s) @@ -250,6 +250,54 @@ dwarf_get_AT_name(unsigned attr, const char **s) *s = "DW_AT_visibility"; break; case DW_AT_vtable_elem_location: *s = "DW_AT_vtable_elem_location"; break; + case DW_AT_sf_names: + *s = "DW_AT_sf_names"; break; + case DW_AT_src_info: + *s = "DW_AT_src_info"; break; + case DW_AT_mac_info: + *s = "DW_AT_mac_info"; break; + case DW_AT_src_coords: + *s = "DW_AT_src_coords"; break; + case DW_AT_body_begin: + *s = "DW_AT_body_begin"; break; + case DW_AT_body_end: + *s = "DW_AT_body_end"; break; + case DW_AT_GNU_vector: + *s = "DW_AT_GNU_vector"; break; + case DW_AT_GNU_guarded_by: + *s = "DW_AT_GNU_guarded_by"; break; + case DW_AT_GNU_pt_guarded_by: + *s = "DW_AT_GNU_pt_guarded_by"; break; + case DW_AT_GNU_guarded: + *s = "DW_AT_GNU_guarded"; break; + case DW_AT_GNU_pt_guarded: + *s = "DW_AT_GNU_pt_guarded"; break; + case DW_AT_GNU_locks_excluded: + *s = "DW_AT_GNU_locks_excluded"; break; + case DW_AT_GNU_exclusive_locks_required: + *s = "DW_AT_GNU_exclusive_locks_required"; break; + case DW_AT_GNU_shared_locks_required: + *s = "DW_AT_GNU_shared_locks_required"; break; + case DW_AT_GNU_odr_signature: + *s = "DW_AT_GNU_odr_signature"; break; + case DW_AT_GNU_template_name: + *s = "DW_AT_GNU_template_name"; break; + case DW_AT_GNU_call_site_value: + *s = "DW_AT_GNU_call_site_value"; break; + case DW_AT_GNU_call_site_data_value: + *s = "DW_AT_GNU_call_site_data_value"; break; + case DW_AT_GNU_call_site_target: + *s = "DW_AT_GNU_call_site_target"; break; + case DW_AT_GNU_call_site_target_clobbered: + *s = "DW_AT_GNU_call_site_target_clobbered"; break; + case DW_AT_GNU_tail_call: + *s = "DW_AT_GNU_tail_call"; break; + case DW_AT_GNU_all_tail_call_sites: + *s = "DW_AT_GNU_all_tail_call_sites"; break; + case DW_AT_GNU_all_call_sites: + *s = "DW_AT_GNU_all_call_sites"; break; + case DW_AT_GNU_all_source_call_sites: + *s = "DW_AT_GNU_all_source_call_sites"; break; default: return (DW_DLV_NO_ENTRY); } @@ -1094,6 +1142,30 @@ dwarf_get_OP_name(unsigned op, const char **s) *s = "DW_OP_stack_value"; break; case DW_OP_GNU_push_tls_address: *s = "DW_OP_GNU_push_tls_address"; break; + case DW_OP_GNU_uninit: + *s = "DW_OP_GNU_uninit"; break; + case DW_OP_GNU_encoded_addr: + *s = "DW_OP_GNU_encoded_addr"; break; + case DW_OP_GNU_implicit_pointer: + *s = "DW_OP_GNU_implicit_pointer"; break; + case DW_OP_GNU_entry_value: + *s = "DW_OP_GNU_entry_value"; break; + case DW_OP_GNU_const_type: + *s = "DW_OP_GNU_const_type"; break; + case DW_OP_GNU_regval_type: + *s = "DW_OP_GNU_regval_type"; break; + case DW_OP_GNU_deref_type: + *s = "DW_OP_GNU_deref_type"; break; + case DW_OP_GNU_convert: + *s = "DW_OP_GNU_convert"; break; + case DW_OP_GNU_reinterpret: + *s = "DW_OP_GNU_reinterpret"; break; + case DW_OP_GNU_parameter_ref: + *s = "DW_OP_GNU_parameter_ref"; break; + case DW_OP_GNU_addr_index: + *s = "DW_OP_GNU_addr_index"; break; + case DW_OP_GNU_const_index: + *s = "DW_OP_GNU_const_index"; break; default: return (DW_DLV_NO_ENTRY); } @@ -1244,6 +1316,26 @@ dwarf_get_TAG_name(unsigned tag, const char **s) *s = "DW_TAG_volatile_type"; break; case DW_TAG_with_stmt: *s = "DW_TAG_with_stmt"; break; + case DW_TAG_format_label: + *s = "DW_TAG_format_label"; break; + case DW_TAG_function_template: + *s = "DW_TAG_function_template"; break; + case DW_TAG_class_template: + *s = "DW_TAG_class_template"; break; + case DW_TAG_GNU_BINCL: + *s = "DW_TAG_GNU_BINCL"; break; + case DW_TAG_GNU_EINCL: + *s = "DW_TAG_GNU_EINCL"; break; + case DW_TAG_GNU_template_template_param: + *s = "DW_TAG_GNU_template_template_param"; break; + case DW_TAG_GNU_template_parameter_pack: + *s = "DW_TAG_GNU_template_parameter_pack"; break; + case DW_TAG_GNU_formal_parameter_pack: + *s = "DW_TAG_GNU_formal_parameter_pack"; break; + case DW_TAG_GNU_call_site: + *s = "DW_TAG_GNU_call_site"; break; + case DW_TAG_GNU_call_site_parameter: + *s = "DW_TAG_GNU_call_site_parameter"; break; default: return (DW_DLV_NO_ENTRY); } diff --git a/contrib/elftoolchain/libdwarf/dwarf_errmsg.c b/contrib/elftoolchain/libdwarf/dwarf_errmsg.c index 16f8e4bc6ed6..e300893a61c1 100644 --- a/contrib/elftoolchain/libdwarf/dwarf_errmsg.c +++ b/contrib/elftoolchain/libdwarf/dwarf_errmsg.c @@ -26,7 +26,7 @@ #include "_libdwarf.h" -ELFTC_VCSID("$Id: dwarf_errmsg.c 2576 2012-09-13 09:16:11Z jkoshy $"); +ELFTC_VCSID("$Id: dwarf_errmsg.c 2975 2014-01-21 20:08:04Z kaiwang27 $"); static const char *_libdwarf_errors[] = { #define DEFINE_ERROR(N,S) [DW_DLE_##N] = S diff --git a/contrib/elftoolchain/libdwarf/dwarf_expand_frame_instructions.3 b/contrib/elftoolchain/libdwarf/dwarf_expand_frame_instructions.3 index eaeb32039c30..7bcd105f030f 100644 --- a/contrib/elftoolchain/libdwarf/dwarf_expand_frame_instructions.3 +++ b/contrib/elftoolchain/libdwarf/dwarf_expand_frame_instructions.3 @@ -22,14 +22,14 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.\" $Id: dwarf_expand_frame_instructions.3 2122 2011-11-09 15:35:14Z jkoshy $ +.\" $Id: dwarf_expand_frame_instructions.3 3181 2015-04-10 13:22:51Z emaste $ .\" .Dd November 9, 2011 .Os .Dt DWARF_EXPAND_FRAME_INSTRUCTIONS 3 .Sh NAME .Nm dwarf_expand_frame_instructions -.Nd expand frame instructions +.Nd expand frame instructions .Sh LIBRARY .Lb libdwarf .Sh SYNOPSIS diff --git a/contrib/elftoolchain/libdwarf/dwarf_formblock.3 b/contrib/elftoolchain/libdwarf/dwarf_formblock.3 index 4ae824f68e9a..1ffb6e6fec0d 100644 --- a/contrib/elftoolchain/libdwarf/dwarf_formblock.3 +++ b/contrib/elftoolchain/libdwarf/dwarf_formblock.3 @@ -22,7 +22,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.\" $Id: dwarf_formblock.3 2073 2011-10-27 03:30:47Z jkoshy $ +.\" $Id: dwarf_formblock.3 3182 2015-04-10 16:08:10Z emaste $ .\" .Dd July 23, 2010 .Os @@ -69,7 +69,7 @@ the DWARF(3) library. The application should not attempt to free this memory area. Portable code may indicate that the memory area is to be freed by -by using +using .Xr dwarf_dealloc 3 . .Sh RETURN VALUES Function diff --git a/contrib/elftoolchain/libdwarf/dwarf_formflag.3 b/contrib/elftoolchain/libdwarf/dwarf_formflag.3 index 93d5902c860a..2e23b3301661 100644 --- a/contrib/elftoolchain/libdwarf/dwarf_formflag.3 +++ b/contrib/elftoolchain/libdwarf/dwarf_formflag.3 @@ -22,7 +22,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.\" $Id: dwarf_formflag.3 2073 2011-10-27 03:30:47Z jkoshy $ +.\" $Id: dwarf_formflag.3 3181 2015-04-10 13:22:51Z emaste $ .\" .Dd June 21, 2010 .Os @@ -58,7 +58,7 @@ or If argument .Ar err is not NULL, it will be used to return an error descriptor in case -of an error. +of an error. .Sh RETURN VALUES Function .Fn dwarf_formflag diff --git a/contrib/elftoolchain/libdwarf/dwarf_formref.3 b/contrib/elftoolchain/libdwarf/dwarf_formref.3 index 9e3458face08..abde30cfb9e4 100644 --- a/contrib/elftoolchain/libdwarf/dwarf_formref.3 +++ b/contrib/elftoolchain/libdwarf/dwarf_formref.3 @@ -22,7 +22,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.\" $Id: dwarf_formref.3 2073 2011-10-27 03:30:47Z jkoshy $ +.\" $Id: dwarf_formref.3 3181 2015-04-10 13:22:51Z emaste $ .\" .Dd June 21, 2010 .Os @@ -101,7 +101,7 @@ ELF section. If argument .Ar err is not NULL, it will be used to return an error descriptor in case -of an error. +of an error. .Sh RETURN VALUES These functions return .Dv DW_DLV_OK diff --git a/contrib/elftoolchain/libdwarf/dwarf_formsig8.3 b/contrib/elftoolchain/libdwarf/dwarf_formsig8.3 index 5445324dd7af..a76c1107d9eb 100644 --- a/contrib/elftoolchain/libdwarf/dwarf_formsig8.3 +++ b/contrib/elftoolchain/libdwarf/dwarf_formsig8.3 @@ -22,7 +22,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.\" $Id: dwarf_formsig8.3 2073 2011-10-27 03:30:47Z jkoshy $ +.\" $Id: dwarf_formsig8.3 3181 2015-04-10 13:22:51Z emaste $ .\" .Dd July 24, 2010 .Os @@ -56,7 +56,7 @@ must be If argument .Ar err is not NULL, it will be used to return an error descriptor in case -of an error. +of an error. .Sh RETURN VALUES Function .Fn dwarf_formsig8 diff --git a/contrib/elftoolchain/libdwarf/dwarf_formudata.3 b/contrib/elftoolchain/libdwarf/dwarf_formudata.3 index f95fdead898c..9ed154eebb97 100644 --- a/contrib/elftoolchain/libdwarf/dwarf_formudata.3 +++ b/contrib/elftoolchain/libdwarf/dwarf_formudata.3 @@ -22,7 +22,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.\" $Id: dwarf_formudata.3 2073 2011-10-27 03:30:47Z jkoshy $ +.\" $Id: dwarf_formudata.3 3181 2015-04-10 13:22:51Z emaste $ .\" .Dd June 21, 2010 .Os @@ -88,7 +88,7 @@ and If argument .Ar err is not NULL, it will be used to return an error descriptor in case -of an error. +of an error. .Sh RETURN VALUES These functions return .Dv DW_DLV_OK diff --git a/contrib/elftoolchain/libdwarf/dwarf_frame.c b/contrib/elftoolchain/libdwarf/dwarf_frame.c index 5ccbbc4aead7..442f232cf092 100644 --- a/contrib/elftoolchain/libdwarf/dwarf_frame.c +++ b/contrib/elftoolchain/libdwarf/dwarf_frame.c @@ -26,7 +26,7 @@ #include "_libdwarf.h" -ELFTC_VCSID("$Id: dwarf_frame.c 2073 2011-10-27 03:30:47Z jkoshy $"); +ELFTC_VCSID("$Id: dwarf_frame.c 3106 2014-12-19 16:00:58Z kaiwang27 $"); int dwarf_get_fde_list(Dwarf_Debug dbg, Dwarf_Cie **cie_list, @@ -539,8 +539,8 @@ dwarf_expand_frame_instructions(Dwarf_Cie cie, Dwarf_Ptr instruction, return (DW_DLV_ERROR); } - ret = _dwarf_frame_get_fop(dbg, instruction, len, ret_oplist, ret_opcnt, - error); + ret = _dwarf_frame_get_fop(dbg, cie->cie_addrsize, instruction, len, + ret_oplist, ret_opcnt, error); if (ret != DW_DLE_NONE) return (DW_DLV_ERROR); diff --git a/contrib/elftoolchain/libdwarf/dwarf_get_AT_name.3 b/contrib/elftoolchain/libdwarf/dwarf_get_AT_name.3 index 5b5d5a1167a8..473adc331f3f 100644 --- a/contrib/elftoolchain/libdwarf/dwarf_get_AT_name.3 +++ b/contrib/elftoolchain/libdwarf/dwarf_get_AT_name.3 @@ -22,7 +22,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.\" $Id: dwarf_get_AT_name.3 2071 2011-10-27 03:20:00Z jkoshy $ +.\" $Id: dwarf_get_AT_name.3 3142 2015-01-29 23:11:14Z jkoshy $ .\" .Dd April 22, 2011 .Os @@ -247,6 +247,7 @@ constants. .It Fn dwarf_get_VIS_name .Dv DW_VIS_* constants. +.El .Sh RETURN VALUES These functions return .Dv DW_DLV_OK on success. diff --git a/contrib/elftoolchain/libdwarf/dwarf_get_arange_info.3 b/contrib/elftoolchain/libdwarf/dwarf_get_arange_info.3 index e8dac7810a0d..3878edde19bb 100644 --- a/contrib/elftoolchain/libdwarf/dwarf_get_arange_info.3 +++ b/contrib/elftoolchain/libdwarf/dwarf_get_arange_info.3 @@ -22,7 +22,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.\" $Id: dwarf_get_arange_info.3 2134 2011-11-10 08:40:14Z jkoshy $ +.\" $Id: dwarf_get_arange_info.3 3142 2015-01-29 23:11:14Z jkoshy $ .\" .Dd April 16, 2011 .Os @@ -102,6 +102,7 @@ One of the arguments or .Ar cu_die_offset was NULL. +.El .Sh EXAMPLE To loop through all the address lookup table entries, use: .Bd -literal -offset indent diff --git a/contrib/elftoolchain/libdwarf/dwarf_get_die_infotypes_flag.3 b/contrib/elftoolchain/libdwarf/dwarf_get_die_infotypes_flag.3 new file mode 100644 index 000000000000..4f5a19ace367 --- /dev/null +++ b/contrib/elftoolchain/libdwarf/dwarf_get_die_infotypes_flag.3 @@ -0,0 +1,73 @@ +.\" Copyright (c) 2014 Kai Wang +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (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: dwarf_get_die_infotypes_flag.3 3118 2014-12-20 20:30:06Z jkoshy $ +.\" +.Dd December 20, 2014 +.Os +.Dt DWARF_GET_DIE_INFOTYPES_FLAG 3 +.Sh NAME +.Nm dwarf_get_die_infotypes_flag +.Nd indicate the originating DWARF section for a DIE +.Sh LIBRARY +.Lb libdwarf +.Sh SYNOPSIS +.In libdwarf.h +.Ft Dwarf_Bool +.Fo dwarf_get_die_infotypes_flag +.Fa "Dwarf_Die die" +.Fc +.Sh DESCRIPTION +Function +.Fn dwarf_get_die_infotypes_flag +returns a flag indicating the originating DWARF section for the +debugging information entry referenced by argument +.Ar die . +.Pp +Argument +.Ar die +should reference a valid debugging information entry descriptor. +.Sh RETURN VALUES +Function +.Fn dwarf_get_die_infotypes_flag +returns a non-zero value if argument +.Ar die +originates in the +.Dq .debug_info +section. +.Pp +It returns zero if argument +.Ar die +originates in the +.Dq .debug_types +section. +.Sh ERRORS +Function +.Fn dwarf_get_die_infotypes_flag +always succeeds. +.Sh SEE ALSO +.Xr dwarf 3 , +.Xr dwarf_next_cu_header_c 3 , +.Xr dwarf_offdie_b 3 , +.Xr dwarf_siblingof_b 3 diff --git a/contrib/elftoolchain/libdwarf/dwarf_get_fde_info_for_all_regs.3 b/contrib/elftoolchain/libdwarf/dwarf_get_fde_info_for_all_regs.3 index 014d138a0e23..f55d3d68a5bb 100644 --- a/contrib/elftoolchain/libdwarf/dwarf_get_fde_info_for_all_regs.3 +++ b/contrib/elftoolchain/libdwarf/dwarf_get_fde_info_for_all_regs.3 @@ -22,7 +22,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.\" $Id: dwarf_get_fde_info_for_all_regs.3 2071 2011-10-27 03:20:00Z jkoshy $ +.\" $Id: dwarf_get_fde_info_for_all_regs.3 3182 2015-04-10 16:08:10Z emaste $ .\" .Dd June 4, 2011 .Os @@ -90,7 +90,8 @@ typedef struct { For each of the register rules returned, the .Va dw_offset_relevant -field is set to 1 if the register rule has a offset value. The +field is set to 1 if the register rule has a offset value. +The .Va dw_regnum field is set to the register number associated with the regsiter rule. The diff --git a/contrib/elftoolchain/libdwarf/dwarf_get_fde_info_for_reg.3 b/contrib/elftoolchain/libdwarf/dwarf_get_fde_info_for_reg.3 index 3d468062602c..fd05814a3630 100644 --- a/contrib/elftoolchain/libdwarf/dwarf_get_fde_info_for_reg.3 +++ b/contrib/elftoolchain/libdwarf/dwarf_get_fde_info_for_reg.3 @@ -22,7 +22,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.\" $Id: dwarf_get_fde_info_for_reg.3 2071 2011-10-27 03:20:00Z jkoshy $ +.\" $Id: dwarf_get_fde_info_for_reg.3 3181 2015-04-10 13:22:51Z emaste $ .\" .Dd June 4, 2011 .Os @@ -99,7 +99,7 @@ counter address associated with the register rule found. If argument .Ar err is not NULL, it will be used to return an error descriptor in case -of an error. +of an error. .Ss COMPATIBILITY Function .Fn dwarf_get_fde_info_for_reg diff --git a/contrib/elftoolchain/libdwarf/dwarf_get_ranges.3 b/contrib/elftoolchain/libdwarf/dwarf_get_ranges.3 index 37847a1a9c22..15bad4dfd7ac 100644 --- a/contrib/elftoolchain/libdwarf/dwarf_get_ranges.3 +++ b/contrib/elftoolchain/libdwarf/dwarf_get_ranges.3 @@ -22,7 +22,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.\" $Id: dwarf_get_ranges.3 2122 2011-11-09 15:35:14Z jkoshy $ +.\" $Id: dwarf_get_ranges.3 3182 2015-04-10 16:08:10Z emaste $ .\" .Dd November 9, 2011 .Os @@ -137,7 +137,7 @@ For this type of entry, the field .Va dwr_addr1 is the value of the largest representable address offset, and .Va dwr_addr2 -is a base address for the begining and ending address offsets of +is a base address for the beginning and ending address offsets of subsequent address range entries in the list. .It Dv DW_RANGES_END An end of list mark. diff --git a/contrib/elftoolchain/libdwarf/dwarf_get_section_max_offsets.3 b/contrib/elftoolchain/libdwarf/dwarf_get_section_max_offsets.3 new file mode 100644 index 000000000000..963d4ac3814e --- /dev/null +++ b/contrib/elftoolchain/libdwarf/dwarf_get_section_max_offsets.3 @@ -0,0 +1,116 @@ +.\" Copyright (c) 2014 Kai Wang +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (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: dwarf_get_section_max_offsets.3 3141 2015-01-29 23:11:10Z jkoshy $ +.\" +.Dd December 21, 2014 +.Os +.Dt DWARF_GET_SECTION_MAX_OFFSETS +.Sh NAME +.Nm dwarf_get_section_max_offsets , +.Nm dwarf_get_section_max_offsets_b +.Nd return the size of DWARF sections +.Sh LIBRARY +.Lb libdwarf +.Sh SYNOPSIS +.In libdwarf.h +.Ft int +.Fo dwarf_get_section_max_offsets +.Fa "Dwarf_Debug dbg" +.Fa "Dwarf_Unsigned *debug_info" +.Fa "Dwarf_Unsigned *debug_abbrev" +.Fa "Dwarf_Unsigned *debug_line" +.Fa "Dwarf_Unsigned *debug_loc" +.Fa "Dwarf_Unsigned *debug_aranges" +.Fa "Dwarf_Unsigned *debug_macinfo" +.Fa "Dwarf_Unsigned *debug_pubnames" +.Fa "Dwarf_Unsigned *debug_str" +.Fa "Dwarf_Unsigned *debug_frame" +.Fa "Dwarf_Unsigned *debug_ranges" +.Fa "Dwarf_Unsigned *debug_pubtypes" +.Fc +.Ft int +.Fo dwarf_get_section_max_offsets_b +.Fa "Dwarf_Debug dbg" +.Fa "Dwarf_Unsigned *debug_info" +.Fa "Dwarf_Unsigned *debug_abbrev" +.Fa "Dwarf_Unsigned *debug_line" +.Fa "Dwarf_Unsigned *debug_loc" +.Fa "Dwarf_Unsigned *debug_aranges" +.Fa "Dwarf_Unsigned *debug_macinfo" +.Fa "Dwarf_Unsigned *debug_pubnames" +.Fa "Dwarf_Unsigned *debug_str" +.Fa "Dwarf_Unsigned *debug_frame" +.Fa "Dwarf_Unsigned *debug_ranges" +.Fa "Dwarf_Unsigned *debug_pubtypes" +.Fa "Dwarf_Unsigned *debug_types" +.Fc +.Sh DESCRIPTION +Function +.Fn dwarf_get_section_max_offsets_b +retrieves the sizes of the DWARF sections in a DWARF debug context. +Argument +.Ar dbg +should reference a DWARF debug context allocated using +.Xr dwarf_init 3 . +The function stores the size of each DWARF section to the location +pointed to by the argument corresponding to the section name. +If a DWARF section does not exist, the location pointed to by the +argument corresponding to that section will be set to zero. +.Pp +A value of NULL may be used for any of the arguments +.Ar debug_info , +.Ar debug_abbrev , +.Ar debug_line , +.Ar debug_loc , +.Ar debug_aranges , +.Ar debug_macinfo , +.Ar debug_pubnames , +.Ar debug_str , +.Ar debug_frame , +.Ar debug_ranges , +.Ar debug_pubtypes +and +.Ar debug_types +if the caller is not interested in the respective section size. +.Pp +Function +.Fn dwarf_get_section_max_offsets +is identical to function +.Fn dwarf_get_section_max_offsets_b +except that it does not provide argument +.Ar debug_types , +and thus cannot return the size of the +.Dq \&.debug_types +section. +.Sh RETURN VALUES +On success, these functions return +.Dv DW_DLV_OK . +If argument +.Ar dbg +is NULL, they return +.Dv DW_DLV_ERROR . +.Sh SEE ALSO +.Xr dwarf 3 , +.Xr dwarf_init 3 diff --git a/contrib/elftoolchain/libdwarf/dwarf_hasattr.3 b/contrib/elftoolchain/libdwarf/dwarf_hasattr.3 index 5b4699bd7778..659887588563 100644 --- a/contrib/elftoolchain/libdwarf/dwarf_hasattr.3 +++ b/contrib/elftoolchain/libdwarf/dwarf_hasattr.3 @@ -22,7 +22,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.\" $Id: dwarf_hasattr.3 2073 2011-10-27 03:30:47Z jkoshy $ +.\" $Id: dwarf_hasattr.3 3181 2015-04-10 13:22:51Z emaste $ .\" .Dd April 17, 2010 .Os @@ -64,7 +64,7 @@ If the named attribute is not present, a zero is written instead. If argument .Ar err is not NULL, it will be used to return an error descriptor in case -of an error. +of an error. .Sh RETURN VALUES On success, function .Fn dwarf_hasattr @@ -85,6 +85,7 @@ Either of argument or .Va ret_bool was NULL. +.El .Sh SEE ALSO .Xr dwarf 3 , .Xr dwarf_attr 3 , diff --git a/contrib/elftoolchain/libdwarf/dwarf_highpc.3 b/contrib/elftoolchain/libdwarf/dwarf_highpc.3 index 998a3b6ad650..4d31f5c8d1a2 100644 --- a/contrib/elftoolchain/libdwarf/dwarf_highpc.3 +++ b/contrib/elftoolchain/libdwarf/dwarf_highpc.3 @@ -1,4 +1,4 @@ -.\" Copyright (c) 2010 Kai Wang +.\" Copyright (c) 2010,2014 Kai Wang .\" All rights reserved. .\" .\" Redistribution and use in source and binary forms, with or without @@ -22,9 +22,9 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.\" $Id: dwarf_highpc.3 2073 2011-10-27 03:30:47Z jkoshy $ +.\" $Id: dwarf_highpc.3 3092 2014-09-02 22:09:30Z kaiwang27 $ .\" -.Dd April 7, 2010 +.Dd July 22, 2014 .Os .Dt DWARF_HIGHPC 3 .Sh NAME @@ -33,6 +33,7 @@ .Nm dwarf_bitsize , .Nm dwarf_bytesize , .Nm dwarf_highpc , +.Nm dwarf_highpc_b , .Nm dwarf_lowpc , .Nm dwarf_srclang .Nd retrieve the value of a DWARF attribute @@ -71,6 +72,14 @@ .Fa "Dwarf_Error *err" .Fc .Ft int +.Fo dwarf_highpc_b +.Fa "Dwarf_Die die" +.Fa "Dwarf_Addr *ret_highpc" +.Fa "Dwarf_Half *ret_form" +.Fa "enum Dwarf_Form_Class *ret_class" +.Fa "Dwarf_Error *err" +.Fc +.Ft int .Fo dwarf_lowpc .Fa "Dwarf_Die die" .Fa "Dwarf_Addr *ret_lowpc" @@ -114,6 +123,10 @@ attribute value. Retrieve the .Dv DW_AT_high_pc attribute value. +.It Fn dwarf_highpc_b +Retrieve the +.Dv DW_AT_high_pc +attribute value. .It Fn dwarf_lowpc Retrieve the .Dv DW_AT_low_pc @@ -123,6 +136,23 @@ Retrieve the .Dv DW_AT_language attribute value. .El +.Pp +Function +.Fn dwarf_highpc_b +is an enhanced version of function +.Fn dwarf_highpc . +It sets the location specified by argument +.Ar ret_form +to the form code of the attribute +.Dv DW_AT_high_pc , +and sets the location specified by argument +.Ar ret_class +to the class of that form. +A value of NULL may be used for either of the arguments +.Ar ret_form +or +.Ar ret_class +if the caller is not interested in the respective value. .Sh RETURN VALUES These functions return .Dv DW_DLV_OK on success. @@ -159,4 +189,5 @@ had no requested attribute. .Xr dwarf 3 , .Xr dwarf_attr 3 , .Xr dwarf_attrlist 3 , -.Xr dwarf_hasattr 3 +.Xr dwarf_hasattr 3 , +.Xr dwarf_get_form_class 3 diff --git a/contrib/elftoolchain/libdwarf/dwarf_lineno.c b/contrib/elftoolchain/libdwarf/dwarf_lineno.c index d0c24b176b71..cbcc9aeaa69d 100644 --- a/contrib/elftoolchain/libdwarf/dwarf_lineno.c +++ b/contrib/elftoolchain/libdwarf/dwarf_lineno.c @@ -26,7 +26,7 @@ #include "_libdwarf.h" -ELFTC_VCSID("$Id: dwarf_lineno.c 2074 2011-10-27 03:34:33Z jkoshy $"); +ELFTC_VCSID("$Id: dwarf_lineno.c 2983 2014-02-09 00:24:31Z kaiwang27 $"); int dwarf_srclines(Dwarf_Die die, Dwarf_Line **linebuf, Dwarf_Signed *linecount, @@ -75,8 +75,8 @@ dwarf_srclines(Dwarf_Die die, Dwarf_Line **linebuf, Dwarf_Signed *linecount, return (DW_DLV_OK); } - if ((li->li_lnarray = malloc(*linecount * - sizeof(struct _Dwarf_Line))) == NULL) { + if ((li->li_lnarray = malloc(*linecount * sizeof(Dwarf_Line))) == + NULL) { DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY); return (DW_DLV_ERROR); } diff --git a/contrib/elftoolchain/libdwarf/dwarf_loclist.c b/contrib/elftoolchain/libdwarf/dwarf_loclist.c index 4696bc52847d..e780a87128e5 100644 --- a/contrib/elftoolchain/libdwarf/dwarf_loclist.c +++ b/contrib/elftoolchain/libdwarf/dwarf_loclist.c @@ -1,5 +1,5 @@ /*- - * Copyright (c) 2009 Kai Wang + * Copyright (c) 2009,2014 Kai Wang * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -26,13 +26,37 @@ #include "_libdwarf.h" -ELFTC_VCSID("$Id: dwarf_loclist.c 2074 2011-10-27 03:34:33Z jkoshy $"); +ELFTC_VCSID("$Id: dwarf_loclist.c 3066 2014-06-06 19:36:06Z kaiwang27 $"); + +static int +copy_locdesc(Dwarf_Debug dbg, Dwarf_Locdesc *dst, Dwarf_Locdesc *src, + Dwarf_Error *error) +{ + + assert(src != NULL && dst != NULL); + + dst->ld_lopc = src->ld_lopc; + dst->ld_hipc = src->ld_hipc; + dst->ld_cents = src->ld_cents; + + if (dst->ld_cents > 0) { + dst->ld_s = calloc(dst->ld_cents, sizeof(Dwarf_Loc)); + if (dst->ld_s == NULL) { + DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY); + return (DW_DLE_MEMORY); + } + memcpy(dst->ld_s, src->ld_s, src->ld_cents * + sizeof(Dwarf_Loc)); + } else + dst->ld_s = NULL; + + return (DW_DLE_NONE); +} int dwarf_loclist_n(Dwarf_Attribute at, Dwarf_Locdesc ***llbuf, Dwarf_Signed *listlen, Dwarf_Error *error) { - Dwarf_Loclist ll; Dwarf_Debug dbg; int ret; @@ -69,26 +93,41 @@ dwarf_loclist_n(Dwarf_Attribute at, Dwarf_Locdesc ***llbuf, /* FALLTHROUGH */ case DW_FORM_sec_offset: ret = _dwarf_loclist_find(dbg, at->at_die->die_cu, - at->u[0].u64, &ll, error); + at->u[0].u64, llbuf, listlen, NULL, error); if (ret == DW_DLE_NO_ENTRY) { DWARF_SET_ERROR(dbg, error, ret); return (DW_DLV_NO_ENTRY); } if (ret != DW_DLE_NONE) return (DW_DLV_ERROR); - *llbuf = ll->ll_ldlist; - *listlen = ll->ll_ldlen; return (DW_DLV_OK); case DW_FORM_block: case DW_FORM_block1: case DW_FORM_block2: case DW_FORM_block4: + case DW_FORM_exprloc: if (at->at_ld == NULL) { ret = _dwarf_loc_add(at->at_die, at, error); if (ret != DW_DLE_NONE) return (DW_DLV_ERROR); } - *llbuf = &at->at_ld; + *llbuf = calloc(1, sizeof(Dwarf_Locdesc *)); + if (*llbuf == NULL) { + DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY); + return (DW_DLV_ERROR); + } + (*llbuf)[0] = calloc(1, sizeof(Dwarf_Locdesc)); + if ((*llbuf)[0] == NULL) { + free(*llbuf); + DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY); + return (DW_DLV_ERROR); + } + if (copy_locdesc(dbg, (*llbuf)[0], at->at_ld, error) != + DW_DLE_NONE) { + free((*llbuf)[0]); + free(*llbuf); + return (DW_DLV_ERROR); + } *listlen = 1; return (DW_DLV_OK); default: @@ -107,75 +146,27 @@ int dwarf_loclist(Dwarf_Attribute at, Dwarf_Locdesc **llbuf, Dwarf_Signed *listlen, Dwarf_Error *error) { - Dwarf_Loclist ll; - Dwarf_Debug dbg; - int ret; + Dwarf_Locdesc **_llbuf; + int i, ret; - dbg = at != NULL ? at->at_die->die_dbg : NULL; + ret = dwarf_loclist_n(at, &_llbuf, listlen, error); + if (ret != DW_DLV_OK) + return (ret); - if (at == NULL || llbuf == NULL || listlen == NULL) { - DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT); - return (DW_DLV_ERROR); - } + /* Only return the first location description of the list. */ + *llbuf = _llbuf[0]; - switch (at->at_attrib) { - case DW_AT_location: - case DW_AT_string_length: - case DW_AT_return_addr: - case DW_AT_data_member_location: - case DW_AT_frame_base: - case DW_AT_segment: - case DW_AT_static_link: - case DW_AT_use_location: - case DW_AT_vtable_elem_location: - switch (at->at_form) { - case DW_FORM_data4: - case DW_FORM_data8: - /* - * DW_FORM_data[48] can not be used as section offset - * since DWARF4. For DWARF[23], the application needs - * to determine if DW_FORM_data[48] is representing - * a constant or a section offset. - */ - if (at->at_die->die_cu->cu_version >= 4) { - printf("called cu_version >= 4\n"); - DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY); - return (DW_DLV_NO_ENTRY); - } - /* FALLTHROUGH */ - case DW_FORM_sec_offset: - ret = _dwarf_loclist_find(at->at_die->die_dbg, - at->at_die->die_cu, at->u[0].u64, &ll, error); - if (ret == DW_DLE_NO_ENTRY) { - DWARF_SET_ERROR(dbg, error, DW_DLV_NO_ENTRY); - return (DW_DLV_NO_ENTRY); - } - if (ret != DW_DLE_NONE) - return (DW_DLV_ERROR); - *llbuf = ll->ll_ldlist[0]; - *listlen = 1; - return (DW_DLV_OK); - case DW_FORM_block: - case DW_FORM_block1: - case DW_FORM_block2: - case DW_FORM_block4: - if (at->at_ld == NULL) { - ret = _dwarf_loc_add(at->at_die, at, error); - if (ret != DW_DLE_NONE) - return (DW_DLV_ERROR); - } - *llbuf = at->at_ld; - *listlen = 1; - return (DW_DLV_OK); - default: - DWARF_SET_ERROR(dbg, error, DW_DLE_ATTR_FORM_BAD); - return (DW_DLV_ERROR); - } - default: - /* Wrong attr supplied. */ - DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT); - return (DW_DLV_ERROR); + /* Free the rest of the list. */ + for (i = 1; i < *listlen; i++) { + if (_llbuf[i]->ld_s) + free(_llbuf[i]->ld_s); + free(_llbuf[i]); } + free(_llbuf); + + *listlen = 1; + + return (DW_DLV_OK); } int @@ -184,19 +175,25 @@ dwarf_get_loclist_entry(Dwarf_Debug dbg, Dwarf_Unsigned offset, Dwarf_Unsigned *entry_len, Dwarf_Unsigned *next_entry, Dwarf_Error *error) { - Dwarf_Loclist ll, next_ll; - Dwarf_Locdesc *ld; + Dwarf_Locdesc *ld, **llbuf; Dwarf_Section *ds; + Dwarf_Signed listlen; int i, ret; + /* + * Note that this API sometimes will not work correctly because + * it assumes that all units have the same pointer size and offset + * size. + */ + if (dbg == NULL || hipc == NULL || lopc == NULL || data == NULL || entry_len == NULL || next_entry == NULL) { DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT); return (DW_DLV_ERROR); } - ret = _dwarf_loclist_find(dbg, STAILQ_FIRST(&dbg->dbg_cu), offset, &ll, - error); + ret = _dwarf_loclist_find(dbg, STAILQ_FIRST(&dbg->dbg_cu), offset, + &llbuf, &listlen, entry_len, error); if (ret == DW_DLE_NO_ENTRY) { DWARF_SET_ERROR(dbg, error, DW_DLV_NO_ENTRY); return (DW_DLV_NO_ENTRY); @@ -204,8 +201,8 @@ dwarf_get_loclist_entry(Dwarf_Debug dbg, Dwarf_Unsigned offset, return (DW_DLV_ERROR); *hipc = *lopc = 0; - for (i = 0; i < ll->ll_ldlen; i++) { - ld = ll->ll_ldlist[i]; + for (i = 0; i < listlen; i++) { + ld = llbuf[i]; if (i == 0) { *hipc = ld->ld_hipc; *lopc = ld->ld_lopc; @@ -219,14 +216,8 @@ dwarf_get_loclist_entry(Dwarf_Debug dbg, Dwarf_Unsigned offset, ds = _dwarf_find_section(dbg, ".debug_loc"); assert(ds != NULL); - *data = (uint8_t *) ds->ds_data + ll->ll_offset; - *entry_len = ll->ll_length; - - next_ll = TAILQ_NEXT(ll, ll_next); - if (next_ll != NULL) - *next_entry = next_ll->ll_offset; - else - *next_entry = ds->ds_size; + *data = (uint8_t *) ds->ds_data + offset; + *next_entry = offset + *entry_len; return (DW_DLV_OK); } @@ -236,24 +227,9 @@ dwarf_loclist_from_expr(Dwarf_Debug dbg, Dwarf_Ptr bytes_in, Dwarf_Unsigned bytes_len, Dwarf_Locdesc **llbuf, Dwarf_Signed *listlen, Dwarf_Error *error) { - Dwarf_Locdesc *ld; - int ret; - - if (dbg == NULL || bytes_in == NULL || bytes_len == 0 || - llbuf == NULL || listlen == NULL) { - DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT); - return (DW_DLV_ERROR); - } - - ret = _dwarf_loc_fill_locexpr(dbg, &ld, bytes_in, bytes_len, - dbg->dbg_pointer_size, error); - if (ret != DW_DLE_NONE) - return (DW_DLV_ERROR); - - *llbuf = ld; - *listlen = 1; - return (DW_DLV_OK); + return (dwarf_loclist_from_expr_a(dbg, bytes_in, bytes_len, + dbg->dbg_pointer_size, llbuf, listlen, error)); } int @@ -261,6 +237,40 @@ dwarf_loclist_from_expr_a(Dwarf_Debug dbg, Dwarf_Ptr bytes_in, Dwarf_Unsigned bytes_len, Dwarf_Half addr_size, Dwarf_Locdesc **llbuf, Dwarf_Signed *listlen, Dwarf_Error *error) { + Dwarf_Half offset_size; + Dwarf_Small version; + + /* + * Obtain offset size and DWARF version from the current + * Compilation Unit or Type Unit. These values are needed + * for correctly parsing DW_OP_GNU_implicit_pointer operator. + * + * Note that dwarf_loclist_from_expr_b() should be used instead + * if the application knows correct values for offset size + * and DWARF version. + */ + if (dbg->dbg_cu_current) { + offset_size = dbg->dbg_cu_current->cu_length_size == 4 ? 4 : 8; + version = dbg->dbg_cu_current->cu_version; + } else if (dbg->dbg_tu_current) { + offset_size = dbg->dbg_tu_current->cu_length_size == 4 ? 4 : 8; + version = dbg->dbg_tu_current->cu_version; + } else { + /* Default values if no CU/TU context. */ + offset_size = 4; + version = 2; /* DWARF2 */ + } + + return (dwarf_loclist_from_expr_b(dbg, bytes_in, bytes_len, addr_size, + offset_size, version, llbuf, listlen, error)); +} + +int +dwarf_loclist_from_expr_b(Dwarf_Debug dbg, Dwarf_Ptr bytes_in, + Dwarf_Unsigned bytes_len, Dwarf_Half addr_size, Dwarf_Half offset_size, + Dwarf_Small version, Dwarf_Locdesc **llbuf, Dwarf_Signed *listlen, + Dwarf_Error *error) +{ Dwarf_Locdesc *ld; int ret; @@ -275,8 +285,13 @@ dwarf_loclist_from_expr_a(Dwarf_Debug dbg, Dwarf_Ptr bytes_in, return (DW_DLV_ERROR); } + if (offset_size != 4 && offset_size != 8) { + DWARF_SET_ERROR(dbg, error, DW_DLE_ARGUMENT); + return (DW_DLV_ERROR); + } + ret = _dwarf_loc_fill_locexpr(dbg, &ld, bytes_in, bytes_len, addr_size, - error); + offset_size, version, error); if (ret != DW_DLE_NONE) return (DW_DLV_ERROR); diff --git a/contrib/elftoolchain/libdwarf/dwarf_loclist_from_expr.3 b/contrib/elftoolchain/libdwarf/dwarf_loclist_from_expr.3 index d317f7e28004..d0eb88c464d2 100644 --- a/contrib/elftoolchain/libdwarf/dwarf_loclist_from_expr.3 +++ b/contrib/elftoolchain/libdwarf/dwarf_loclist_from_expr.3 @@ -1,4 +1,4 @@ -.\" Copyright (c) 2011 Kai Wang +.\" Copyright (c) 2011,2014 Kai Wang .\" All rights reserved. .\" .\" Redistribution and use in source and binary forms, with or without @@ -22,14 +22,15 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.\" $Id: dwarf_loclist_from_expr.3 2074 2011-10-27 03:34:33Z jkoshy $ +.\" $Id: dwarf_loclist_from_expr.3 3129 2014-12-21 20:06:26Z jkoshy $ .\" -.Dd July 6, 2011 +.Dd December 21, 2014 .Os .Dt DWARF_LOCLIST_FROM_EXPR 3 .Sh NAME .Nm dwarf_loclist_from_expr , -.Nm dwarf_loclist_from_expr_a +.Nm dwarf_loclist_from_expr_a , +.Nm dwarf_loclist_from_expr_b .Nd translate DWARF location expression bytes .Sh LIBRARY .Lb libdwarf @@ -54,6 +55,18 @@ .Fa "Dwarf_Signed *listlen" .Fa "Dwarf_Error *err" .Fc +.Ft int +.Fo dwarf_loclist_from_expr_b +.Fa "Dwarf_Debug dbg" +.Fa "Dwarf_Ptr bytes_in" +.Fa "Dwarf_Unsigned bytes_len" +.Fa "Dwarf_Half addr_size" +.Fa "Dwarf_Half offset_size" +.Fa "Dwarf_Small version" +.Fa "Dwarf_Locdesc **llbuf" +.Fa "Dwarf_Signed *listlen" +.Fa "Dwarf_Error *error" +.Fc .Sh DESCRIPTION Function .Fn dwarf_loclist_from_expr @@ -104,6 +117,21 @@ except that it requires one additional argument .Ar addr_size , which specifies the address size to use when translating the location expression bytes. +.Pp +Function +.Fn dwarf_loclist_from_expr_b +is identical to function +.Fn dwarf_loclist_from_expr_a +except that it requires two additional arguments for translating the +location expression bytes. +Argument +.Ar offset_size +specifies the offset size, and argument +.Ar version +specifies the DWARF version. +These values are required to correctly translate the +.Dv DW_OP_GNU_implicit_pointer +opcode. .Ss Memory Management The memory area used for the descriptor returned in argument .Ar llbuf diff --git a/contrib/elftoolchain/libdwarf/dwarf_next_cu_header.3 b/contrib/elftoolchain/libdwarf/dwarf_next_cu_header.3 index 5be00b8ec4ee..f68867d7f21e 100644 --- a/contrib/elftoolchain/libdwarf/dwarf_next_cu_header.3 +++ b/contrib/elftoolchain/libdwarf/dwarf_next_cu_header.3 @@ -1,4 +1,4 @@ -.\" Copyright (c) 2010 Kai Wang +.\" Copyright (c) 2010,2014 Kai Wang .\" All rights reserved. .\" .\" Redistribution and use in source and binary forms, with or without @@ -22,14 +22,15 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.\" $Id: dwarf_next_cu_header.3 2074 2011-10-27 03:34:33Z jkoshy $ +.\" $Id: dwarf_next_cu_header.3 3182 2015-04-10 16:08:10Z emaste $ .\" -.Dd July 24, 2010 +.Dd December 21, 2014 .Os .Dt DWARF_NEXT_CU_HEADER 3 .Sh NAME .Nm dwarf_next_cu_header , -.Nm dwarf_next_cu_header_b +.Nm dwarf_next_cu_header_b , +.Nm dwarf_next_cu_header_c .Nd step through compilation units in a DWARF debug context .Sh LIBRARY .Lb libdwarf @@ -57,33 +58,71 @@ .Fa "Dwarf_Unsigned *cu_next_offset" .Fa "Dwarf_Error *err" .Fc +.Ft int +.Fo dwarf_next_cu_header_c +.Fa "Dwarf_Debug dbg" +.Fa "Dwarf_Bool is_info" +.Fa "Dwarf_Unsigned *cu_length" +.Fa "Dwarf_Half *cu_version" +.Fa "Dwarf_Off *cu_abbrev_offset" +.Fa "Dwarf_Half *cu_pointer_size" +.Fa "Dwarf_Half *cu_offset_size" +.Fa "Dwarf_Half *cu_extension_size" +.Fa "Dwarf_Sig8 *type_signature" +.Fa "Dwarf_Unsigned *type_offset" +.Fa "Dwarf_Unsigned *cu_next_offset" +.Fa "Dwarf_Error *err" +.Fc .Sh DESCRIPTION -These functions are used to step through compilation unit contexts +These functions are used to step through compilation or type units associated with a DWARF debug context, optionally returning information about the unit. .Pp Function -.Fn dwarf_next_cu_header_b +.Fn dwarf_next_cu_header_c is the API recommended for new application code. +Function +.Fn dwarf_next_cu_header +and +.Fn dwarf_next_cu_header_b +can only operate on compilation units associated with the +.Dq \&.debug_info +section. +They are less general than function +.Fn dwarf_next_cu_header_c , +and are deprecated for use by new application code. +.Pp Argument .Ar dbg should reference a DWARF debug context allocated using .Xr dwarf_init 3 . +If argument +.Ar is_info +is set to 1, +the function returns information for compilation units found in the +.Dq \&.debug_info +section. +If argument +.Ar is_info +is set to 0, +the function returns information for type units found in the +.Dq \&.debug_types +sections. Argument .Ar cu_length should point to a location that will be set to the -length of the compilation unit. +length of the compilation or type unit. Argument .Ar cu_version should point to a location that will be set to the -version number for the compilation unit. +version number for the compilation or type unit. Argument .Ar cu_abbrev_offset should point to a location that will be set to the starting offset (in the .Dq .debug_abbrev section) of the set of debugging information entry abbreviations -associated with this compilation unit. +associated with this compilation or type unit. Argument .Ar cu_pointer_size should point to a location that will be set to the @@ -92,7 +131,7 @@ underlying object being debugged. Argument .Ar cu_offset_size should point to a location that will be set to the -size in bytes for a DWARF offset in the compilation unit. +size in bytes for a DWARF offset in the compilation or type unit. Argument .Ar cu_extension_size is only needed for processing MIPS/IRIX objects that use @@ -100,10 +139,26 @@ a non-standard DWARF format. It should point to a location that will be set to 4 for normal objects and to 0 for non-standard ones. Argument +.Ar type_signature +and +.Ar type_offset +is only needed for processing type units. +Argument +.Ar type_signature +should point to a location that will be set to the 64-bit unique signature +of the type described in the type unit. +Argument +.Ar type_offset +should point to a location that will be set to the offset of the debugging +information entry that describes the type. +Argument .Ar cu_next_offset should point to a location that will be set to the offset of the next compilation unit header in the .Dq \&.debug_info +section, +or the offset of the next type unit header in the +.Dq \&.debug_types section. Argument .Ar err @@ -111,42 +166,23 @@ should point to a location that will hold an error descriptor in case of an error. .Pp Function +.Fn dwarf_next_cu_header_b +is identical to function +.Fn dwarf_next_cu_header_c +except that it does not provide arguments +.Ar is_info , +.Ar type_signature +and +.Ar type_offset . +.Pp +Function .Fn dwarf_next_cu_header -is less general than -.Fn dwarf_next_cu_header_b , -and is deprecated for use by new application code. -Argument -.Ar dbg -should reference a DWARF debug context allocated using -.Xr dwarf_init 3 . -Argument -.Ar cu_length -should point to a location that will be set to the -length of the compilation unit. -Argument -.Ar cu_version -should point to a location that will be set to the -version number for the compilation unit. -Argument -.Ar cu_abbrev_offset -should point to a location that will be set to the -starting offset in the -.Dq .debug_abbrev -section of the set of debugging information entry abbreviations -associated with this compilation unit. -Argument -.Ar cu_pointer_size -should point to a location that will be set to the -size of an address in bytes for the machine architecture of the -underlying debugging object. -Argument -.Ar cu_next_offset -should point to a location that will be set to the -offset of the next compilation unit. -Argument -.Ar err -should point to a location that will hold an error descriptor in case -of an error. +is identical to function +.Fn dwarf_next_cu_header_b +except that it does not provide arguments +.Ar cu_offset_size +and +.Ar cu_extension_size . .Pp A value of NULL may be used for any of the arguments .Ar cu_length , @@ -155,30 +191,78 @@ A value of NULL may be used for any of the arguments .Ar cu_pointer_size , .Ar cu_offset_size , .Ar cu_extension_size , +.Ar type_signature , +.Ar type_offset , .Ar cu_next_offset and .Ar err if the caller is not interested in the respective value. .Ss Iterating Through Compilation Units in a Debug Context .Pp -The first call to functions -.Fn dwarf_next_cu_header_b -and -.Fn dwarf_next_cu_header -for a given debug context will return information about the first -compilation unit in the debug context. -Subsequent calls to these functions will iterate through the remaining -compilation units in the debug context. -On stepping past the last compilation unit in the debug context, -functions -.Fn dwarf_next_cu_header -and -.Fn dwarf_next_cu_header_b -return +The first call to function +.Fn dwarf_next_cu_header_c +for a given debug context with argument +.Ar is_info +set to 1 will return information about the first +compilation unit in the +.Dq \&.debug_info +section. +Subsequent calls to the function will iterate through the remaining +compilation units in the section. +On stepping past the last compilation unit in the section, +function +.Fn dwarf_next_cu_header_c +returns +.Dv DW_DLV_NO_ENTRY +and resets its internal state. +The next call to the function will restart from the first compilation +unit in the section. +.Ss Iterating Through Type Units in a Debug Context +When a DWARF debug context is allocated using +.Xr dwarf_init 3 , +an internal pointer associated with the context will point to the first +.Dq \&.debug_types +section found in the debug object. +The first call to function +.Fn dwarf_next_cu_header_c +for the debug context with argument +.Ar is_info +set to 0 will return information about the first +type unit in that +.Dq \&.debug_types +section. +Subsequent calls to the function will iterate through the remaining +type units in the section. +On stepping past the last type unit in the debug context, +function +.Fn dwarf_next_cu_header_c +returns +.Dv DW_DLV_NO_ENTRY +and resets its internal state. +The next call to the function will restart from the first type +unit in the +.Dq \&.debug_types +section. +.Pp +If the debug object contains multiple +.Dq \&.debug_types +sections, the function +.Fn dwarf_next_types_section +can be called to move the internal pointer to the next +.Dq \&.debug_types +section. +As a result, subsequent calls of the function +.Fn dwarf_next_cu_header_c +will operate on the new +.Dq \&.debug_types +section. +Function +.Fn dwarf_next_types_section +returns .Dv DW_DLV_NO_ENTRY -and reset their internal state. -The next call to these functions will restart from the first compilation -unit in the debug context. +when there are no more +.Dq \&.debug_types +sections left in the debug object. .Sh RETURN VALUES On success, these functions return .Dv DW_DLV_OK . @@ -200,4 +284,5 @@ was NULL. .Xr dwarf 3 , .Xr dwarf_get_cu_die_offset_given_cu_header_offset 3 , .Xr dwarf_init 3 , +.Xr dwarf_next_types_section 3 , .Xr dwarf_siblingof 3 diff --git a/contrib/elftoolchain/libdwarf/dwarf_next_types_section.3 b/contrib/elftoolchain/libdwarf/dwarf_next_types_section.3 new file mode 100644 index 000000000000..98ff70d82668 --- /dev/null +++ b/contrib/elftoolchain/libdwarf/dwarf_next_types_section.3 @@ -0,0 +1,134 @@ +.\" Copyright (c) 2014 Kai Wang +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (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: dwarf_next_types_section.3 3116 2014-12-20 18:26:55Z jkoshy $ +.\" +.Dd December 20, 2014 +.Os +.Dt DWARF_NEXT_TYPES_SECTION 3 +.Sh NAME +.Nm dwarf_next_types_section +.Nd step through .debug_types sections in a debug context +.Sh LIBRARY +.Lb libdwarf +.Sh SYNOPSIS +.In libdwarf.h +.Ft int +.Fo dwarf_next_types_section +.Fa "Dwarf_Debug dbg" +.Fa "Dwarf_Error *err" +.Fc +.Sh DESCRIPTION +Function +.Fn dwarf_next_types_section +steps through the +.Dq \&.debug_types +sections found in a debug context. +.Pp +Argument +.Ar dbg +should reference a DWARF debug context allocated using +.Xr dwarf_init 3 . +Argument +.Ar err +should point to a location that will hold an error descriptor in case +of an error. +.Pp +When a DWARF debug context is allocated using +.Xr dwarf_init 3 , +an internal pointer associated with the context will point to the +first +.Dq \&.debug_types +section present in the debug object. +When the application calls function +.Fn dwarf_next_types_section , +this internal pointer will move to the next +.Dq \&.debug_types +section present. +On stepping past the last +.Dq \&.debug_types +section left in the debug context, function +.Fn dwarf_next_types_section +returns +.Dv DW_DLV_NO_ENTRY . +The next call to the function will restart from the first +.Dq \&.debug_types +section in the debug context. +.Pp +Application code should call function +.Xr dwarf_next_cu_header_c 3 +to iterate though the type units associated with the current +.Dq \&.debug_types +section. +.Sh RETURN VALUES +On success, function +.Fn dwarf_next_types_section +returns +.Dv DW_DLV_OK . +.Pp +In case of an error, it returns +.Dv DW_DLV_ERROR +and sets argument +.Ar err . +When there are no more +.Dq \&.debug_types +sections left to traverse, it returns +.Dv DW_DLV_NO_ENTRY . +.Sh COMPATIBILITY +This function is an extension to the +.Xr DWARF 3 +API. +.Sh ERRORS +The +.Fn dwarf_next_types_section +function may fail with the following errors: +.Bl -tag -width ".Bq Er DW_DLE_ARGUMENT" +.It Bq Er DW_DLE_ARGUMENT +Argument +.Va dbg +was NULL. +.El +.Sh EXAMPLES +To iterate though every type unit in all the +.Dq \&.debug_types +sections found in a debug context: +.Bd -literal -offset indent +Dwarf_Debug dbg; +Dwarf_Sig8 sig8; +Dwarf_Unsigned typeoff; +Dwarf_Error de; + +\&... allocate dbg using dwarf_init() etc ... + +do { + while ((ret = dwarf_next_cu_header_c(dbg, 0, NULL, NULL, NULL, + NULL, NULL, NULL, &sig8, &typeoff, NULL, &de)) == DW_DLV_OK) { + /* Access DIEs etc ... */ + } +} while (dwarf_next_types_section(dbg, &de) == DW_DLV_OK); +.Ed +.Sh SEE ALSO +.Xr dwarf 3 , +.Xr dwarf_init 3 , +.Xr dwarf_next_cu_header_c 3 diff --git a/contrib/elftoolchain/libdwarf/dwarf_producer_init.3 b/contrib/elftoolchain/libdwarf/dwarf_producer_init.3 index 717335e45119..b8ca6a9ce657 100644 --- a/contrib/elftoolchain/libdwarf/dwarf_producer_init.3 +++ b/contrib/elftoolchain/libdwarf/dwarf_producer_init.3 @@ -22,7 +22,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.\" $Id: dwarf_producer_init.3 2074 2011-10-27 03:34:33Z jkoshy $ +.\" $Id: dwarf_producer_init.3 3182 2015-04-10 16:08:10Z emaste $ .\" .Dd August 20, 2011 .Os @@ -58,7 +58,7 @@ descriptor representing a DWARF producer instance. .Pp The argument .Ar errhand -should contain the adddress of a function to be called in case of an +should contain the address of a function to be called in case of an error. If this argument is .Dv NULL , diff --git a/contrib/elftoolchain/libdwarf/dwarf_ranges.c b/contrib/elftoolchain/libdwarf/dwarf_ranges.c index 9ad60832350d..2e01da553f7e 100644 --- a/contrib/elftoolchain/libdwarf/dwarf_ranges.c +++ b/contrib/elftoolchain/libdwarf/dwarf_ranges.c @@ -26,7 +26,7 @@ #include "_libdwarf.h" -ELFTC_VCSID("$Id: dwarf_ranges.c 2075 2011-10-27 03:47:28Z jkoshy $"); +ELFTC_VCSID("$Id: dwarf_ranges.c 3029 2014-04-21 23:26:02Z kaiwang27 $"); static int _dwarf_get_ranges(Dwarf_Debug dbg, Dwarf_CU cu, Dwarf_Off off, @@ -63,7 +63,7 @@ dwarf_get_ranges(Dwarf_Debug dbg, Dwarf_Off offset, Dwarf_Ranges **ranges, } if (!dbg->dbg_info_loaded) { - if (_dwarf_info_load(dbg, 1, error) != DW_DLE_NONE) + if (_dwarf_info_load(dbg, 1, 1, error) != DW_DLE_NONE) return (DW_DLV_ERROR); } diff --git a/contrib/elftoolchain/libdwarf/dwarf_reloc.c b/contrib/elftoolchain/libdwarf/dwarf_reloc.c index 5e96db3c6fd8..0430e4db49ce 100644 --- a/contrib/elftoolchain/libdwarf/dwarf_reloc.c +++ b/contrib/elftoolchain/libdwarf/dwarf_reloc.c @@ -26,15 +26,15 @@ #include "_libdwarf.h" -ELFTC_VCSID("$Id: dwarf_reloc.c 2075 2011-10-27 03:47:28Z jkoshy $"); +ELFTC_VCSID("$Id: dwarf_reloc.c 3161 2015-02-15 21:43:36Z emaste $"); int dwarf_set_reloc_application(int apply) { int oldapply; - oldapply = _libdwarf.applyrela; - _libdwarf.applyrela = apply; + oldapply = _libdwarf.applyreloc; + _libdwarf.applyreloc = apply; return (oldapply); } diff --git a/contrib/elftoolchain/libdwarf/dwarf_sections.c b/contrib/elftoolchain/libdwarf/dwarf_sections.c new file mode 100644 index 000000000000..d3c1379378fe --- /dev/null +++ b/contrib/elftoolchain/libdwarf/dwarf_sections.c @@ -0,0 +1,111 @@ +/*- + * Copyright (c) 2014 Kai Wang + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include "_libdwarf.h" + +ELFTC_VCSID("$Id: dwarf_sections.c 3036 2014-05-05 19:19:31Z kaiwang27 $"); + +#define SET(N, V) \ + do { \ + if ((N) != NULL) \ + *(N) = (V); \ + } while (0) + +int +dwarf_get_section_max_offsets_b(Dwarf_Debug dbg, Dwarf_Unsigned *debug_info, + Dwarf_Unsigned *debug_abbrev, Dwarf_Unsigned *debug_line, + Dwarf_Unsigned *debug_loc, Dwarf_Unsigned *debug_aranges, + Dwarf_Unsigned *debug_macinfo, Dwarf_Unsigned *debug_pubnames, + Dwarf_Unsigned *debug_str, Dwarf_Unsigned *debug_frame, + Dwarf_Unsigned *debug_ranges, Dwarf_Unsigned *debug_pubtypes, + Dwarf_Unsigned *debug_types) +{ + const char *n; + Dwarf_Unsigned sz; + int i; + + if (dbg == NULL) + return (DW_DLV_ERROR); + + SET(debug_info, 0); + SET(debug_abbrev, 0); + SET(debug_line, 0); + SET(debug_loc, 0); + SET(debug_aranges, 0); + SET(debug_macinfo, 0); + SET(debug_pubnames, 0); + SET(debug_str, 0); + SET(debug_frame, 0); + SET(debug_ranges, 0); + SET(debug_pubtypes, 0); + SET(debug_types, 0); + + for (i = 0; (Dwarf_Unsigned) i < dbg->dbg_seccnt; i++) { + n = dbg->dbg_section[i].ds_name; + sz = dbg->dbg_section[i].ds_size; + if (!strcmp(n, ".debug_info")) + SET(debug_info, sz); + else if (!strcmp(n, ".debug_abbrev")) + SET(debug_abbrev, sz); + else if (!strcmp(n, ".debug_line")) + SET(debug_line, sz); + else if (!strcmp(n, ".debug_loc")) + SET(debug_loc, sz); + else if (!strcmp(n, ".debug_aranges")) + SET(debug_aranges, sz); + else if (!strcmp(n, ".debug_macinfo")) + SET(debug_macinfo, sz); + else if (!strcmp(n, ".debug_pubnames")) + SET(debug_pubnames, sz); + else if (!strcmp(n, ".debug_str")) + SET(debug_str, sz); + else if (!strcmp(n, ".debug_frame")) + SET(debug_frame, sz); + else if (!strcmp(n, ".debug_ranges")) + SET(debug_ranges, sz); + else if (!strcmp(n, ".debug_pubtypes")) + SET(debug_pubtypes, sz); + else if (!strcmp(n, ".debug_types")) + SET(debug_types, sz); + } + + return (DW_DLV_OK); +} + +int +dwarf_get_section_max_offsets(Dwarf_Debug dbg, Dwarf_Unsigned *debug_info, + Dwarf_Unsigned *debug_abbrev, Dwarf_Unsigned *debug_line, + Dwarf_Unsigned *debug_loc, Dwarf_Unsigned *debug_aranges, + Dwarf_Unsigned *debug_macinfo, Dwarf_Unsigned *debug_pubnames, + Dwarf_Unsigned *debug_str, Dwarf_Unsigned *debug_frame, + Dwarf_Unsigned *debug_ranges, Dwarf_Unsigned *debug_pubtypes) +{ + + return (dwarf_get_section_max_offsets(dbg, debug_info, debug_abbrev, + debug_line, debug_loc, debug_aranges, debug_macinfo, + debug_pubnames, debug_str, debug_frame, debug_ranges, + debug_pubtypes)); +} diff --git a/contrib/elftoolchain/libdwarf/dwarf_set_reloc_application.3 b/contrib/elftoolchain/libdwarf/dwarf_set_reloc_application.3 index d53c746d1be2..db40cbb2522a 100644 --- a/contrib/elftoolchain/libdwarf/dwarf_set_reloc_application.3 +++ b/contrib/elftoolchain/libdwarf/dwarf_set_reloc_application.3 @@ -22,9 +22,9 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.\" $Id: dwarf_set_reloc_application.3 2075 2011-10-27 03:47:28Z jkoshy $ +.\" $Id: dwarf_set_reloc_application.3 3161 2015-02-15 21:43:36Z emaste $ .\" -.Dd June 26, 2011 +.Dd February 11, 2015 .Os .Dt DWARF_SET_RELOC_APPLICATION 3 .Sh NAME @@ -47,6 +47,8 @@ handled by the DWARF(3) library. If the argument .Ar apply holds a non-zero value, the library will process all the relevant +.Dq ".rel" +and .Dq ".rela" relocation sections and will apply the relocation records found to their corresponding DWARF sections. diff --git a/contrib/elftoolchain/libdwarf/dwarf_whatattr.3 b/contrib/elftoolchain/libdwarf/dwarf_whatattr.3 index 7c9a6d04bfcb..d7f6dd4e38b9 100644 --- a/contrib/elftoolchain/libdwarf/dwarf_whatattr.3 +++ b/contrib/elftoolchain/libdwarf/dwarf_whatattr.3 @@ -22,7 +22,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.\" $Id: dwarf_whatattr.3 2075 2011-10-27 03:47:28Z jkoshy $ +.\" $Id: dwarf_whatattr.3 3181 2015-04-10 13:22:51Z emaste $ .\" .Dd May 22, 2010 .Os @@ -51,7 +51,7 @@ and writes it to the location pointed to by argument If argument .Ar err is not NULL, it will be used to return an error descriptor in case -of an error. +of an error. .Sh RETURN VALUES On success, function .Fn dwarf_whatattr @@ -72,6 +72,7 @@ Either of argument or .Va retcode was NULL. +.El .Sh SEE ALSO .Xr dwarf 3 , .Xr dwarf_attr 3 , diff --git a/contrib/elftoolchain/libdwarf/libdwarf.c b/contrib/elftoolchain/libdwarf/libdwarf.c index 5f487624717f..b2406cb73fb9 100644 --- a/contrib/elftoolchain/libdwarf/libdwarf.c +++ b/contrib/elftoolchain/libdwarf/libdwarf.c @@ -26,10 +26,10 @@ #include "_libdwarf.h" -ELFTC_VCSID("$Id: libdwarf.c 2070 2011-10-27 03:05:32Z jkoshy $"); +ELFTC_VCSID("$Id: libdwarf.c 3161 2015-02-15 21:43:36Z emaste $"); struct _libdwarf_globals _libdwarf = { .errhand = NULL, .errarg = NULL, - .applyrela = 1 + .applyreloc = 1 }; diff --git a/contrib/elftoolchain/libdwarf/libdwarf.h b/contrib/elftoolchain/libdwarf/libdwarf.h index 105a0e7d96d2..0cb8b1ae8cbc 100644 --- a/contrib/elftoolchain/libdwarf/libdwarf.h +++ b/contrib/elftoolchain/libdwarf/libdwarf.h @@ -1,6 +1,6 @@ /*- * Copyright (c) 2007 John Birrell (jb@freebsd.org) - * Copyright (c) 2009-2011 Kai Wang + * Copyright (c) 2009-2011,2014 Kai Wang * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -24,7 +24,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: libdwarf.h 2576 2012-09-13 09:16:11Z jkoshy $ + * $Id: libdwarf.h 3174 2015-03-27 17:13:41Z emaste $ */ #ifndef _LIBDWARF_H_ @@ -47,7 +47,6 @@ typedef struct _Dwarf_ArangeSet *Dwarf_ArangeSet; typedef struct _Dwarf_Attribute *Dwarf_Attribute; typedef struct _Dwarf_Attribute *Dwarf_P_Attribute; typedef struct _Dwarf_AttrDef *Dwarf_AttrDef; -typedef struct _Dwarf_CU *Dwarf_CU; typedef struct _Dwarf_Cie *Dwarf_Cie; typedef struct _Dwarf_Cie *Dwarf_P_Cie; typedef struct _Dwarf_Debug *Dwarf_Debug; @@ -60,7 +59,6 @@ typedef struct _Dwarf_FrameSec *Dwarf_FrameSec; typedef struct _Dwarf_Line *Dwarf_Line; typedef struct _Dwarf_LineFile *Dwarf_LineFile; typedef struct _Dwarf_LineInfo *Dwarf_LineInfo; -typedef struct _Dwarf_Loclist *Dwarf_Loclist; typedef struct _Dwarf_MacroSet *Dwarf_MacroSet; typedef struct _Dwarf_NamePair *Dwarf_NamePair; typedef struct _Dwarf_NamePair *Dwarf_Func; @@ -441,11 +439,14 @@ enum Dwarf_ISA { DW_ISA_SPARC, DW_ISA_X86, DW_ISA_X86_64, + DW_ISA_AARCH64, DW_ISA_MAX }; /* Function prototype definitions. */ -__BEGIN_DECLS +#ifdef __cplusplus +extern "C" { +#endif Dwarf_P_Attribute dwarf_add_AT_comp_dir(Dwarf_P_Die, char *, Dwarf_Error *); Dwarf_P_Attribute dwarf_add_AT_const_value_signedint(Dwarf_P_Die, Dwarf_Signed, Dwarf_Error *); @@ -519,6 +520,7 @@ int dwarf_attr(Dwarf_Die, Dwarf_Half, Dwarf_Attribute *, Dwarf_Error *); int dwarf_attrlist(Dwarf_Die, Dwarf_Attribute **, Dwarf_Signed *, Dwarf_Error *); +int dwarf_attroffset(Dwarf_Attribute, Dwarf_Off *, Dwarf_Error *); int dwarf_attrval_flag(Dwarf_Die, Dwarf_Half, Dwarf_Bool *, Dwarf_Error *); int dwarf_attrval_signed(Dwarf_Die, Dwarf_Half, Dwarf_Signed *, @@ -626,6 +628,9 @@ int dwarf_get_cu_die_offset(Dwarf_Arange, Dwarf_Off *, Dwarf_Error *); int dwarf_get_cu_die_offset_given_cu_header_offset(Dwarf_Debug, Dwarf_Off, Dwarf_Off *, Dwarf_Error *); +int dwarf_get_cu_die_offset_given_cu_header_offset_b(Dwarf_Debug, + Dwarf_Off, Dwarf_Bool, Dwarf_Off *, Dwarf_Error *); +Dwarf_Bool dwarf_get_die_infotypes_flag(Dwarf_Die); int dwarf_get_elf(Dwarf_Debug, Elf **, Dwarf_Error *); int dwarf_get_fde_at_pc(Dwarf_Fde *, Dwarf_Addr, Dwarf_Fde *, Dwarf_Addr *, Dwarf_Addr *, Dwarf_Error *); @@ -678,6 +683,16 @@ int dwarf_get_relocation_info_count(Dwarf_P_Debug, Dwarf_Unsigned *, int *, Dwarf_Error *); Dwarf_Ptr dwarf_get_section_bytes(Dwarf_P_Debug, Dwarf_Signed, Dwarf_Signed *, Dwarf_Unsigned *, Dwarf_Error *); +int dwarf_get_section_max_offsets(Dwarf_Debug, Dwarf_Unsigned *, + Dwarf_Unsigned *, Dwarf_Unsigned *, Dwarf_Unsigned *, + Dwarf_Unsigned *, Dwarf_Unsigned *, Dwarf_Unsigned *, + Dwarf_Unsigned *, Dwarf_Unsigned *, Dwarf_Unsigned *, + Dwarf_Unsigned *); +int dwarf_get_section_max_offsets_b(Dwarf_Debug, Dwarf_Unsigned *, + Dwarf_Unsigned *, Dwarf_Unsigned *, Dwarf_Unsigned *, + Dwarf_Unsigned *, Dwarf_Unsigned *, Dwarf_Unsigned *, + Dwarf_Unsigned *, Dwarf_Unsigned *, Dwarf_Unsigned *, + Dwarf_Unsigned *, Dwarf_Unsigned *); int dwarf_get_str(Dwarf_Debug, Dwarf_Off, char **, Dwarf_Signed *, Dwarf_Error *); int dwarf_get_types(Dwarf_Debug, Dwarf_Type **, Dwarf_Signed *, @@ -700,6 +715,8 @@ int dwarf_hasattr(Dwarf_Die, Dwarf_Half, Dwarf_Bool *, int dwarf_hasform(Dwarf_Attribute, Dwarf_Half, Dwarf_Bool *, Dwarf_Error *); int dwarf_highpc(Dwarf_Die, Dwarf_Addr *, Dwarf_Error *); +int dwarf_highpc_b(Dwarf_Die, Dwarf_Addr *, Dwarf_Half *, + enum Dwarf_Form_Class *, Dwarf_Error *); int dwarf_init(int, int, Dwarf_Handler, Dwarf_Ptr, Dwarf_Debug *, Dwarf_Error *); int dwarf_line_srcfileno(Dwarf_Line, Dwarf_Unsigned *, @@ -722,6 +739,10 @@ int dwarf_loclist_from_expr(Dwarf_Debug, Dwarf_Ptr, Dwarf_Unsigned, int dwarf_loclist_from_expr_a(Dwarf_Debug, Dwarf_Ptr, Dwarf_Unsigned, Dwarf_Half, Dwarf_Locdesc **, Dwarf_Signed *, Dwarf_Error *); +int dwarf_loclist_from_expr_b(Dwarf_Debug, Dwarf_Ptr, + Dwarf_Unsigned, Dwarf_Half, Dwarf_Half, + Dwarf_Small, Dwarf_Locdesc **, Dwarf_Signed *, + Dwarf_Error *); int dwarf_loclist_n(Dwarf_Attribute, Dwarf_Locdesc ***, Dwarf_Signed *, Dwarf_Error *); int dwarf_lowpc(Dwarf_Die, Dwarf_Addr *, Dwarf_Error *); @@ -735,11 +756,18 @@ int dwarf_next_cu_header(Dwarf_Debug, Dwarf_Unsigned *, int dwarf_next_cu_header_b(Dwarf_Debug, Dwarf_Unsigned *, Dwarf_Half *, Dwarf_Off *, Dwarf_Half *, Dwarf_Half *, Dwarf_Half *, Dwarf_Unsigned *, Dwarf_Error *); +int dwarf_next_cu_header_c(Dwarf_Debug, Dwarf_Bool, + Dwarf_Unsigned *, Dwarf_Half *, Dwarf_Off *, Dwarf_Half *, + Dwarf_Half *, Dwarf_Half *, Dwarf_Sig8 *, Dwarf_Unsigned *, + Dwarf_Unsigned *, Dwarf_Error *); +int dwarf_next_types_section(Dwarf_Debug, Dwarf_Error *); int dwarf_object_finish(Dwarf_Debug, Dwarf_Error *); int dwarf_object_init(Dwarf_Obj_Access_Interface *, Dwarf_Handler, Dwarf_Ptr, Dwarf_Debug *, Dwarf_Error *); int dwarf_offdie(Dwarf_Debug, Dwarf_Off, Dwarf_Die *, Dwarf_Error *); +int dwarf_offdie_b(Dwarf_Debug, Dwarf_Off, Dwarf_Bool, Dwarf_Die *, + Dwarf_Error *); Dwarf_Unsigned dwarf_producer_finish(Dwarf_P_Debug, Dwarf_Error *); Dwarf_P_Debug dwarf_producer_init(Dwarf_Unsigned, Dwarf_Callback_Func, Dwarf_Handler, Dwarf_Ptr, Dwarf_Error *); @@ -765,6 +793,8 @@ int dwarf_set_reloc_application(int); Dwarf_Ptr dwarf_seterrarg(Dwarf_Debug, Dwarf_Ptr); Dwarf_Handler dwarf_seterrhand(Dwarf_Debug, Dwarf_Handler); int dwarf_siblingof(Dwarf_Debug, Dwarf_Die, Dwarf_Die *, Dwarf_Error *); +int dwarf_siblingof_b(Dwarf_Debug, Dwarf_Die, Dwarf_Die *, Dwarf_Bool, + Dwarf_Error *); int dwarf_srcfiles(Dwarf_Die, char ***, Dwarf_Signed *, Dwarf_Error *); int dwarf_srclang(Dwarf_Die, Dwarf_Unsigned *, Dwarf_Error *); int dwarf_srclines(Dwarf_Die, Dwarf_Line **, Dwarf_Signed *, @@ -803,6 +833,8 @@ int dwarf_whatattr(Dwarf_Attribute, Dwarf_Half *, Dwarf_Error *); int dwarf_whatform(Dwarf_Attribute, Dwarf_Half *, Dwarf_Error *); int dwarf_whatform_direct(Dwarf_Attribute, Dwarf_Half *, Dwarf_Error *); -__END_DECLS +#ifdef __cplusplus +} +#endif #endif /* !_LIBDWARF_H_ */ diff --git a/contrib/elftoolchain/libdwarf/libdwarf_abbrev.c b/contrib/elftoolchain/libdwarf/libdwarf_abbrev.c index f4a395df0360..abcc2fda9580 100644 --- a/contrib/elftoolchain/libdwarf/libdwarf_abbrev.c +++ b/contrib/elftoolchain/libdwarf/libdwarf_abbrev.c @@ -27,7 +27,7 @@ #include "_libdwarf.h" -ELFTC_VCSID("$Id: libdwarf_abbrev.c 2070 2011-10-27 03:05:32Z jkoshy $"); +ELFTC_VCSID("$Id: libdwarf_abbrev.c 3136 2014-12-24 16:04:38Z kaiwang27 $"); int _dwarf_abbrev_add(Dwarf_CU cu, uint64_t entry, uint64_t tag, uint8_t children, @@ -180,7 +180,9 @@ _dwarf_abbrev_find(Dwarf_CU cu, uint64_t entry, Dwarf_Abbrev *abp, /* Load and search the abbrev table. */ ds = _dwarf_find_section(cu->cu_dbg, ".debug_abbrev"); - assert(ds != NULL); + if (ds == NULL) + return (DW_DLE_NO_ENTRY); + offset = cu->cu_abbrev_offset_cur; while (offset < ds->ds_size) { ret = _dwarf_abbrev_parse(cu->cu_dbg, cu, &offset, &ab, error); diff --git a/contrib/elftoolchain/libdwarf/libdwarf_arange.c b/contrib/elftoolchain/libdwarf/libdwarf_arange.c index 75434f6ae20b..eefb63ba6b10 100644 --- a/contrib/elftoolchain/libdwarf/libdwarf_arange.c +++ b/contrib/elftoolchain/libdwarf/libdwarf_arange.c @@ -26,7 +26,7 @@ #include "_libdwarf.h" -ELFTC_VCSID("$Id: libdwarf_arange.c 2070 2011-10-27 03:05:32Z jkoshy $"); +ELFTC_VCSID("$Id: libdwarf_arange.c 3029 2014-04-21 23:26:02Z kaiwang27 $"); void _dwarf_arange_cleanup(Dwarf_Debug dbg) @@ -67,7 +67,7 @@ _dwarf_arange_init(Dwarf_Debug dbg, Dwarf_Error *error) return (DW_DLE_NONE); if (!dbg->dbg_info_loaded) { - ret = _dwarf_info_load(dbg, 1, error); + ret = _dwarf_info_load(dbg, 1, 1, error); if (ret != DW_DLE_NONE) return (ret); } @@ -137,8 +137,8 @@ _dwarf_arange_init(Dwarf_Debug dbg, Dwarf_Error *error) /* Build arange array. */ if (dbg->dbg_arange_cnt > 0) { - if ((dbg->dbg_arange_array = malloc(dbg->dbg_arange_cnt * - sizeof(struct _Dwarf_Arange))) == NULL) { + if ((dbg->dbg_arange_array = malloc(dbg->dbg_arange_cnt * + sizeof(Dwarf_Arange))) == NULL) { DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY); ret = DW_DLE_MEMORY; goto fail_cleanup; diff --git a/contrib/elftoolchain/libdwarf/libdwarf_attr.c b/contrib/elftoolchain/libdwarf/libdwarf_attr.c index a7fb71cd1f24..dfbbc484c352 100644 --- a/contrib/elftoolchain/libdwarf/libdwarf_attr.c +++ b/contrib/elftoolchain/libdwarf/libdwarf_attr.c @@ -27,7 +27,7 @@ #include "_libdwarf.h" -ELFTC_VCSID("$Id: libdwarf_attr.c 2966 2013-09-21 14:40:14Z kaiwang27 $"); +ELFTC_VCSID("$Id: libdwarf_attr.c 3064 2014-06-06 19:35:55Z kaiwang27 $"); int _dwarf_attr_alloc(Dwarf_Die die, Dwarf_Attribute *atp, Dwarf_Error *error) @@ -106,6 +106,7 @@ _dwarf_attr_init(Dwarf_Debug dbg, Dwarf_Section *ds, uint64_t *offsetp, ret = DW_DLE_NONE; memset(&atref, 0, sizeof(atref)); atref.at_die = die; + atref.at_offset = *offsetp; atref.at_attrib = ad->ad_attrib; atref.at_form = indirect ? form : ad->ad_form; atref.at_indirect = indirect; @@ -162,7 +163,7 @@ _dwarf_attr_init(Dwarf_Debug dbg, Dwarf_Section *ds, uint64_t *offsetp, if (cu->cu_version == 2) atref.u[0].u64 = dbg->read(ds->ds_data, offsetp, cu->cu_pointer_size); - else if (cu->cu_version == 3) + else atref.u[0].u64 = dbg->read(ds->ds_data, offsetp, dwarf_size); break; diff --git a/contrib/elftoolchain/libdwarf/libdwarf_die.c b/contrib/elftoolchain/libdwarf/libdwarf_die.c index 4572875ed0d7..b7796d3cb6b0 100644 --- a/contrib/elftoolchain/libdwarf/libdwarf_die.c +++ b/contrib/elftoolchain/libdwarf/libdwarf_die.c @@ -27,7 +27,7 @@ #include "_libdwarf.h" -ELFTC_VCSID("$Id: libdwarf_die.c 2948 2013-05-30 21:25:52Z kaiwang27 $"); +ELFTC_VCSID("$Id: libdwarf_die.c 3039 2014-05-18 15:10:56Z kaiwang27 $"); int _dwarf_die_alloc(Dwarf_Debug dbg, Dwarf_Die *ret_die, Dwarf_Error *error) @@ -81,6 +81,7 @@ Dwarf_Die _dwarf_die_find(Dwarf_Die die, Dwarf_Unsigned off) { Dwarf_Debug dbg; + Dwarf_Section *ds; Dwarf_CU cu; Dwarf_Die die1; Dwarf_Error de; @@ -88,9 +89,10 @@ _dwarf_die_find(Dwarf_Die die, Dwarf_Unsigned off) cu = die->die_cu; dbg = die->die_dbg; + ds = cu->cu_is_info ? dbg->dbg_info_sec : dbg->dbg_types_sec; - ret = _dwarf_die_parse(dbg, dbg->dbg_info_sec, cu, cu->cu_dwarf_size, - off, cu->cu_next_offset, &die1, 0, &de); + ret = _dwarf_die_parse(dbg, ds, cu, cu->cu_dwarf_size, off, + cu->cu_next_offset, &die1, 0, &de); if (ret == DW_DLE_NONE) return (die1); diff --git a/contrib/elftoolchain/libdwarf/libdwarf_elf_init.c b/contrib/elftoolchain/libdwarf/libdwarf_elf_init.c index 1e374f2d793e..af2d370b980c 100644 --- a/contrib/elftoolchain/libdwarf/libdwarf_elf_init.c +++ b/contrib/elftoolchain/libdwarf/libdwarf_elf_init.c @@ -26,7 +26,7 @@ #include "_libdwarf.h" -ELFTC_VCSID("$Id: libdwarf_elf_init.c 2972 2013-12-23 06:46:04Z kaiwang27 $"); +ELFTC_VCSID("$Id: libdwarf_elf_init.c 3161 2015-02-15 21:43:36Z emaste $"); static const char *debug_name[] = { ".debug_abbrev", @@ -50,32 +50,46 @@ static const char *debug_name[] = { }; static void -_dwarf_elf_apply_reloc(Dwarf_Debug dbg, void *buf, Elf_Data *rel_data, - Elf_Data *symtab_data, int endian) +_dwarf_elf_write_reloc(Dwarf_Debug dbg, Elf_Data *symtab_data, int endian, + void *buf, uint64_t offset, GElf_Xword r_info, GElf_Sxword r_addend) { - Dwarf_Unsigned type; - GElf_Rela rela; GElf_Sym sym; - size_t symndx; - uint64_t offset; - int size, j; + int size; + + if (gelf_getsym(symtab_data, GELF_R_SYM(r_info), &sym) == NULL) + return; + if ((size = _dwarf_get_reloc_size(dbg, GELF_R_TYPE(r_info))) == 0) + return; /* Unknown or non-absolute relocation. */ + if (endian == ELFDATA2MSB) + _dwarf_write_msb(buf, &offset, sym.st_value + r_addend, size); + else + _dwarf_write_lsb(buf, &offset, sym.st_value + r_addend, size); +} - j = 0; - while (gelf_getrela(rel_data, j++, &rela) != NULL) { - symndx = GELF_R_SYM(rela.r_info); - type = GELF_R_TYPE(rela.r_info); +static void +_dwarf_elf_apply_rel_reloc(Dwarf_Debug dbg, void *buf, Elf_Data *rel_data, + Elf_Data *symtab_data, int endian) +{ + GElf_Rel rel; + int j; - if (gelf_getsym(symtab_data, symndx, &sym) == NULL) - continue; + j = 0; + while (gelf_getrel(rel_data, j++, &rel) != NULL) + _dwarf_elf_write_reloc(dbg, symtab_data, endian, buf, + rel.r_offset, rel.r_info, 0); +} - offset = rela.r_offset; - size = _dwarf_get_reloc_size(dbg, type); +static void +_dwarf_elf_apply_rela_reloc(Dwarf_Debug dbg, void *buf, Elf_Data *rel_data, + Elf_Data *symtab_data, int endian) +{ + GElf_Rela rela; + int j; - if (endian == ELFDATA2MSB) - _dwarf_write_msb(buf, &offset, rela.r_addend, size); - else - _dwarf_write_lsb(buf, &offset, rela.r_addend, size); - } + j = 0; + while (gelf_getrela(rel_data, j++, &rela) != NULL) + _dwarf_elf_write_reloc(dbg, symtab_data, endian, buf, + rela.r_offset, rela.r_info, rela.r_addend); } static int @@ -104,7 +118,8 @@ _dwarf_elf_relocate(Dwarf_Debug dbg, Elf *elf, Dwarf_Elf_Data *ed, size_t shndx, return (DW_DLE_ELF); } - if (sh.sh_type != SHT_RELA || sh.sh_size == 0) + if ((sh.sh_type != SHT_REL && sh.sh_type != SHT_RELA) || + sh.sh_size == 0) continue; if (sh.sh_info == shndx && sh.sh_link == symtab) { @@ -125,8 +140,12 @@ _dwarf_elf_relocate(Dwarf_Debug dbg, Elf *elf, Dwarf_Elf_Data *ed, size_t shndx, } memcpy(ed->ed_alloc, ed->ed_data->d_buf, ed->ed_data->d_size); - _dwarf_elf_apply_reloc(dbg, ed->ed_alloc, rel, - symtab_data, eh.e_ident[EI_DATA]); + if (sh.sh_type == SHT_REL) + _dwarf_elf_apply_rel_reloc(dbg, ed->ed_alloc, + rel, symtab_data, eh.e_ident[EI_DATA]); + else + _dwarf_elf_apply_rela_reloc(dbg, ed->ed_alloc, + rel, symtab_data, eh.e_ident[EI_DATA]); return (DW_DLE_NONE); } @@ -282,7 +301,7 @@ _dwarf_elf_init(Dwarf_Debug dbg, Elf *elf, Dwarf_Error *error) } } - if (_libdwarf.applyrela) { + if (_libdwarf.applyreloc) { if (_dwarf_elf_relocate(dbg, elf, &e->eo_data[j], elf_ndxscn(scn), symtab_ndx, symtab_data, error) != DW_DLE_NONE) diff --git a/contrib/elftoolchain/libdwarf/libdwarf_frame.c b/contrib/elftoolchain/libdwarf/libdwarf_frame.c index 375d64f5f66f..dd331379a4fd 100644 --- a/contrib/elftoolchain/libdwarf/libdwarf_frame.c +++ b/contrib/elftoolchain/libdwarf/libdwarf_frame.c @@ -1,5 +1,5 @@ /*- - * Copyright (c) 2009-2011 Kai Wang + * Copyright (c) 2009-2011,2014 Kai Wang * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -26,7 +26,7 @@ #include "_libdwarf.h" -ELFTC_VCSID("$Id: libdwarf_frame.c 2529 2012-07-29 23:31:12Z kaiwang27 $"); +ELFTC_VCSID("$Id: libdwarf_frame.c 3106 2014-12-19 16:00:58Z kaiwang27 $"); static int _dwarf_frame_find_cie(Dwarf_FrameSec fs, Dwarf_Unsigned offset, @@ -49,8 +49,9 @@ _dwarf_frame_find_cie(Dwarf_FrameSec fs, Dwarf_Unsigned offset, } static int -_dwarf_frame_read_lsb_encoded(Dwarf_Debug dbg, uint64_t *val, uint8_t *data, - uint64_t *offsetp, uint8_t encode, Dwarf_Addr pc, Dwarf_Error *error) +_dwarf_frame_read_lsb_encoded(Dwarf_Debug dbg, Dwarf_Cie cie, uint64_t *val, + uint8_t *data, uint64_t *offsetp, uint8_t encode, Dwarf_Addr pc, + Dwarf_Error *error) { uint8_t application; @@ -62,7 +63,7 @@ _dwarf_frame_read_lsb_encoded(Dwarf_Debug dbg, uint64_t *val, uint8_t *data, switch (encode) { case DW_EH_PE_absptr: - *val = dbg->read(data, offsetp, dbg->dbg_pointer_size); + *val = dbg->read(data, offsetp, cie->cie_addrsize); break; case DW_EH_PE_uleb128: *val = _dwarf_read_uleb128(data, offsetp); @@ -149,7 +150,7 @@ _dwarf_frame_parse_lsb_cie_augment(Dwarf_Debug dbg, Dwarf_Cie cie, /* Skip two augments in augment data. */ encode = *augdata_p++; offset = 0; - ret = _dwarf_frame_read_lsb_encoded(dbg, &val, + ret = _dwarf_frame_read_lsb_encoded(dbg, cie, &val, augdata_p, &offset, encode, 0, error); if (ret != DW_DLE_NONE) return (ret); @@ -233,6 +234,18 @@ _dwarf_frame_add_cie(Dwarf_Debug dbg, Dwarf_FrameSec fs, Dwarf_Section *ds, cie->cie_ehdata = dbg->read(ds->ds_data, off, dbg->dbg_pointer_size); + /* DWARF4 added "address_size" and "segment_size". */ + if (cie->cie_version == 4) { + cie->cie_addrsize = dbg->read(ds->ds_data, off, 1); + cie->cie_segmentsize = dbg->read(ds->ds_data, off, 1); + } else { + /* + * Otherwise (DWARF[23]) we just set CIE addrsize to the + * debug context pointer size. + */ + cie->cie_addrsize = dbg->dbg_pointer_size; + } + cie->cie_caf = _dwarf_read_uleb128(ds->ds_data, off); cie->cie_daf = _dwarf_read_sleb128(ds->ds_data, off); @@ -345,8 +358,9 @@ _dwarf_frame_add_fde(Dwarf_Debug dbg, Dwarf_FrameSec fs, Dwarf_Section *ds, * The FDE PC start/range for .eh_frame is encoded according * to the LSB spec's extension to DWARF2. */ - ret = _dwarf_frame_read_lsb_encoded(dbg, &val, ds->ds_data, - off, cie->cie_fde_encode, ds->ds_addr + *off, error); + ret = _dwarf_frame_read_lsb_encoded(dbg, cie, &val, + ds->ds_data, off, cie->cie_fde_encode, ds->ds_addr + *off, + error); if (ret != DW_DLE_NONE) return (ret); fde->fde_initloc = val; @@ -354,16 +368,16 @@ _dwarf_frame_add_fde(Dwarf_Debug dbg, Dwarf_FrameSec fs, Dwarf_Section *ds, * FDE PC range should not be relative value to anything. * So pass 0 for pc value. */ - ret = _dwarf_frame_read_lsb_encoded(dbg, &val, ds->ds_data, - off, cie->cie_fde_encode, 0, error); + ret = _dwarf_frame_read_lsb_encoded(dbg, cie, &val, + ds->ds_data, off, cie->cie_fde_encode, 0, error); if (ret != DW_DLE_NONE) return (ret); fde->fde_adrange = val; } else { fde->fde_initloc = dbg->read(ds->ds_data, off, - dbg->dbg_pointer_size); + cie->cie_addrsize); fde->fde_adrange = dbg->read(ds->ds_data, off, - dbg->dbg_pointer_size); + cie->cie_addrsize); } /* Optional FDE augmentation data for .eh_frame section. (ignored) */ @@ -530,9 +544,9 @@ fail_cleanup: } static int -_dwarf_frame_run_inst(Dwarf_Debug dbg, Dwarf_Regtable3 *rt, uint8_t *insts, - Dwarf_Unsigned len, Dwarf_Unsigned caf, Dwarf_Signed daf, Dwarf_Addr pc, - Dwarf_Addr pc_req, Dwarf_Addr *row_pc, Dwarf_Error *error) +_dwarf_frame_run_inst(Dwarf_Debug dbg, Dwarf_Regtable3 *rt, uint8_t addr_size, + uint8_t *insts, Dwarf_Unsigned len, Dwarf_Unsigned caf, Dwarf_Signed daf, + Dwarf_Addr pc, Dwarf_Addr pc_req, Dwarf_Addr *row_pc, Dwarf_Error *error) { Dwarf_Regtable3 *init_rt, *saved_rt; uint8_t *p, *pe; @@ -632,7 +646,7 @@ _dwarf_frame_run_inst(Dwarf_Debug dbg, Dwarf_Regtable3 *rt, uint8_t *insts, switch (low6) { case DW_CFA_set_loc: - pc = dbg->decode(&p, dbg->dbg_pointer_size); + pc = dbg->decode(&p, addr_size); #ifdef FRAME_DEBUG printf("DW_CFA_set_loc(pc=%#jx)\n", pc); #endif @@ -898,14 +912,13 @@ program_done: } static int -_dwarf_frame_convert_inst(Dwarf_Debug dbg, uint8_t *insts, Dwarf_Unsigned len, - Dwarf_Unsigned *count, Dwarf_Frame_Op *fop, Dwarf_Frame_Op3 *fop3, - Dwarf_Error *error) +_dwarf_frame_convert_inst(Dwarf_Debug dbg, uint8_t addr_size, uint8_t *insts, + Dwarf_Unsigned len, Dwarf_Unsigned *count, Dwarf_Frame_Op *fop, + Dwarf_Frame_Op3 *fop3, Dwarf_Error *error) { uint8_t *p, *pe; uint8_t high2, low6; uint64_t reg, reg2, uoff, soff, blen; - int ret; #define SET_BASE_OP(x) \ do { \ @@ -970,7 +983,6 @@ _dwarf_frame_convert_inst(Dwarf_Debug dbg, uint8_t *insts, Dwarf_Unsigned len, } \ } while(0) - ret = DW_DLE_NONE; *count = 0; p = insts; @@ -1020,7 +1032,7 @@ _dwarf_frame_convert_inst(Dwarf_Debug dbg, uint8_t *insts, Dwarf_Unsigned len, switch (low6) { case DW_CFA_set_loc: - uoff = dbg->decode(&p, dbg->dbg_pointer_size); + uoff = dbg->decode(&p, addr_size); SET_OFFSET(uoff); break; case DW_CFA_advance_loc1: @@ -1103,15 +1115,16 @@ _dwarf_frame_convert_inst(Dwarf_Debug dbg, uint8_t *insts, Dwarf_Unsigned len, } int -_dwarf_frame_get_fop(Dwarf_Debug dbg, uint8_t *insts, Dwarf_Unsigned len, - Dwarf_Frame_Op **ret_oplist, Dwarf_Signed *ret_opcnt, Dwarf_Error *error) +_dwarf_frame_get_fop(Dwarf_Debug dbg, uint8_t addr_size, uint8_t *insts, + Dwarf_Unsigned len, Dwarf_Frame_Op **ret_oplist, Dwarf_Signed *ret_opcnt, + Dwarf_Error *error) { Dwarf_Frame_Op *oplist; Dwarf_Unsigned count; int ret; - ret = _dwarf_frame_convert_inst(dbg, insts, len, &count, NULL, NULL, - error); + ret = _dwarf_frame_convert_inst(dbg, addr_size, insts, len, &count, + NULL, NULL, error); if (ret != DW_DLE_NONE) return (ret); @@ -1120,8 +1133,8 @@ _dwarf_frame_get_fop(Dwarf_Debug dbg, uint8_t *insts, Dwarf_Unsigned len, return (DW_DLE_MEMORY); } - ret = _dwarf_frame_convert_inst(dbg, insts, len, &count, oplist, NULL, - error); + ret = _dwarf_frame_convert_inst(dbg, addr_size, insts, len, &count, + oplist, NULL, error); if (ret != DW_DLE_NONE) { free(oplist); return (ret); @@ -1201,17 +1214,17 @@ _dwarf_frame_get_internal_table(Dwarf_Fde fde, Dwarf_Addr pc_req, /* Run initial instructions in CIE. */ cie = fde->fde_cie; assert(cie != NULL); - ret = _dwarf_frame_run_inst(dbg, rt, cie->cie_initinst, - cie->cie_instlen, cie->cie_caf, cie->cie_daf, 0, ~0ULL, - &row_pc, error); + ret = _dwarf_frame_run_inst(dbg, rt, cie->cie_addrsize, + cie->cie_initinst, cie->cie_instlen, cie->cie_caf, cie->cie_daf, 0, + ~0ULL, &row_pc, error); if (ret != DW_DLE_NONE) return (ret); /* Run instructions in FDE. */ if (pc_req >= fde->fde_initloc) { - ret = _dwarf_frame_run_inst(dbg, rt, fde->fde_inst, - fde->fde_instlen, cie->cie_caf, cie->cie_daf, - fde->fde_initloc, pc_req, &row_pc, error); + ret = _dwarf_frame_run_inst(dbg, rt, cie->cie_addrsize, + fde->fde_inst, fde->fde_instlen, cie->cie_caf, + cie->cie_daf, fde->fde_initloc, pc_req, &row_pc, error); if (ret != DW_DLE_NONE) return (ret); } diff --git a/contrib/elftoolchain/libdwarf/libdwarf_info.c b/contrib/elftoolchain/libdwarf/libdwarf_info.c index dc82b7ddfde8..74765930aaed 100644 --- a/contrib/elftoolchain/libdwarf/libdwarf_info.c +++ b/contrib/elftoolchain/libdwarf/libdwarf_info.c @@ -1,6 +1,6 @@ /*- * Copyright (c) 2007 John Birrell (jb@freebsd.org) - * Copyright (c) 2010,2011 Kai Wang + * Copyright (c) 2010,2011,2014 Kai Wang * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -27,7 +27,7 @@ #include "_libdwarf.h" -ELFTC_VCSID("$Id: libdwarf_info.c 2942 2013-05-04 23:03:54Z kaiwang27 $"); +ELFTC_VCSID("$Id: libdwarf_info.c 3136 2014-12-24 16:04:38Z kaiwang27 $"); int _dwarf_info_first_cu(Dwarf_Debug dbg, Dwarf_Error *error) @@ -46,7 +46,7 @@ _dwarf_info_first_cu(Dwarf_Debug dbg, Dwarf_Error *error) return (DW_DLE_NO_ENTRY); dbg->dbg_info_off = 0; - ret = _dwarf_info_load(dbg, 0, error); + ret = _dwarf_info_load(dbg, 0, 1, error); if (ret != DW_DLE_NONE) return (ret); @@ -56,6 +56,32 @@ _dwarf_info_first_cu(Dwarf_Debug dbg, Dwarf_Error *error) } int +_dwarf_info_first_tu(Dwarf_Debug dbg, Dwarf_Error *error) +{ + Dwarf_CU tu; + int ret; + + assert(dbg->dbg_tu_current == NULL); + tu = STAILQ_FIRST(&dbg->dbg_tu); + if (tu != NULL) { + dbg->dbg_tu_current = tu; + return (DW_DLE_NONE); + } + + if (dbg->dbg_types_loaded) + return (DW_DLE_NO_ENTRY); + + dbg->dbg_types_off = 0; + ret = _dwarf_info_load(dbg, 0, 0, error); + if (ret != DW_DLE_NONE) + return (ret); + + dbg->dbg_tu_current = STAILQ_FIRST(&dbg->dbg_tu); + + return (DW_DLE_NONE); +} + +int _dwarf_info_next_cu(Dwarf_Debug dbg, Dwarf_Error *error) { Dwarf_CU cu; @@ -73,7 +99,7 @@ _dwarf_info_next_cu(Dwarf_Debug dbg, Dwarf_Error *error) return (DW_DLE_NO_ENTRY); } - ret = _dwarf_info_load(dbg, 0, error); + ret = _dwarf_info_load(dbg, 0, 1, error); if (ret != DW_DLE_NONE) return (ret); @@ -83,7 +109,35 @@ _dwarf_info_next_cu(Dwarf_Debug dbg, Dwarf_Error *error) } int -_dwarf_info_load(Dwarf_Debug dbg, int load_all, Dwarf_Error *error) +_dwarf_info_next_tu(Dwarf_Debug dbg, Dwarf_Error *error) +{ + Dwarf_CU cu; + int ret; + + assert(dbg->dbg_tu_current != NULL); + cu = STAILQ_NEXT(dbg->dbg_tu_current, cu_next); + if (cu != NULL) { + dbg->dbg_tu_current = cu; + return (DW_DLE_NONE); + } + + if (dbg->dbg_types_loaded) { + dbg->dbg_tu_current = NULL; + return (DW_DLE_NO_ENTRY); + } + + ret = _dwarf_info_load(dbg, 0, 0, error); + if (ret != DW_DLE_NONE) + return (ret); + + dbg->dbg_tu_current = STAILQ_NEXT(dbg->dbg_tu_current, cu_next); + + return (DW_DLE_NONE); +} + +int +_dwarf_info_load(Dwarf_Debug dbg, Dwarf_Bool load_all, Dwarf_Bool is_info, + Dwarf_Error *error) { Dwarf_CU cu; Dwarf_Section *ds; @@ -93,12 +147,23 @@ _dwarf_info_load(Dwarf_Debug dbg, int load_all, Dwarf_Error *error) uint64_t offset; ret = DW_DLE_NONE; - if (dbg->dbg_info_loaded) - return (DW_DLE_NONE); - offset = dbg->dbg_info_off; - ds = dbg->dbg_info_sec; - assert(ds != NULL); + if (is_info) { + if (dbg->dbg_info_loaded) + return (ret); + offset = dbg->dbg_info_off; + ds = dbg->dbg_info_sec; + if (ds == NULL) + return (DW_DLE_NO_ENTRY); + } else { + if (dbg->dbg_types_loaded) + return (ret); + offset = dbg->dbg_types_off; + ds = dbg->dbg_types_sec; + if (ds == NULL) + return (DW_DLE_NO_ENTRY); + } + while (offset < ds->ds_size) { if ((cu = calloc(1, sizeof(struct _Dwarf_CU))) == NULL) { DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY); @@ -106,6 +171,7 @@ _dwarf_info_load(Dwarf_Debug dbg, int load_all, Dwarf_Error *error) } cu->cu_dbg = dbg; + cu->cu_is_info = is_info; cu->cu_offset = offset; length = dbg->read(ds->ds_data, &offset, 4); @@ -129,7 +195,10 @@ _dwarf_info_load(Dwarf_Debug dbg, int load_all, Dwarf_Error *error) /* Compute the offset to the next compilation unit: */ next_offset = offset + length; - dbg->dbg_info_off = next_offset; + if (is_info) + dbg->dbg_info_off = next_offset; + else + dbg->dbg_types_off = next_offset; /* Initialise the compilation unit. */ cu->cu_length = length; @@ -141,8 +210,20 @@ _dwarf_info_load(Dwarf_Debug dbg, int load_all, Dwarf_Error *error) cu->cu_pointer_size = dbg->read(ds->ds_data, &offset, 1); cu->cu_next_offset = next_offset; + /* .debug_types extra fields. */ + if (!is_info) { + memcpy(cu->cu_type_sig.signature, + (char *) ds->ds_data + offset, 8); + offset += 8; + cu->cu_type_offset = dbg->read(ds->ds_data, &offset, + dwarf_size); + } + /* Add the compilation unit to the list. */ - STAILQ_INSERT_TAIL(&dbg->dbg_cu, cu, cu_next); + if (is_info) + STAILQ_INSERT_TAIL(&dbg->dbg_cu, cu, cu_next); + else + STAILQ_INSERT_TAIL(&dbg->dbg_tu, cu, cu_next); if (cu->cu_version < 2 || cu->cu_version > 4) { DWARF_SET_ERROR(dbg, error, DW_DLE_VERSION_STAMP_ERROR); @@ -158,8 +239,13 @@ _dwarf_info_load(Dwarf_Debug dbg, int load_all, Dwarf_Error *error) break; } - if ((Dwarf_Unsigned) dbg->dbg_info_off >= ds->ds_size) - dbg->dbg_info_loaded = 1; + if (is_info) { + if ((Dwarf_Unsigned) dbg->dbg_info_off >= ds->ds_size) + dbg->dbg_info_loaded = 1; + } else { + if ((Dwarf_Unsigned) dbg->dbg_types_off >= ds->ds_size) + dbg->dbg_types_loaded = 1; + } return (ret); } @@ -180,6 +266,22 @@ _dwarf_info_cleanup(Dwarf_Debug dbg) } free(cu); } + + _dwarf_type_unit_cleanup(dbg); +} + +void +_dwarf_type_unit_cleanup(Dwarf_Debug dbg) +{ + Dwarf_CU cu, tcu; + + assert(dbg != NULL && dbg->dbg_mode == DW_DLC_READ); + + STAILQ_FOREACH_SAFE(cu, &dbg->dbg_tu, cu_next, tcu) { + STAILQ_REMOVE(&dbg->dbg_tu, cu, _Dwarf_CU, cu_next); + _dwarf_abbrev_cleanup(cu); + free(cu); + } } int diff --git a/contrib/elftoolchain/libdwarf/libdwarf_init.c b/contrib/elftoolchain/libdwarf/libdwarf_init.c index 71d596699d58..b85c87c59af0 100644 --- a/contrib/elftoolchain/libdwarf/libdwarf_init.c +++ b/contrib/elftoolchain/libdwarf/libdwarf_init.c @@ -26,7 +26,7 @@ #include "_libdwarf.h" -ELFTC_VCSID("$Id: libdwarf_init.c 2948 2013-05-30 21:25:52Z kaiwang27 $"); +ELFTC_VCSID("$Id: libdwarf_init.c 3136 2014-12-24 16:04:38Z kaiwang27 $"); static int _dwarf_consumer_init(Dwarf_Debug dbg, Dwarf_Error *error) @@ -69,7 +69,8 @@ _dwarf_consumer_init(Dwarf_Debug dbg, Dwarf_Error *error) dbg->dbg_seccnt = cnt; - if ((dbg->dbg_section = calloc(cnt, sizeof(Dwarf_Section))) == NULL) { + if ((dbg->dbg_section = calloc(cnt + 1, sizeof(Dwarf_Section))) == + NULL) { DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY); return (DW_DLE_MEMORY); } @@ -90,13 +91,12 @@ _dwarf_consumer_init(Dwarf_Debug dbg, Dwarf_Error *error) return (ret); } } + dbg->dbg_section[cnt].ds_name = NULL; - if (_dwarf_find_section(dbg, ".debug_abbrev") == NULL || - ((dbg->dbg_info_sec = _dwarf_find_section(dbg, ".debug_info")) == - NULL)) { - DWARF_SET_ERROR(dbg, error, DW_DLE_DEBUG_INFO_NULL); - return (DW_DLE_DEBUG_INFO_NULL); - } + dbg->dbg_info_sec = _dwarf_find_section(dbg, ".debug_info"); + + /* Try to find the optional DWARF4 .debug_types section. */ + dbg->dbg_types_sec = _dwarf_find_next_types_section(dbg, NULL); /* Initialise call frame API related parameters. */ _dwarf_frame_params_init(dbg); @@ -210,10 +210,10 @@ _dwarf_init(Dwarf_Debug dbg, Dwarf_Unsigned pro_flags, Dwarf_Handler errhand, dbg->dbg_errarg = errarg; STAILQ_INIT(&dbg->dbg_cu); + STAILQ_INIT(&dbg->dbg_tu); STAILQ_INIT(&dbg->dbg_rllist); STAILQ_INIT(&dbg->dbg_aslist); STAILQ_INIT(&dbg->dbg_mslist); - TAILQ_INIT(&dbg->dbg_loclist); if (dbg->dbg_mode == DW_DLC_READ || dbg->dbg_mode == DW_DLC_RDWR) { ret = _dwarf_consumer_init(dbg, error); @@ -270,7 +270,6 @@ _dwarf_consumer_deinit(Dwarf_Debug dbg) assert(dbg != NULL && dbg->dbg_mode == DW_DLC_READ); _dwarf_info_cleanup(dbg); - _dwarf_loclist_cleanup(dbg); _dwarf_ranges_cleanup(dbg); _dwarf_frame_cleanup(dbg); _dwarf_arange_cleanup(dbg); diff --git a/contrib/elftoolchain/libdwarf/libdwarf_lineno.c b/contrib/elftoolchain/libdwarf/libdwarf_lineno.c index 8bb3c85f3dee..d0ff5f8e80da 100644 --- a/contrib/elftoolchain/libdwarf/libdwarf_lineno.c +++ b/contrib/elftoolchain/libdwarf/libdwarf_lineno.c @@ -26,7 +26,7 @@ #include "_libdwarf.h" -ELFTC_VCSID("$Id: libdwarf_lineno.c 2972 2013-12-23 06:46:04Z kaiwang27 $"); +ELFTC_VCSID("$Id: libdwarf_lineno.c 3164 2015-02-19 01:20:12Z kaiwang27 $"); static int _dwarf_lineno_add_file(Dwarf_LineInfo li, uint8_t **p, const char *compdir, @@ -87,9 +87,8 @@ _dwarf_lineno_run_program(Dwarf_CU cu, Dwarf_LineInfo li, uint8_t *p, { Dwarf_Debug dbg; Dwarf_Line ln, tln; - uint64_t address, file, line, column, isa, opsize; + uint64_t address, file, line, column, opsize; int is_stmt, basic_block, end_sequence; - int prologue_end, epilogue_begin; int ret; #define RESET_REGISTERS \ @@ -101,8 +100,6 @@ _dwarf_lineno_run_program(Dwarf_CU cu, Dwarf_LineInfo li, uint8_t *p, is_stmt = li->li_defstmt; \ basic_block = 0; \ end_sequence = 0; \ - prologue_end = 0; \ - epilogue_begin = 0; \ } while(0) #define APPEND_ROW \ @@ -181,8 +178,6 @@ _dwarf_lineno_run_program(Dwarf_CU cu, Dwarf_LineInfo li, uint8_t *p, case DW_LNS_copy: APPEND_ROW; basic_block = 0; - prologue_end = 0; - epilogue_begin = 0; break; case DW_LNS_advance_pc: address += _dwarf_decode_uleb128(&p) * @@ -210,13 +205,11 @@ _dwarf_lineno_run_program(Dwarf_CU cu, Dwarf_LineInfo li, uint8_t *p, address += dbg->decode(&p, 2); break; case DW_LNS_set_prologue_end: - prologue_end = 1; break; case DW_LNS_set_epilogue_begin: - epilogue_begin = 1; break; case DW_LNS_set_isa: - isa = _dwarf_decode_uleb128(&p); + (void) _dwarf_decode_uleb128(&p); break; default: /* Unrecognized extened opcodes. What to do? */ @@ -233,8 +226,6 @@ _dwarf_lineno_run_program(Dwarf_CU cu, Dwarf_LineInfo li, uint8_t *p, address += ADDRESS(*p); APPEND_ROW; basic_block = 0; - prologue_end = 0; - epilogue_begin = 0; p++; } } @@ -324,6 +315,8 @@ _dwarf_lineno_init(Dwarf_Die die, uint64_t offset, Dwarf_Error *error) li->li_hdrlen = dbg->read(ds->ds_data, &offset, dwarf_size); hdroff = offset; li->li_minlen = dbg->read(ds->ds_data, &offset, 1); + if (li->li_version == 4) + li->li_maxop = dbg->read(ds->ds_data, &offset, 1); li->li_defstmt = dbg->read(ds->ds_data, &offset, 1); li->li_lbase = dbg->read(ds->ds_data, &offset, 1); li->li_lrange = dbg->read(ds->ds_data, &offset, 1); @@ -482,7 +475,7 @@ _dwarf_lineno_gen_program(Dwarf_P_Debug dbg, Dwarf_P_Section ds, Dwarf_Unsigned address, file, line, spc; Dwarf_Unsigned addr0, maddr; Dwarf_Signed line0, column; - int is_stmt, basic_block, end_sequence; + int is_stmt, basic_block; int need_copy; int ret; @@ -494,7 +487,6 @@ _dwarf_lineno_gen_program(Dwarf_P_Debug dbg, Dwarf_P_Section ds, column = 0; \ is_stmt = li->li_defstmt; \ basic_block = 0; \ - end_sequence = 0; \ } while(0) li = dbg->dbgp_lineinfo; diff --git a/contrib/elftoolchain/libdwarf/libdwarf_loc.c b/contrib/elftoolchain/libdwarf/libdwarf_loc.c index ea366fb0768e..c2d3f5cc6662 100644 --- a/contrib/elftoolchain/libdwarf/libdwarf_loc.c +++ b/contrib/elftoolchain/libdwarf/libdwarf_loc.c @@ -1,5 +1,6 @@ /*- * Copyright (c) 2007 John Birrell (jb@freebsd.org) + * Copyright (c) 2014 Kai Wang * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -26,7 +27,7 @@ #include "_libdwarf.h" -ELFTC_VCSID("$Id: libdwarf_loc.c 2070 2011-10-27 03:05:32Z jkoshy $"); +ELFTC_VCSID("$Id: libdwarf_loc.c 3070 2014-06-23 03:08:33Z kaiwang27 $"); /* * Given an array of bytes of length 'len' representing a @@ -38,12 +39,12 @@ ELFTC_VCSID("$Id: libdwarf_loc.c 2070 2011-10-27 03:05:32Z jkoshy $"); */ static int _dwarf_loc_fill_loc(Dwarf_Debug dbg, Dwarf_Locdesc *lbuf, uint8_t pointer_size, - uint8_t *p, int len) + uint8_t offset_size, uint8_t version, uint8_t *p, int len) { int count; uint64_t operand1; uint64_t operand2; - uint8_t *ps, *pe; + uint8_t *ps, *pe, s; count = 0; ps = p; @@ -165,37 +166,49 @@ _dwarf_loc_fill_loc(Dwarf_Debug dbg, Dwarf_Locdesc *lbuf, uint8_t pointer_size, case DW_OP_ne: case DW_OP_nop: + case DW_OP_push_object_address: case DW_OP_form_tls_address: case DW_OP_call_frame_cfa: case DW_OP_stack_value: case DW_OP_GNU_push_tls_address: + case DW_OP_GNU_uninit: break; /* Operations with 1-byte operands. */ case DW_OP_const1u: - case DW_OP_const1s: case DW_OP_pick: case DW_OP_deref_size: case DW_OP_xderef_size: operand1 = *p++; break; + case DW_OP_const1s: + operand1 = (int8_t) *p++; + break; + /* Operations with 2-byte operands. */ case DW_OP_call2: case DW_OP_const2u: - case DW_OP_const2s: case DW_OP_bra: case DW_OP_skip: operand1 = dbg->decode(&p, 2); break; + case DW_OP_const2s: + operand1 = (int16_t) dbg->decode(&p, 2); + break; + /* Operations with 4-byte operands. */ case DW_OP_call4: case DW_OP_const4u: - case DW_OP_const4s: + case DW_OP_GNU_parameter_ref: operand1 = dbg->decode(&p, 4); break; + case DW_OP_const4s: + operand1 = (int32_t) dbg->decode(&p, 4); + break; + /* Operations with 8-byte operands. */ case DW_OP_const8u: case DW_OP_const8s: @@ -207,6 +220,9 @@ _dwarf_loc_fill_loc(Dwarf_Debug dbg, Dwarf_Locdesc *lbuf, uint8_t pointer_size, case DW_OP_plus_uconst: case DW_OP_regx: case DW_OP_piece: + case DW_OP_GNU_deref_type: + case DW_OP_GNU_convert: + case DW_OP_GNU_reinterpret: operand1 = _dwarf_decode_uleb128(&p); break; @@ -252,6 +268,7 @@ _dwarf_loc_fill_loc(Dwarf_Debug dbg, Dwarf_Locdesc *lbuf, uint8_t pointer_size, * Oeration with two unsigned LEB128 operands. */ case DW_OP_bit_piece: + case DW_OP_GNU_regval_type: operand1 = _dwarf_decode_uleb128(&p); operand2 = _dwarf_decode_uleb128(&p); break; @@ -267,10 +284,14 @@ _dwarf_loc_fill_loc(Dwarf_Debug dbg, Dwarf_Locdesc *lbuf, uint8_t pointer_size, /* * Operation with an unsigned LEB128 operand - * followed by a block. Store a pointer to the - * block in the operand2. + * representing the size of a block, followed + * by the block content. + * + * Store the size of the block in the operand1 + * and a pointer to the block in the operand2. */ case DW_OP_implicit_value: + case DW_OP_GNU_entry_value: operand1 = _dwarf_decode_uleb128(&p); operand2 = (Dwarf_Unsigned) (uintptr_t) p; p += operand1; @@ -278,25 +299,59 @@ _dwarf_loc_fill_loc(Dwarf_Debug dbg, Dwarf_Locdesc *lbuf, uint8_t pointer_size, /* Target address size operand. */ case DW_OP_addr: + case DW_OP_GNU_addr_index: + case DW_OP_GNU_const_index: operand1 = dbg->decode(&p, pointer_size); break; + /* Offset size operand. */ + case DW_OP_call_ref: + operand1 = dbg->decode(&p, offset_size); + break; + /* - * XXX Opcode DW_OP_call_ref has an operand with size - * "dwarf_size". Here we use dbg->dbg_offset_size - * as "dwarf_size" to be compatible with SGI libdwarf. - * However note that dbg->dbg_offset_size is just - * a "guess" value so the parsing result of - * DW_OP_call_ref might not be correct at all. XXX + * The first byte is address byte length, followed by + * the address value. If the length is 0, the address + * size is the same as target pointer size. */ - case DW_OP_call_ref: - operand1 = dbg->decode(&p, dbg->dbg_offset_size); + case DW_OP_GNU_encoded_addr: + s = *p++; + if (s == 0) + s = pointer_size; + operand1 = dbg->decode(&p, s); + break; + + /* + * Operand1: DIE offset (size depending on DWARF version) + * DWARF2: pointer size + * DWARF{3,4}: offset size + * + * Operand2: SLEB128 + */ + case DW_OP_GNU_implicit_pointer: + if (version == 2) + operand1 = dbg->decode(&p, pointer_size); + else + operand1 = dbg->decode(&p, offset_size); + operand2 = _dwarf_decode_sleb128(&p); + break; + + /* + * Operand1: DIE offset (ULEB128) + * Operand2: pointer to a block. The block's first byte + * is its size. + */ + case DW_OP_GNU_const_type: + operand1 = _dwarf_decode_uleb128(&p); + operand2 = (Dwarf_Unsigned) (uintptr_t) p; + s = *p++; + p += s; break; /* All other operations cause an error. */ default: count = -1; - break; + goto done; } if (lbuf != NULL) { @@ -307,6 +362,7 @@ _dwarf_loc_fill_loc(Dwarf_Debug dbg, Dwarf_Locdesc *lbuf, uint8_t pointer_size, count++; } +done: return (count); } @@ -561,7 +617,8 @@ _dwarf_loc_expr_add_atom(Dwarf_Debug dbg, uint8_t *out, uint8_t *end, int _dwarf_loc_fill_locdesc(Dwarf_Debug dbg, Dwarf_Locdesc *llbuf, uint8_t *in, - uint64_t in_len, uint8_t pointer_size, Dwarf_Error *error) + uint64_t in_len, uint8_t pointer_size, uint8_t offset_size, + uint8_t version, Dwarf_Error *error) { int num; @@ -570,8 +627,8 @@ _dwarf_loc_fill_locdesc(Dwarf_Debug dbg, Dwarf_Locdesc *llbuf, uint8_t *in, assert(in_len > 0); /* Compute the number of locations. */ - if ((num = _dwarf_loc_fill_loc(dbg, NULL, pointer_size, in, in_len)) < - 0) { + if ((num = _dwarf_loc_fill_loc(dbg, NULL, pointer_size, offset_size, + version, in, in_len)) < 0) { DWARF_SET_ERROR(dbg, error, DW_DLE_LOC_EXPR_BAD); return (DW_DLE_LOC_EXPR_BAD); } @@ -585,14 +642,16 @@ _dwarf_loc_fill_locdesc(Dwarf_Debug dbg, Dwarf_Locdesc *llbuf, uint8_t *in, return (DW_DLE_MEMORY); } - (void) _dwarf_loc_fill_loc(dbg, llbuf, pointer_size, in, in_len); + (void) _dwarf_loc_fill_loc(dbg, llbuf, pointer_size, offset_size, + version, in, in_len); return (DW_DLE_NONE); } int _dwarf_loc_fill_locexpr(Dwarf_Debug dbg, Dwarf_Locdesc **ret_llbuf, uint8_t *in, - uint64_t in_len, uint8_t pointer_size, Dwarf_Error *error) + uint64_t in_len, uint8_t pointer_size, uint8_t offset_size, + uint8_t version, Dwarf_Error *error) { Dwarf_Locdesc *llbuf; int ret; @@ -606,7 +665,7 @@ _dwarf_loc_fill_locexpr(Dwarf_Debug dbg, Dwarf_Locdesc **ret_llbuf, uint8_t *in, llbuf->ld_s = NULL; ret = _dwarf_loc_fill_locdesc(dbg, llbuf, in, in_len, pointer_size, - error); + offset_size, version, error); if (ret != DW_DLE_NONE) { free(llbuf); return (ret); @@ -635,7 +694,8 @@ _dwarf_loc_add(Dwarf_Die die, Dwarf_Attribute at, Dwarf_Error *error) assert(dbg != NULL); ret = _dwarf_loc_fill_locexpr(dbg, &at->at_ld, at->u[1].u8p, - at->u[0].u64, cu->cu_pointer_size, error); + at->u[0].u64, cu->cu_pointer_size, cu->cu_length_size == 4 ? 4 : 8, + cu->cu_version, error); return (ret); } diff --git a/contrib/elftoolchain/libdwarf/libdwarf_loclist.c b/contrib/elftoolchain/libdwarf/libdwarf_loclist.c index 8030e9a67852..bb3e39f6c899 100644 --- a/contrib/elftoolchain/libdwarf/libdwarf_loclist.c +++ b/contrib/elftoolchain/libdwarf/libdwarf_loclist.c @@ -26,11 +26,11 @@ #include "_libdwarf.h" -ELFTC_VCSID("$Id: libdwarf_loclist.c 2972 2013-12-23 06:46:04Z kaiwang27 $"); +ELFTC_VCSID("$Id: libdwarf_loclist.c 3061 2014-06-02 00:42:41Z kaiwang27 $"); static int _dwarf_loclist_add_locdesc(Dwarf_Debug dbg, Dwarf_CU cu, Dwarf_Section *ds, - uint64_t *off, Dwarf_Locdesc **ld, uint64_t *ldlen, + Dwarf_Unsigned *off, Dwarf_Locdesc **ld, Dwarf_Signed *ldlen, Dwarf_Unsigned *total_len, Dwarf_Error *error) { uint64_t start, end; @@ -75,6 +75,7 @@ _dwarf_loclist_add_locdesc(Dwarf_Debug dbg, Dwarf_CU cu, Dwarf_Section *ds, if (ld != NULL) { ret = _dwarf_loc_fill_locdesc(dbg, ld[i], ds->ds_data + *off, len, cu->cu_pointer_size, + cu->cu_length_size == 4 ? 4 : 8, cu->cu_version, error); if (ret != DW_DLE_NONE) return (ret); @@ -91,37 +92,15 @@ _dwarf_loclist_add_locdesc(Dwarf_Debug dbg, Dwarf_CU cu, Dwarf_Section *ds, int _dwarf_loclist_find(Dwarf_Debug dbg, Dwarf_CU cu, uint64_t lloff, - Dwarf_Loclist *ret_ll, Dwarf_Error *error) -{ - Dwarf_Loclist ll; - int ret; - - assert(ret_ll != NULL); - ret = DW_DLE_NONE; - - TAILQ_FOREACH(ll, &dbg->dbg_loclist, ll_next) - if (ll->ll_offset == lloff) - break; - - if (ll == NULL) - ret = _dwarf_loclist_add(dbg, cu, lloff, ret_ll, error); - else - *ret_ll = ll; - - return (ret); -} - -int -_dwarf_loclist_add(Dwarf_Debug dbg, Dwarf_CU cu, uint64_t lloff, - Dwarf_Loclist *ret_ll, Dwarf_Error *error) + Dwarf_Locdesc ***ret_llbuf, Dwarf_Signed *listlen, + Dwarf_Unsigned *entry_len, Dwarf_Error *error) { + Dwarf_Locdesc **llbuf; Dwarf_Section *ds; - Dwarf_Loclist ll, tll; - uint64_t ldlen; + Dwarf_Signed ldlen; + Dwarf_Unsigned off; int i, ret; - ret = DW_DLE_NONE; - if ((ds = _dwarf_find_section(dbg, ".debug_loc")) == NULL) { DWARF_SET_ERROR(dbg, error, DW_DLE_NO_ENTRY); return (DW_DLE_NO_ENTRY); @@ -132,98 +111,55 @@ _dwarf_loclist_add(Dwarf_Debug dbg, Dwarf_CU cu, uint64_t lloff, return (DW_DLE_NO_ENTRY); } - if ((ll = malloc(sizeof(struct _Dwarf_Loclist))) == NULL) { - DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY); - return (DW_DLE_MEMORY); - } - - ll->ll_offset = lloff; - /* Get the number of locdesc the first round. */ - ret = _dwarf_loclist_add_locdesc(dbg, cu, ds, &lloff, NULL, &ldlen, + off = lloff; + ret = _dwarf_loclist_add_locdesc(dbg, cu, ds, &off, NULL, &ldlen, NULL, error); if (ret != DW_DLE_NONE) - goto fail_cleanup; + return (ret); + + if (ldlen == 0) + return (DW_DLE_NO_ENTRY); /* * Dwarf_Locdesc list memory is allocated in this way (one more level * of indirect) to make the loclist API be compatible with SGI libdwarf. */ - ll->ll_ldlen = ldlen; - if (ldlen != 0) { - if ((ll->ll_ldlist = calloc(ldlen, sizeof(Dwarf_Locdesc *))) == - NULL) { + if ((llbuf = calloc(ldlen, sizeof(Dwarf_Locdesc *))) == NULL) { + DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY); + return (DW_DLE_MEMORY); + } + for (i = 0; i < ldlen; i++) { + if ((llbuf[i] = calloc(1, sizeof(Dwarf_Locdesc))) == NULL) { DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY); ret = DW_DLE_MEMORY; goto fail_cleanup; } - for (i = 0; (uint64_t) i < ldlen; i++) { - if ((ll->ll_ldlist[i] = - calloc(1, sizeof(Dwarf_Locdesc))) == NULL) { - DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY); - ret = DW_DLE_MEMORY; - goto fail_cleanup; - } - } - } else - ll->ll_ldlist = NULL; + } - lloff = ll->ll_offset; + off = lloff; /* Fill in locdesc. */ - ret = _dwarf_loclist_add_locdesc(dbg, cu, ds, &lloff, ll->ll_ldlist, - NULL, &ll->ll_length, error); + ret = _dwarf_loclist_add_locdesc(dbg, cu, ds, &off, llbuf, NULL, + entry_len, error); if (ret != DW_DLE_NONE) goto fail_cleanup; - /* Insert to the queue. Sort by offset. */ - TAILQ_FOREACH(tll, &dbg->dbg_loclist, ll_next) - if (tll->ll_offset > ll->ll_offset) { - TAILQ_INSERT_BEFORE(tll, ll, ll_next); - break; - } - - if (tll == NULL) - TAILQ_INSERT_TAIL(&dbg->dbg_loclist, ll, ll_next); + *ret_llbuf = llbuf; + *listlen = ldlen; - *ret_ll = ll; return (DW_DLE_NONE); fail_cleanup: - _dwarf_loclist_free(ll); - - return (ret); -} - -void -_dwarf_loclist_free(Dwarf_Loclist ll) -{ - int i; - - if (ll == NULL) - return; - - if (ll->ll_ldlist != NULL) { - for (i = 0; i < ll->ll_ldlen; i++) { - if (ll->ll_ldlist[i]->ld_s) - free(ll->ll_ldlist[i]->ld_s); - free(ll->ll_ldlist[i]); + if (llbuf != NULL) { + for (i = 0; i < ldlen; i++) { + if (llbuf[i]->ld_s) + free(llbuf[i]->ld_s); + free(llbuf[i]); } - free(ll->ll_ldlist); + free(llbuf); } - free(ll); -} -void -_dwarf_loclist_cleanup(Dwarf_Debug dbg) -{ - Dwarf_Loclist ll, tll; - - assert(dbg != NULL && dbg->dbg_mode == DW_DLC_READ); - - TAILQ_FOREACH_SAFE(ll, &dbg->dbg_loclist, ll_next, tll) { - TAILQ_REMOVE(&dbg->dbg_loclist, ll, ll_next); - _dwarf_loclist_free(ll); - } + return (ret); } diff --git a/contrib/elftoolchain/libdwarf/libdwarf_nametbl.c b/contrib/elftoolchain/libdwarf/libdwarf_nametbl.c index 158aca34ad43..661b56f1c9c8 100644 --- a/contrib/elftoolchain/libdwarf/libdwarf_nametbl.c +++ b/contrib/elftoolchain/libdwarf/libdwarf_nametbl.c @@ -26,7 +26,7 @@ #include "_libdwarf.h" -ELFTC_VCSID("$Id: libdwarf_nametbl.c 2070 2011-10-27 03:05:32Z jkoshy $"); +ELFTC_VCSID("$Id: libdwarf_nametbl.c 3029 2014-04-21 23:26:02Z kaiwang27 $"); void _dwarf_nametbl_cleanup(Dwarf_NameSec *nsp) @@ -103,7 +103,7 @@ _dwarf_nametbl_init(Dwarf_Debug dbg, Dwarf_NameSec *namesec, Dwarf_Section *ds, nt->nt_cu_length = dbg->read(ds->ds_data, &offset, dwarf_size); if (!dbg->dbg_info_loaded) { - ret = _dwarf_info_load(dbg, 1, error); + ret = _dwarf_info_load(dbg, 1, 1, error); if (ret != DW_DLE_NONE) goto fail_cleanup; } diff --git a/contrib/elftoolchain/libdwarf/libdwarf_reloc.c b/contrib/elftoolchain/libdwarf/libdwarf_reloc.c index ea916772aadb..96bb785845fe 100644 --- a/contrib/elftoolchain/libdwarf/libdwarf_reloc.c +++ b/contrib/elftoolchain/libdwarf/libdwarf_reloc.c @@ -26,7 +26,7 @@ #include "_libdwarf.h" -ELFTC_VCSID("$Id: libdwarf_reloc.c 2948 2013-05-30 21:25:52Z kaiwang27 $"); +ELFTC_VCSID("$Id: libdwarf_reloc.c 3149 2015-02-15 19:00:06Z emaste $"); Dwarf_Unsigned _dwarf_get_reloc_type(Dwarf_P_Debug dbg, int is64) @@ -35,6 +35,8 @@ _dwarf_get_reloc_type(Dwarf_P_Debug dbg, int is64) assert(dbg != NULL); switch (dbg->dbgp_isa) { + case DW_ISA_AARCH64: + return (is64 ? R_AARCH64_ABS64 : R_AARCH64_ABS32); case DW_ISA_X86: return (R_386_32); case DW_ISA_X86_64: @@ -62,6 +64,12 @@ _dwarf_get_reloc_size(Dwarf_Debug dbg, Dwarf_Unsigned rel_type) switch (dbg->dbg_machine) { case EM_NONE: break; + case EM_AARCH64: + if (rel_type == R_AARCH64_ABS32) + return (4); + else if (rel_type == R_AARCH64_ABS64) + return (8); + break; case EM_ARM: if (rel_type == R_ARM_ABS32) return (4); diff --git a/contrib/elftoolchain/libdwarf/libdwarf_sections.c b/contrib/elftoolchain/libdwarf/libdwarf_sections.c index 3ac30b251e02..24d5db839e49 100644 --- a/contrib/elftoolchain/libdwarf/libdwarf_sections.c +++ b/contrib/elftoolchain/libdwarf/libdwarf_sections.c @@ -26,7 +26,7 @@ #include "_libdwarf.h" -ELFTC_VCSID("$Id: libdwarf_sections.c 2379 2012-01-05 02:08:20Z jkoshy $"); +ELFTC_VCSID("$Id: libdwarf_sections.c 3041 2014-05-18 15:11:03Z kaiwang27 $"); #define _SECTION_INIT_SIZE 128 @@ -212,7 +212,7 @@ _dwarf_find_section(Dwarf_Debug dbg, const char *name) Dwarf_Section *ds; Dwarf_Half i; - assert(name != NULL); + assert(dbg != NULL && name != NULL); for (i = 0; i < dbg->dbg_seccnt; i++) { ds = &dbg->dbg_section[i]; @@ -223,6 +223,27 @@ _dwarf_find_section(Dwarf_Debug dbg, const char *name) return (NULL); } +Dwarf_Section * +_dwarf_find_next_types_section(Dwarf_Debug dbg, Dwarf_Section *ds) +{ + + assert(dbg != NULL); + + if (ds == NULL) + return (_dwarf_find_section(dbg, ".debug_types")); + + assert(ds->ds_name != NULL); + + do { + ds++; + if (ds->ds_name != NULL && + !strcmp(ds->ds_name, ".debug_types")) + return (ds); + } while (ds->ds_name != NULL); + + return (NULL); +} + Dwarf_P_Section _dwarf_pro_find_section(Dwarf_P_Debug dbg, const char *name) { diff --git a/contrib/elftoolchain/libelf/_libelf.h b/contrib/elftoolchain/libelf/_libelf.h index fba2f97fd670..be45335907f3 100644 --- a/contrib/elftoolchain/libelf/_libelf.h +++ b/contrib/elftoolchain/libelf/_libelf.h @@ -23,7 +23,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: _libelf.h 2365 2011-12-29 04:36:44Z jkoshy $ + * $Id: _libelf.h 3174 2015-03-27 17:13:41Z emaste $ */ #ifndef __LIBELF_H_ @@ -48,7 +48,7 @@ struct _libelf_globals { int libelf_error; int libelf_fillchar; unsigned int libelf_version; - char libelf_msg[LIBELF_MSG_SIZE]; + unsigned char libelf_msg[LIBELF_MSG_SIZE]; }; extern struct _libelf_globals _libelf; @@ -71,14 +71,14 @@ extern struct _libelf_globals _libelf; * Flags for library internal use. These use the upper 16 bits of the * `e_flags' field. */ -#define LIBELF_F_API_MASK 0x00FFFF /* Flags defined by the API. */ -#define LIBELF_F_AR_HEADER 0x010000 /* translated header available */ -#define LIBELF_F_AR_VARIANT_SVR4 0x020000 /* BSD style ar(1) archive */ -#define LIBELF_F_DATA_MALLOCED 0x040000 /* whether data was malloc'ed */ -#define LIBELF_F_RAWFILE_MALLOC 0x080000 /* whether e_rawfile was malloc'ed */ -#define LIBELF_F_RAWFILE_MMAP 0x100000 /* whether e_rawfile was mmap'ed */ -#define LIBELF_F_SHDRS_LOADED 0x200000 /* whether all shdrs were read in */ -#define LIBELF_F_SPECIAL_FILE 0x400000 /* non-regular file */ +#define LIBELF_F_API_MASK 0x00FFFFU /* Flags defined by the API. */ +#define LIBELF_F_AR_HEADER 0x010000U /* translated header available */ +#define LIBELF_F_AR_VARIANT_SVR4 0x020000U /* BSD style ar(1) archive */ +#define LIBELF_F_DATA_MALLOCED 0x040000U /* whether data was malloc'ed */ +#define LIBELF_F_RAWFILE_MALLOC 0x080000U /* whether e_rawfile was malloc'ed */ +#define LIBELF_F_RAWFILE_MMAP 0x100000U /* whether e_rawfile was mmap'ed */ +#define LIBELF_F_SHDRS_LOADED 0x200000U /* whether all shdrs were read in */ +#define LIBELF_F_SPECIAL_FILE 0x400000U /* non-regular file */ struct _Elf { int e_activations; /* activation count */ @@ -89,7 +89,7 @@ struct _Elf { unsigned int e_flags; /* ELF_F_* & LIBELF_F_* flags */ Elf_Kind e_kind; /* ELF_K_* */ Elf *e_parent; /* non-NULL for archive members */ - char *e_rawfile; /* uninterpreted bytes */ + unsigned char *e_rawfile; /* uninterpreted bytes */ size_t e_rawsize; /* size of uninterpreted bytes */ unsigned int e_version; /* file version */ @@ -99,16 +99,16 @@ struct _Elf { */ union { Elf_Arhdr *e_arhdr; /* translated header */ - char *e_rawhdr; /* untranslated header */ + unsigned char *e_rawhdr; /* untranslated header */ } e_hdr; union { struct { /* ar(1) archives */ off_t e_next; /* set by elf_rand()/elf_next() */ int e_nchildren; - char *e_rawstrtab; /* file name strings */ + unsigned char *e_rawstrtab; /* file name strings */ size_t e_rawstrtabsz; - char *e_rawsymtab; /* symbol table */ + unsigned char *e_rawsymtab; /* symbol table */ size_t e_rawsymtabsz; Elf_Arsym *e_symtab; size_t e_symtabsz; @@ -162,21 +162,31 @@ enum { ELF_TOMEMORY }; -#define LIBELF_COPY_U32(DST,SRC,NAME) do { \ - if ((SRC)->NAME > UINT_MAX) { \ - LIBELF_SET_ERROR(RANGE, 0); \ - return (0); \ - } \ - (DST)->NAME = (SRC)->NAME; \ + +/* + * The LIBELF_COPY macros are used to copy fields from a GElf_* + * structure to their 32-bit counterparts, while checking for out of + * range values. + * + * - LIBELF_COPY_U32 :: copy an unsigned 32 bit field. + * - LIBELF_COPY_S32 :: copy a signed 32 bit field. + */ + +#define LIBELF_COPY_U32(DST, SRC, NAME) do { \ + if ((SRC)->NAME > UINT32_MAX) { \ + LIBELF_SET_ERROR(RANGE, 0); \ + return (0); \ + } \ + (DST)->NAME = (SRC)->NAME & 0xFFFFFFFFU; \ } while (0) -#define LIBELF_COPY_S32(DST,SRC,NAME) do { \ - if ((SRC)->NAME > INT_MAX || \ - (SRC)->NAME < INT_MIN) { \ - LIBELF_SET_ERROR(RANGE, 0); \ - return (0); \ - } \ - (DST)->NAME = (SRC)->NAME; \ +#define LIBELF_COPY_S32(DST, SRC, NAME) do { \ + if ((SRC)->NAME > INT32_MAX || \ + (SRC)->NAME < INT32_MIN) { \ + LIBELF_SET_ERROR(RANGE, 0); \ + return (0); \ + } \ + (DST)->NAME = (int32_t) (SRC)->NAME; \ } while (0) @@ -184,29 +194,31 @@ enum { * Function Prototypes. */ -__BEGIN_DECLS +#ifdef __cplusplus +extern "C" { +#endif struct _Libelf_Data *_libelf_allocate_data(Elf_Scn *_s); Elf *_libelf_allocate_elf(void); Elf_Scn *_libelf_allocate_scn(Elf *_e, size_t _ndx); Elf_Arhdr *_libelf_ar_gethdr(Elf *_e); Elf *_libelf_ar_open(Elf *_e, int _reporterror); Elf *_libelf_ar_open_member(int _fd, Elf_Cmd _c, Elf *_ar); -int _libelf_ar_get_member(char *_s, size_t _sz, int _base, size_t *_ret); Elf_Arsym *_libelf_ar_process_bsd_symtab(Elf *_ar, size_t *_dst); Elf_Arsym *_libelf_ar_process_svr4_symtab(Elf *_ar, size_t *_dst); -unsigned long _libelf_checksum(Elf *_e, int _elfclass); +long _libelf_checksum(Elf *_e, int _elfclass); void *_libelf_ehdr(Elf *_e, int _elfclass, int _allocate); -int _libelf_falign(Elf_Type _t, int _elfclass); +unsigned int _libelf_falign(Elf_Type _t, int _elfclass); size_t _libelf_fsize(Elf_Type _t, int _elfclass, unsigned int _version, size_t count); int (*_libelf_get_translator(Elf_Type _t, int _direction, int _elfclass)) - (char *_dst, size_t dsz, char *_src, size_t _cnt, int _byteswap); + (unsigned char *_dst, size_t dsz, unsigned char *_src, + size_t _cnt, int _byteswap); void *_libelf_getphdr(Elf *_e, int _elfclass); void *_libelf_getshdr(Elf_Scn *_scn, int _elfclass); void _libelf_init_elf(Elf *_e, Elf_Kind _kind); int _libelf_load_section_headers(Elf *e, void *ehdr); -int _libelf_malign(Elf_Type _t, int _elfclass); -Elf *_libelf_memory(char *_image, size_t _sz, int _reporterror); +unsigned int _libelf_malign(Elf_Type _t, int _elfclass); +Elf *_libelf_memory(unsigned char *_image, size_t _sz, int _reporterror); size_t _libelf_msize(Elf_Type _t, int _elfclass, unsigned int _version); void *_libelf_newphdr(Elf *_e, int _elfclass, size_t _count); Elf *_libelf_open_object(int _fd, Elf_Cmd _c, int _reporterror); @@ -220,6 +232,8 @@ int _libelf_setshstrndx(Elf *_e, void *_eh, int _elfclass, Elf_Data *_libelf_xlate(Elf_Data *_d, const Elf_Data *_s, unsigned int _encoding, int _elfclass, int _direction); int _libelf_xlate_shtype(uint32_t _sht); -__END_DECLS +#ifdef __cplusplus +} +#endif #endif /* __LIBELF_H_ */ diff --git a/contrib/elftoolchain/libelf/_libelf_ar.h b/contrib/elftoolchain/libelf/_libelf_ar.h index d6b15a7501ef..45a7e16be863 100644 --- a/contrib/elftoolchain/libelf/_libelf_ar.h +++ b/contrib/elftoolchain/libelf/_libelf_ar.h @@ -23,7 +23,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: _libelf_ar.h 2032 2011-10-23 09:07:00Z jkoshy $ + * $Id: _libelf_ar.h 3013 2014-03-23 06:16:59Z jkoshy $ */ #ifndef __LIBELF_AR_H_ @@ -42,15 +42,16 @@ (sizeof(LIBELF_AR_BSD_EXTENDED_NAME_PREFIX) - 1) #define IS_EXTENDED_BSD_NAME(NAME) \ - (strncmp((NAME), LIBELF_AR_BSD_EXTENDED_NAME_PREFIX, \ + (strncmp((const char *) (NAME), \ + LIBELF_AR_BSD_EXTENDED_NAME_PREFIX, \ LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE) == 0) -char *_libelf_ar_get_string(const char *_buf, size_t _sz, int _rawname, - int _svr4names); +unsigned char *_libelf_ar_get_string(const char *_buf, size_t _sz, + unsigned int _rawname, int _svr4names); char *_libelf_ar_get_raw_name(const struct ar_hdr *_arh); char *_libelf_ar_get_translated_name(const struct ar_hdr *_arh, Elf *_ar); -int _libelf_ar_get_number(const char *_buf, size_t _sz, int _base, - size_t *_ret); +int _libelf_ar_get_number(const char *_buf, size_t _sz, + unsigned int _base, size_t *_ret); #endif /* __LIBELF_AR_H_ */ diff --git a/contrib/elftoolchain/libelf/_libelf_config.h b/contrib/elftoolchain/libelf/_libelf_config.h index b9442fdc81d0..1b8f35b2242d 100644 --- a/contrib/elftoolchain/libelf/_libelf_config.h +++ b/contrib/elftoolchain/libelf/_libelf_config.h @@ -23,10 +23,10 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: _libelf_config.h 2287 2011-12-04 06:45:47Z jkoshy $ + * $Id: _libelf_config.h 3168 2015-02-24 19:17:47Z emaste $ */ -#ifdef __DragonFly__ +#if defined(__APPLE__) || defined(__DragonFly__) #if defined(__amd64__) #define LIBELF_ARCH EM_X86_64 @@ -53,6 +53,12 @@ #define LIBELF_BYTEORDER ELFDATA2LSB #define LIBELF_CLASS ELFCLASS64 +#elif defined(__aarch64__) + +#define LIBELF_ARCH EM_AARCH64 +#define LIBELF_BYTEORDER ELFDATA2LSB +#define LIBELF_CLASS ELFCLASS64 + #elif defined(__arm__) #define LIBELF_ARCH EM_ARM diff --git a/contrib/elftoolchain/libelf/elf.3 b/contrib/elftoolchain/libelf/elf.3 index 462e72854177..20cb9445535d 100644 --- a/contrib/elftoolchain/libelf/elf.3 +++ b/contrib/elftoolchain/libelf/elf.3 @@ -21,9 +21,9 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: elf.3 2885 2013-01-11 02:11:28Z jkoshy $ +.\" $Id: elf.3 3195 2015-05-12 17:22:19Z emaste $ .\" -.Dd August 14, 2011 +.Dd July 28, 2014 .Os .Dt ELF 3 .Sh NAME @@ -367,6 +367,11 @@ section entries. .Xc .It Dv SHT_DYNSYM Ta Dv ELF_T_SYM Ta Symbols for dynamic linking. .It Dv SHT_FINI_ARRAY Ta Dv ELF_T_ADDR Ta Termination function pointers. +.It Dv SHT_GNU_HASH Ta Dv ELF_T_GNUHASH Ta GNU hash sections. +.It Dv SHT_GNU_LIBLIST Ta Dv ELF_T_WORD Ta List of libraries to be pre-linked. +.It Dv SHT_GNU_verdef Ta Dv ELF_T_VDEF Ta Symbol version definitions. +.It Dv SHT_GNU_verneed Ta Dv ELF_T_VNEED Ta Symbol versioning requirements. +.It Dv SHT_GNU_versym Ta Dv ELF_T_HALF Ta Version symbols. .It Dv SHT_GROUP Ta Dv ELF_T_WORD Ta Section group marker. .It Dv SHT_HASH Ta Dv ELF_T_HASH Ta Symbol hashes. .It Dv SHT_INIT_ARRAY Ta Dv ELF_T_ADDR Ta Initialization function pointers. @@ -383,13 +388,31 @@ See .It Dv SHT_STRTAB Ta Dv ELF_T_BYTE Ta String tables. .It Dv SHT_SYMTAB Ta Dv ELF_T_SYM Ta Symbol tables. .It Dv SHT_SYMTAB_SHNDX Ta Dv ELF_T_WORD Ta Used with extended section numbering. -.It Dv SHT_GNU_verdef Ta Dv ELF_T_VDEF Ta Symbol version definitions. -.It Dv SHT_GNU_verneed Ta Dv ELF_T_VNEED Ta Symbol versioning requirements. -.It Dv SHT_GNU_versym Ta Dv ELF_T_HALF Ta Version symbols. +.It Dv SHT_SUNW_dof Ta Dv ELF_T_BYTE Ta Xo +Used by +.Xr dtrace 1 . +.Xc .It Dv SHT_SUNW_move Ta Dv ELF_T_MOVE Ta ELF move records. .It Dv SHT_SUNW_syminfo Ta Dv ELF_T_SYMINFO Ta Additional symbol flags. +.It Dv SHT_SUNW_verdef Ta Dv ELF_T_VDEF Ta Xo +Same as +.Dv SHT_GNU_verdef . +.Xc +.It Dv SHT_SUNW_verneed Ta Dv ELF_T_VNEED Ta Xo +Same as +.Dv SHT_GNU_verneed . +.Xc +.It Dv SHT_SUNW_versym Ta Dv ELF_T_HALF Ta Xo +Same as +.Dv SHT_GNU_versym . +.Xc .El -.TE +.Pp +Section types in the range +.Ns [ Dv SHT_LOOS , +.Dv SHT_HIUSER ] +are otherwise considered to be of type +.Dv ELF_T_BYTE . .Ss Functional Grouping This section contains a brief overview of the available functionality in the ELF library. @@ -532,7 +555,7 @@ flag on an ELF descriptor using .Xr elf_flagelf 3 , following which the library will use the data offsets and alignments specified by the application when laying out the file. -Application control of file layout is described further in the +Application control of file layout is described further in the .Xr elf_update 3 manual page. .Pp @@ -585,5 +608,4 @@ The current implementation of the ELF(3) API appeared in .Fx 7.0 . .Sh AUTHORS The ELF library was written by -.An "Joseph Koshy" -.Aq jkoshy@FreeBSD.org . +.An Joseph Koshy Aq Mt jkoshy@FreeBSD.org . diff --git a/contrib/elftoolchain/libelf/elf_begin.3 b/contrib/elftoolchain/libelf/elf_begin.3 index 960456afbd89..e6c11b60e865 100644 --- a/contrib/elftoolchain/libelf/elf_begin.3 +++ b/contrib/elftoolchain/libelf/elf_begin.3 @@ -21,7 +21,7 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: elf_begin.3 2313 2011-12-11 06:19:24Z jkoshy $ +.\" $Id: elf_begin.3 3182 2015-04-10 16:08:10Z emaste $ .\" .Dd December 11, 2011 .Os @@ -272,7 +272,7 @@ was created. .It Bq Er ELF_E_ARGUMENT An .Xr ar 1 -archive was opened with with +archive was opened with .Ar cmd set to .Dv ELF_C_RDWR . diff --git a/contrib/elftoolchain/libelf/elf_cntl.3 b/contrib/elftoolchain/libelf/elf_cntl.3 index 32649d1f955e..257897590db2 100644 --- a/contrib/elftoolchain/libelf/elf_cntl.3 +++ b/contrib/elftoolchain/libelf/elf_cntl.3 @@ -21,7 +21,7 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: elf_cntl.3 289 2009-01-08 08:26:08Z jkoshy $ +.\" $Id: elf_cntl.3 3181 2015-04-10 13:22:51Z emaste $ .\" .Dd August 9, 2006 .Os diff --git a/contrib/elftoolchain/libelf/elf_data.c b/contrib/elftoolchain/libelf/elf_data.c index 06a3577dc5dd..ce80e1c94c40 100644 --- a/contrib/elftoolchain/libelf/elf_data.c +++ b/contrib/elftoolchain/libelf/elf_data.c @@ -27,11 +27,12 @@ #include <assert.h> #include <errno.h> #include <libelf.h> +#include <stdint.h> #include <stdlib.h> #include "_libelf.h" -ELFTC_VCSID("$Id: elf_data.c 2921 2013-03-04 16:19:22Z jkoshy $"); +ELFTC_VCSID("$Id: elf_data.c 3177 2015-03-30 18:19:41Z emaste $"); Elf_Data * elf_getdata(Elf_Scn *s, Elf_Data *ed) @@ -39,10 +40,11 @@ elf_getdata(Elf_Scn *s, Elf_Data *ed) Elf *e; unsigned int sh_type; int elfclass, elftype; - size_t fsz, msz, count; + size_t count, fsz, msz; struct _Libelf_Data *d; uint64_t sh_align, sh_offset, sh_size; - int (*xlate)(char *_d, size_t _dsz, char *_s, size_t _c, int _swap); + int (*xlate)(unsigned char *_d, size_t _dsz, unsigned char *_s, + size_t _c, int _swap); d = (struct _Libelf_Data *) ed; @@ -108,11 +110,23 @@ elf_getdata(Elf_Scn *s, Elf_Data *ed) return (NULL); } - count = sh_size / fsz; + if (sh_size / fsz > SIZE_MAX) { + LIBELF_SET_ERROR(RANGE, 0); + return (NULL); + } + + count = (size_t) (sh_size / fsz); msz = _libelf_msize(elftype, elfclass, e->e_version); + if (count > 0 && msz > SIZE_MAX / count) { + LIBELF_SET_ERROR(RANGE, 0); + return (NULL); + } + assert(msz > 0); + assert(count <= SIZE_MAX); + assert(msz * count <= SIZE_MAX); if ((d = _libelf_allocate_data(s)) == NULL) return (NULL); @@ -129,7 +143,7 @@ elf_getdata(Elf_Scn *s, Elf_Data *ed) return (&d->d_data); } - if ((d->d_data.d_buf = malloc(msz*count)) == NULL) { + if ((d->d_data.d_buf = malloc(msz * count)) == NULL) { (void) _libelf_release_data(d); LIBELF_SET_ERROR(RESOURCE, 0); return (NULL); @@ -138,7 +152,7 @@ elf_getdata(Elf_Scn *s, Elf_Data *ed) d->d_flags |= LIBELF_F_DATA_MALLOCED; xlate = _libelf_get_translator(elftype, ELF_TOMEMORY, elfclass); - if (!(*xlate)(d->d_data.d_buf, d->d_data.d_size, + if (!(*xlate)(d->d_data.d_buf, (size_t) d->d_data.d_size, e->e_rawfile + sh_offset, count, e->e_byteorder != LIBELF_PRIVATE(byteorder))) { _libelf_release_data(d); diff --git a/contrib/elftoolchain/libelf/elf_end.c b/contrib/elftoolchain/libelf/elf_end.c index 097664c12579..3f32ebbee8ec 100644 --- a/contrib/elftoolchain/libelf/elf_end.c +++ b/contrib/elftoolchain/libelf/elf_end.c @@ -24,8 +24,6 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> - #include <assert.h> #include <libelf.h> #include <stdlib.h> @@ -36,7 +34,7 @@ #include <sys/mman.h> #endif -ELFTC_VCSID("$Id: elf_end.c 2240 2011-11-28 06:36:48Z jkoshy $"); +ELFTC_VCSID("$Id: elf_end.c 3174 2015-03-27 17:13:41Z emaste $"); int elf_end(Elf *e) diff --git a/contrib/elftoolchain/libelf/elf_errmsg.c b/contrib/elftoolchain/libelf/elf_errmsg.c index 20ee55fa60b5..9e5b2e92ee1b 100644 --- a/contrib/elftoolchain/libelf/elf_errmsg.c +++ b/contrib/elftoolchain/libelf/elf_errmsg.c @@ -24,15 +24,13 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> - #include <libelf.h> #include <stdio.h> #include <string.h> #include "_libelf.h" -ELFTC_VCSID("$Id: elf_errmsg.c 2225 2011-11-26 18:55:54Z jkoshy $"); +ELFTC_VCSID("$Id: elf_errmsg.c 3174 2015-03-27 17:13:41Z emaste $"); /* * Retrieve a human readable translation for an error message. @@ -76,7 +74,7 @@ elf_errmsg(int error) if (error < ELF_E_NONE || error >= ELF_E_NUM) return _libelf_errors[ELF_E_NUM]; if (oserr) { - (void) snprintf(LIBELF_PRIVATE(msg), + (void) snprintf((char *) LIBELF_PRIVATE(msg), sizeof(LIBELF_PRIVATE(msg)), "%s: %s", _libelf_errors[error], strerror(oserr)); return (const char *)&LIBELF_PRIVATE(msg); diff --git a/contrib/elftoolchain/libelf/elf_errno.c b/contrib/elftoolchain/libelf/elf_errno.c index c78d4c7a7cea..10ae69acbe58 100644 --- a/contrib/elftoolchain/libelf/elf_errno.c +++ b/contrib/elftoolchain/libelf/elf_errno.c @@ -24,13 +24,11 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> - #include <libelf.h> #include "_libelf.h" -ELFTC_VCSID("$Id: elf_errno.c 2225 2011-11-26 18:55:54Z jkoshy $"); +ELFTC_VCSID("$Id: elf_errno.c 3174 2015-03-27 17:13:41Z emaste $"); int elf_errno(void) diff --git a/contrib/elftoolchain/libelf/elf_fill.c b/contrib/elftoolchain/libelf/elf_fill.c index 4c9facc78273..427d78d5ad66 100644 --- a/contrib/elftoolchain/libelf/elf_fill.c +++ b/contrib/elftoolchain/libelf/elf_fill.c @@ -24,13 +24,11 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> - #include <libelf.h> #include "_libelf.h" -ELFTC_VCSID("$Id: elf_fill.c 2225 2011-11-26 18:55:54Z jkoshy $"); +ELFTC_VCSID("$Id: elf_fill.c 3174 2015-03-27 17:13:41Z emaste $"); void elf_fill(int fill) diff --git a/contrib/elftoolchain/libelf/elf_flag.c b/contrib/elftoolchain/libelf/elf_flag.c index ab9d24a0ecea..73503214a9fe 100644 --- a/contrib/elftoolchain/libelf/elf_flag.c +++ b/contrib/elftoolchain/libelf/elf_flag.c @@ -24,13 +24,11 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> - #include <libelf.h> #include "_libelf.h" -ELFTC_VCSID("$Id: elf_flag.c 2272 2011-12-03 17:07:31Z jkoshy $"); +ELFTC_VCSID("$Id: elf_flag.c 3174 2015-03-27 17:13:41Z emaste $"); unsigned int elf_flagarhdr(Elf_Arhdr *a, Elf_Cmd c, unsigned int flags) @@ -111,7 +109,7 @@ elf_flagehdr(Elf *e, Elf_Cmd c, unsigned int flags) unsigned int elf_flagelf(Elf *e, Elf_Cmd c, unsigned int flags) { - int r; + unsigned int r; if (e == NULL) return (0); @@ -173,7 +171,7 @@ elf_flagphdr(Elf *e, Elf_Cmd c, unsigned int flags) unsigned int elf_flagscn(Elf_Scn *s, Elf_Cmd c, unsigned int flags) { - int r; + unsigned int r; if (s == NULL) return (0); diff --git a/contrib/elftoolchain/libelf/elf_getarhdr.c b/contrib/elftoolchain/libelf/elf_getarhdr.c index 07f6888147bf..75e2e86226ec 100644 --- a/contrib/elftoolchain/libelf/elf_getarhdr.c +++ b/contrib/elftoolchain/libelf/elf_getarhdr.c @@ -24,13 +24,11 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> - #include <libelf.h> #include "_libelf.h" -ELFTC_VCSID("$Id: elf_getarhdr.c 2225 2011-11-26 18:55:54Z jkoshy $"); +ELFTC_VCSID("$Id: elf_getarhdr.c 3174 2015-03-27 17:13:41Z emaste $"); Elf_Arhdr * elf_getarhdr(Elf *e) diff --git a/contrib/elftoolchain/libelf/elf_getarsym.c b/contrib/elftoolchain/libelf/elf_getarsym.c index eeb8513aa4ec..3679b81e155b 100644 --- a/contrib/elftoolchain/libelf/elf_getarsym.c +++ b/contrib/elftoolchain/libelf/elf_getarsym.c @@ -24,13 +24,11 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> - #include <libelf.h> #include "_libelf.h" -ELFTC_VCSID("$Id: elf_getarsym.c 2225 2011-11-26 18:55:54Z jkoshy $"); +ELFTC_VCSID("$Id: elf_getarsym.c 3174 2015-03-27 17:13:41Z emaste $"); Elf_Arsym * elf_getarsym(Elf *ar, size_t *ptr) diff --git a/contrib/elftoolchain/libelf/elf_getbase.c b/contrib/elftoolchain/libelf/elf_getbase.c index 417808df360c..b038c3ddd260 100644 --- a/contrib/elftoolchain/libelf/elf_getbase.c +++ b/contrib/elftoolchain/libelf/elf_getbase.c @@ -24,13 +24,11 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> - #include <libelf.h> #include "_libelf.h" -ELFTC_VCSID("$Id: elf_getbase.c 2225 2011-11-26 18:55:54Z jkoshy $"); +ELFTC_VCSID("$Id: elf_getbase.c 3174 2015-03-27 17:13:41Z emaste $"); off_t elf_getbase(Elf *e) diff --git a/contrib/elftoolchain/libelf/elf_getdata.3 b/contrib/elftoolchain/libelf/elf_getdata.3 index 8816a5a0694f..68b28890b4d6 100644 --- a/contrib/elftoolchain/libelf/elf_getdata.3 +++ b/contrib/elftoolchain/libelf/elf_getdata.3 @@ -21,7 +21,7 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: elf_getdata.3 1766 2011-08-22 06:01:03Z jkoshy $ +.\" $Id: elf_getdata.3 3181 2015-04-10 13:22:51Z emaste $ .\" .Dd January 26, 2011 .Os @@ -174,7 +174,7 @@ These functions return a valid pointer to a data descriptor if successful, or NULL if an error occurs. .Sh ERRORS These functions may fail with the following errors: -.Bl -tag -width "[ELF_E_RESOURCE]" +.Bl -tag -width "[ELF_E_RESOURCE]" .It Bq Er ELF_E_ARGUMENT Either of the arguments .Ar scn diff --git a/contrib/elftoolchain/libelf/elf_getident.c b/contrib/elftoolchain/libelf/elf_getident.c index aeea996b9fac..a69139c932cf 100644 --- a/contrib/elftoolchain/libelf/elf_getident.c +++ b/contrib/elftoolchain/libelf/elf_getident.c @@ -24,15 +24,13 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> - #include <ar.h> #include <assert.h> #include <libelf.h> #include "_libelf.h" -ELFTC_VCSID("$Id: elf_getident.c 2225 2011-11-26 18:55:54Z jkoshy $"); +ELFTC_VCSID("$Id: elf_getident.c 3174 2015-03-27 17:13:41Z emaste $"); char * elf_getident(Elf *e, size_t *sz) diff --git a/contrib/elftoolchain/libelf/elf_hash.c b/contrib/elftoolchain/libelf/elf_hash.c index 373523714bcb..ef7e44d1afdb 100644 --- a/contrib/elftoolchain/libelf/elf_hash.c +++ b/contrib/elftoolchain/libelf/elf_hash.c @@ -24,13 +24,11 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> - #include <libelf.h> #include "_libelf.h" -ELFTC_VCSID("$Id: elf_hash.c 2225 2011-11-26 18:55:54Z jkoshy $"); +ELFTC_VCSID("$Id: elf_hash.c 3174 2015-03-27 17:13:41Z emaste $"); /* * This elf_hash function is defined by the System V ABI. diff --git a/contrib/elftoolchain/libelf/elf_kind.c b/contrib/elftoolchain/libelf/elf_kind.c index ba83adce8453..f497bd315a8f 100644 --- a/contrib/elftoolchain/libelf/elf_kind.c +++ b/contrib/elftoolchain/libelf/elf_kind.c @@ -24,13 +24,11 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> - #include <libelf.h> #include "_libelf.h" -ELFTC_VCSID("$Id: elf_kind.c 2225 2011-11-26 18:55:54Z jkoshy $"); +ELFTC_VCSID("$Id: elf_kind.c 3174 2015-03-27 17:13:41Z emaste $"); Elf_Kind elf_kind(Elf *e) diff --git a/contrib/elftoolchain/libelf/elf_memory.c b/contrib/elftoolchain/libelf/elf_memory.c index 9c4755d0f59b..d70f6e094912 100644 --- a/contrib/elftoolchain/libelf/elf_memory.c +++ b/contrib/elftoolchain/libelf/elf_memory.c @@ -28,7 +28,7 @@ #include "_libelf.h" -ELFTC_VCSID("$Id: elf_memory.c 2368 2011-12-29 06:34:28Z jkoshy $"); +ELFTC_VCSID("$Id: elf_memory.c 3013 2014-03-23 06:16:59Z jkoshy $"); Elf * elf_memory(char *image, size_t sz) @@ -43,5 +43,5 @@ elf_memory(char *image, size_t sz) return (NULL); } - return (_libelf_memory(image, sz, 1)); + return (_libelf_memory((unsigned char *) image, sz, 1)); } diff --git a/contrib/elftoolchain/libelf/elf_next.c b/contrib/elftoolchain/libelf/elf_next.c index 605a593dd9e3..4c33714f291a 100644 --- a/contrib/elftoolchain/libelf/elf_next.c +++ b/contrib/elftoolchain/libelf/elf_next.c @@ -24,15 +24,13 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> - #include <ar.h> #include <assert.h> #include <libelf.h> #include "_libelf.h" -ELFTC_VCSID("$Id: elf_next.c 2225 2011-11-26 18:55:54Z jkoshy $"); +ELFTC_VCSID("$Id: elf_next.c 3174 2015-03-27 17:13:41Z emaste $"); Elf_Cmd elf_next(Elf *e) @@ -48,13 +46,17 @@ elf_next(Elf *e) return (ELF_C_NULL); } - assert (parent->e_kind == ELF_K_AR); - assert (parent->e_cmd == ELF_C_READ); + assert(parent->e_kind == ELF_K_AR); + assert(parent->e_cmd == ELF_C_READ); assert(e->e_rawfile > parent->e_rawfile); - next = e->e_rawfile - parent->e_rawfile + e->e_rawsize; + next = e->e_rawfile - parent->e_rawfile + (off_t) e->e_rawsize; next = (next + 1) & ~1; /* round up to an even boundary */ + /* + * Setup the 'e_next' field of the archive descriptor for the + * next call to 'elf_begin()'. + */ parent->e_u.e_ar.e_next = (next >= (off_t) parent->e_rawsize) ? (off_t) 0 : next; diff --git a/contrib/elftoolchain/libelf/elf_open.3 b/contrib/elftoolchain/libelf/elf_open.3 index dad969e2a202..3dd1e36a03f4 100644 --- a/contrib/elftoolchain/libelf/elf_open.3 +++ b/contrib/elftoolchain/libelf/elf_open.3 @@ -21,11 +21,11 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: elf_open.3 2512 2012-05-31 06:15:57Z jkoshy $ +.\" $Id: elf_open.3 3181 2015-04-10 13:22:51Z emaste $ .\" .Dd May 31, 2012 .Os -.Dt ELF_OPEN 3 +.Dt ELF_OPEN 3 .Sh NAME .Nm elf_open .Nd open ELF objects and ar(1) archives diff --git a/contrib/elftoolchain/libelf/elf_open.c b/contrib/elftoolchain/libelf/elf_open.c index b039431571fb..5aad459f4002 100644 --- a/contrib/elftoolchain/libelf/elf_open.c +++ b/contrib/elftoolchain/libelf/elf_open.c @@ -63,5 +63,5 @@ elf_openmemory(char *image, size_t sz) return (NULL); } - return (_libelf_memory(image, sz, 0)); + return (_libelf_memory((unsigned char *) image, sz, 0)); } diff --git a/contrib/elftoolchain/libelf/elf_phnum.c b/contrib/elftoolchain/libelf/elf_phnum.c index 59948061b407..4ac665df308d 100644 --- a/contrib/elftoolchain/libelf/elf_phnum.c +++ b/contrib/elftoolchain/libelf/elf_phnum.c @@ -24,14 +24,12 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> - #include <ar.h> #include <libelf.h> #include "_libelf.h" -ELFTC_VCSID("$Id: elf_phnum.c 2225 2011-11-26 18:55:54Z jkoshy $"); +ELFTC_VCSID("$Id: elf_phnum.c 3174 2015-03-27 17:13:41Z emaste $"); static int _libelf_getphdrnum(Elf *e, size_t *phnum) diff --git a/contrib/elftoolchain/libelf/elf_rand.c b/contrib/elftoolchain/libelf/elf_rand.c index f48f01745802..eb2c9eaa2c1a 100644 --- a/contrib/elftoolchain/libelf/elf_rand.c +++ b/contrib/elftoolchain/libelf/elf_rand.c @@ -24,14 +24,12 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> - #include <ar.h> #include <libelf.h> #include "_libelf.h" -ELFTC_VCSID("$Id: elf_rand.c 2225 2011-11-26 18:55:54Z jkoshy $"); +ELFTC_VCSID("$Id: elf_rand.c 3174 2015-03-27 17:13:41Z emaste $"); off_t elf_rand(Elf *ar, off_t offset) @@ -40,7 +38,7 @@ elf_rand(Elf *ar, off_t offset) if (ar == NULL || ar->e_kind != ELF_K_AR || (offset & 1) || offset < SARMAG || - offset + sizeof(struct ar_hdr) >= ar->e_rawsize) { + (size_t) offset + sizeof(struct ar_hdr) >= ar->e_rawsize) { LIBELF_SET_ERROR(ARGUMENT, 0); return 0; } diff --git a/contrib/elftoolchain/libelf/elf_rawfile.c b/contrib/elftoolchain/libelf/elf_rawfile.c index 76cfd7fa3435..f63982fb0b4b 100644 --- a/contrib/elftoolchain/libelf/elf_rawfile.c +++ b/contrib/elftoolchain/libelf/elf_rawfile.c @@ -24,19 +24,17 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> - #include <libelf.h> #include "_libelf.h" -ELFTC_VCSID("$Id: elf_rawfile.c 2225 2011-11-26 18:55:54Z jkoshy $"); +ELFTC_VCSID("$Id: elf_rawfile.c 3174 2015-03-27 17:13:41Z emaste $"); char * elf_rawfile(Elf *e, size_t *sz) { - char *ptr; size_t size; + unsigned char *ptr; size = e ? e->e_rawsize : 0; ptr = NULL; @@ -49,5 +47,5 @@ elf_rawfile(Elf *e, size_t *sz) if (sz) *sz = size; - return (ptr); + return ((char *) ptr); } diff --git a/contrib/elftoolchain/libelf/elf_scn.c b/contrib/elftoolchain/libelf/elf_scn.c index 357fbb33d847..d3e274715b78 100644 --- a/contrib/elftoolchain/libelf/elf_scn.c +++ b/contrib/elftoolchain/libelf/elf_scn.c @@ -24,7 +24,6 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> #include <sys/queue.h> #include <assert.h> @@ -32,11 +31,12 @@ #include <gelf.h> #include <libelf.h> #include <stddef.h> +#include <stdint.h> #include <stdlib.h> #include "_libelf.h" -ELFTC_VCSID("$Id: elf_scn.c 2225 2011-11-26 18:55:54Z jkoshy $"); +ELFTC_VCSID("$Id: elf_scn.c 3177 2015-03-30 18:19:41Z emaste $"); /* * Load an ELF section table and create a list of Elf_Scn structures. @@ -44,22 +44,25 @@ ELFTC_VCSID("$Id: elf_scn.c 2225 2011-11-26 18:55:54Z jkoshy $"); int _libelf_load_section_headers(Elf *e, void *ehdr) { - int ec, swapbytes; - size_t fsz, i, shnum; + Elf_Scn *scn; uint64_t shoff; - char *src; Elf32_Ehdr *eh32; Elf64_Ehdr *eh64; - Elf_Scn *scn; - int (*xlator)(char *_d, size_t _dsz, char *_s, size_t _c, int _swap); + int ec, swapbytes; + unsigned char *src; + size_t fsz, i, shnum; + int (*xlator)(unsigned char *_d, size_t _dsz, unsigned char *_s, + size_t _c, int _swap); assert(e != NULL); assert(ehdr != NULL); assert((e->e_flags & LIBELF_F_SHDRS_LOADED) == 0); #define CHECK_EHDR(E,EH) do { \ - if (fsz != (EH)->e_shentsize || \ - shoff + fsz * shnum > e->e_rawsize) { \ + if (shoff > e->e_rawsize || \ + fsz != (EH)->e_shentsize || \ + shnum > SIZE_MAX / fsz || \ + fsz * shnum > e->e_rawsize - shoff) { \ LIBELF_SET_ERROR(HEADER, 0); \ return (0); \ } \ @@ -104,8 +107,8 @@ _libelf_load_section_headers(Elf *e, void *ehdr) if ((scn = _libelf_allocate_scn(e, i)) == NULL) return (0); - (*xlator)((char *) &scn->s_shdr, sizeof(scn->s_shdr), src, - (size_t) 1, swapbytes); + (*xlator)((unsigned char *) &scn->s_shdr, sizeof(scn->s_shdr), + src, (size_t) 1, swapbytes); if (ec == ELFCLASS32) { scn->s_offset = scn->s_rawoff = diff --git a/contrib/elftoolchain/libelf/elf_shnum.c b/contrib/elftoolchain/libelf/elf_shnum.c index a06a9e411310..5c0560fb5567 100644 --- a/contrib/elftoolchain/libelf/elf_shnum.c +++ b/contrib/elftoolchain/libelf/elf_shnum.c @@ -24,14 +24,12 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> - #include <ar.h> #include <libelf.h> #include "_libelf.h" -ELFTC_VCSID("$Id: elf_shnum.c 2225 2011-11-26 18:55:54Z jkoshy $"); +ELFTC_VCSID("$Id: elf_shnum.c 3174 2015-03-27 17:13:41Z emaste $"); static int _libelf_getshdrnum(Elf *e, size_t *shnum) diff --git a/contrib/elftoolchain/libelf/elf_shstrndx.c b/contrib/elftoolchain/libelf/elf_shstrndx.c index 0a7bab96b1fc..dac40c99a0dd 100644 --- a/contrib/elftoolchain/libelf/elf_shstrndx.c +++ b/contrib/elftoolchain/libelf/elf_shstrndx.c @@ -24,14 +24,12 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> - #include <ar.h> #include <libelf.h> #include "_libelf.h" -ELFTC_VCSID("$Id: elf_shstrndx.c 2225 2011-11-26 18:55:54Z jkoshy $"); +ELFTC_VCSID("$Id: elf_shstrndx.c 3174 2015-03-27 17:13:41Z emaste $"); static int _libelf_getshdrstrndx(Elf *e, size_t *strndx) diff --git a/contrib/elftoolchain/libelf/elf_strptr.c b/contrib/elftoolchain/libelf/elf_strptr.c index c79970dc8907..e2a6b2899f58 100644 --- a/contrib/elftoolchain/libelf/elf_strptr.c +++ b/contrib/elftoolchain/libelf/elf_strptr.c @@ -31,7 +31,7 @@ #include "_libelf.h" -ELFTC_VCSID("$Id: elf_strptr.c 2271 2011-12-03 17:06:35Z jkoshy $"); +ELFTC_VCSID("$Id: elf_strptr.c 2990 2014-03-17 09:56:58Z jkoshy $"); /* * Convert an ELF section#,offset pair to a string pointer. @@ -42,8 +42,8 @@ elf_strptr(Elf *e, size_t scndx, size_t offset) { Elf_Scn *s; Elf_Data *d; - size_t alignment, count; GElf_Shdr shdr; + uint64_t alignment, count; if (e == NULL || e->e_kind != ELF_K_ELF) { LIBELF_SET_ERROR(ARGUMENT, 0); @@ -90,7 +90,7 @@ elf_strptr(Elf *e, size_t scndx, size_t offset) * account 'holes' in coverage of the section introduced * by alignment requirements. */ - count = (size_t) 0; /* cumulative count of bytes seen */ + count = (uint64_t) 0; /* cumulative count of bytes seen */ while ((d = elf_getdata(s, d)) != NULL && count <= offset) { if (d->d_buf == NULL || d->d_size == 0) diff --git a/contrib/elftoolchain/libelf/elf_update.c b/contrib/elftoolchain/libelf/elf_update.c index b589233c8e4a..6cc109daafcc 100644 --- a/contrib/elftoolchain/libelf/elf_update.c +++ b/contrib/elftoolchain/libelf/elf_update.c @@ -41,7 +41,7 @@ #include <sys/mman.h> #endif -ELFTC_VCSID("$Id: elf_update.c 2931 2013-03-23 11:41:07Z jkoshy $"); +ELFTC_VCSID("$Id: elf_update.c 3190 2015-05-04 15:23:08Z jkoshy $"); /* * Layout strategy: @@ -110,14 +110,13 @@ SLIST_HEAD(_Elf_Extent_List, _Elf_Extent); static int _libelf_compute_section_extents(Elf *e, Elf_Scn *s, off_t rc) { - int ec; Elf_Data *d; size_t fsz, msz; + int ec, elftype; uint32_t sh_type; uint64_t d_align; Elf32_Shdr *shdr32; Elf64_Shdr *shdr64; - unsigned int elftype; struct _Libelf_Data *ld; uint64_t scn_size, scn_alignment; uint64_t sh_align, sh_entsize, sh_offset, sh_size; @@ -253,7 +252,7 @@ _libelf_compute_section_extents(Elf *e, Elf_Scn *s, off_t rc) scn_size = roundup2(scn_size, d->d_align); d->d_off = scn_size; fsz = _libelf_fsize(d->d_type, ec, d->d_version, - d->d_size / msz); + (size_t) d->d_size / msz); scn_size += fsz; } @@ -272,8 +271,10 @@ _libelf_compute_section_extents(Elf *e, Elf_Scn *s, off_t rc) * offsets and alignment for sanity. */ if (e->e_flags & ELF_F_LAYOUT) { - if (scn_alignment > sh_align || sh_offset % sh_align || - sh_size < scn_size) { + if (scn_alignment > sh_align || + sh_offset % sh_align || + sh_size < scn_size || + sh_offset % _libelf_falign(elftype, ec)) { LIBELF_SET_ERROR(LAYOUT, 0); return (0); } @@ -307,7 +308,7 @@ computeoffset: * Compute the new offset for the section based on * the section's alignment needs. */ - sh_offset = roundup(rc, sh_align); + sh_offset = roundup((uint64_t) rc, sh_align); /* * Update the section header. @@ -471,7 +472,7 @@ _libelf_resync_sections(Elf *e, off_t rc, struct _Elf_Extent_List *extents) return ((off_t) -1); if ((size_t) rc < s->s_offset + s->s_size) - rc = s->s_offset + s->s_size; + rc = (off_t) (s->s_offset + s->s_size); } return (rc); @@ -529,17 +530,22 @@ _libelf_resync_elf(Elf *e, struct _Elf_Extent_List *extents) if (ec == ELFCLASS32) { eh_byteorder = eh32->e_ident[EI_DATA]; eh_class = eh32->e_ident[EI_CLASS]; - phoff = (uint64_t) eh32->e_phoff; - shoff = (uint64_t) eh32->e_shoff; + phoff = (off_t) eh32->e_phoff; + shoff = (off_t) eh32->e_shoff; eh_version = eh32->e_version; } else { eh_byteorder = eh64->e_ident[EI_DATA]; eh_class = eh64->e_ident[EI_CLASS]; - phoff = eh64->e_phoff; - shoff = eh64->e_shoff; + phoff = (off_t) eh64->e_phoff; + shoff = (off_t) eh64->e_shoff; eh_version = eh64->e_version; } + if (phoff < 0 || shoff < 0) { + LIBELF_SET_ERROR(HEADER, 0); + return ((off_t) -1); + } + if (eh_version == EV_NONE) eh_version = EV_CURRENT; @@ -564,18 +570,20 @@ _libelf_resync_elf(Elf *e, struct _Elf_Extent_List *extents) e->e_byteorder = eh_byteorder; #define INITIALIZE_EHDR(E,EC,V) do { \ + unsigned int _version = (unsigned int) (V); \ (E)->e_ident[EI_MAG0] = ELFMAG0; \ (E)->e_ident[EI_MAG1] = ELFMAG1; \ (E)->e_ident[EI_MAG2] = ELFMAG2; \ (E)->e_ident[EI_MAG3] = ELFMAG3; \ - (E)->e_ident[EI_CLASS] = (EC); \ - (E)->e_ident[EI_VERSION] = (V); \ - (E)->e_ehsize = _libelf_fsize(ELF_T_EHDR, (EC), (V), \ - (size_t) 1); \ - (E)->e_phentsize = (phnum == 0) ? 0 : _libelf_fsize( \ - ELF_T_PHDR, (EC), (V), (size_t) 1); \ - (E)->e_shentsize = _libelf_fsize(ELF_T_SHDR, (EC), (V), \ - (size_t) 1); \ + (E)->e_ident[EI_CLASS] = (unsigned char) (EC); \ + (E)->e_ident[EI_VERSION] = (_version & 0xFFU); \ + (E)->e_ehsize = (uint16_t) _libelf_fsize(ELF_T_EHDR, \ + (EC), _version, (size_t) 1); \ + (E)->e_phentsize = (uint16_t) ((phnum == 0) ? 0 : \ + _libelf_fsize(ELF_T_PHDR, (EC), _version, \ + (size_t) 1)); \ + (E)->e_shentsize = (uint16_t) _libelf_fsize(ELF_T_SHDR, \ + (EC), _version, (size_t) 1); \ } while (0) if (ec == ELFCLASS32) @@ -585,9 +593,10 @@ _libelf_resync_elf(Elf *e, struct _Elf_Extent_List *extents) (void) elf_flagehdr(e, ELF_C_SET, ELF_F_DIRTY); - rc += _libelf_fsize(ELF_T_EHDR, ec, eh_version, (size_t) 1); + rc += (off_t) _libelf_fsize(ELF_T_EHDR, ec, eh_version, (size_t) 1); - if (!_libelf_insert_extent(extents, ELF_EXTENT_EHDR, 0, rc, ehdr)) + if (!_libelf_insert_extent(extents, ELF_EXTENT_EHDR, 0, (uint64_t) rc, + ehdr)) return ((off_t) -1); /* @@ -608,20 +617,20 @@ _libelf_resync_elf(Elf *e, struct _Elf_Extent_List *extents) return ((off_t) -1); } - if (phoff % align) { + if (phoff % (off_t) align) { LIBELF_SET_ERROR(LAYOUT, 0); return ((off_t) -1); } } else - phoff = roundup(rc, align); + phoff = roundup(rc, (off_t) align); - rc = phoff + fsz; + rc = phoff + (off_t) fsz; phdr = _libelf_getphdr(e, ec); - if (!_libelf_insert_extent(extents, ELF_EXTENT_PHDR, phoff, - fsz, phdr)) + if (!_libelf_insert_extent(extents, ELF_EXTENT_PHDR, + (uint64_t) phoff, fsz, phdr)) return ((off_t) -1); } else phoff = 0; @@ -656,18 +665,18 @@ _libelf_resync_elf(Elf *e, struct _Elf_Extent_List *extents) align = _libelf_falign(ELF_T_SHDR, ec); if (e->e_flags & ELF_F_LAYOUT) { - if (shoff % align) { + if (shoff % (off_t) align) { LIBELF_SET_ERROR(LAYOUT, 0); return ((off_t) -1); } } else - shoff = roundup(rc, align); + shoff = roundup(rc, (off_t) align); - if (shoff + fsz > (size_t) rc) - rc = shoff + fsz; + if (shoff + (off_t) fsz > rc) + rc = shoff + (off_t) fsz; - if (!_libelf_insert_extent(extents, ELF_EXTENT_SHDR, shoff, - fsz, NULL)) + if (!_libelf_insert_extent(extents, ELF_EXTENT_SHDR, + (uint64_t) shoff, fsz, NULL)) return ((off_t) -1); } else shoff = 0; @@ -700,22 +709,23 @@ _libelf_resync_elf(Elf *e, struct _Elf_Extent_List *extents) * Write out the contents of an ELF section. */ -static size_t -_libelf_write_scn(Elf *e, char *nf, struct _Elf_Extent *ex) +static off_t +_libelf_write_scn(Elf *e, unsigned char *nf, struct _Elf_Extent *ex) { int ec; + off_t rc; Elf_Scn *s; int elftype; Elf_Data *d, dst; uint32_t sh_type; struct _Libelf_Data *ld; uint64_t sh_off, sh_size; - size_t fsz, msz, nobjects, rc; + size_t fsz, msz, nobjects; assert(ex->ex_type == ELF_EXTENT_SECTION); s = ex->ex_desc; - rc = ex->ex_start; + rc = (off_t) ex->ex_start; if ((ec = e->e_class) == ELFCLASS32) { sh_type = s->s_shdr.s_shdr32.sh_type; @@ -756,18 +766,20 @@ _libelf_write_scn(Elf *e, char *nf, struct _Elf_Extent *ex) if ((uint64_t) rc < sh_off + d->d_off) (void) memset(nf + rc, - LIBELF_PRIVATE(fillchar), sh_off + - d->d_off - rc); - rc = sh_off + d->d_off; + LIBELF_PRIVATE(fillchar), + (size_t) (sh_off + d->d_off - + (uint64_t) rc)); + rc = (off_t) (sh_off + d->d_off); assert(d->d_buf != NULL); assert(d->d_type == ELF_T_BYTE); assert(d->d_version == e->e_version); (void) memcpy(nf + rc, - e->e_rawfile + s->s_rawoff + d->d_off, d->d_size); + e->e_rawfile + s->s_rawoff + d->d_off, + (size_t) d->d_size); - rc += d->d_size; + rc += (off_t) d->d_size; } return (rc); @@ -789,15 +801,16 @@ _libelf_write_scn(Elf *e, char *nf, struct _Elf_Extent *ex) if ((uint64_t) rc < sh_off + d->d_off) (void) memset(nf + rc, - LIBELF_PRIVATE(fillchar), sh_off + d->d_off - rc); + LIBELF_PRIVATE(fillchar), + (size_t) (sh_off + d->d_off - (uint64_t) rc)); - rc = sh_off + d->d_off; + rc = (off_t) (sh_off + d->d_off); assert(d->d_buf != NULL); assert(d->d_version == e->e_version); assert(d->d_size % msz == 0); - nobjects = d->d_size / msz; + nobjects = (size_t) (d->d_size / msz); fsz = _libelf_fsize(d->d_type, ec, e->e_version, nobjects); @@ -808,10 +821,10 @@ _libelf_write_scn(Elf *e, char *nf, struct _Elf_Extent *ex) NULL) return ((off_t) -1); - rc += fsz; + rc += (off_t) fsz; } - return ((off_t) rc); + return (rc); } /* @@ -819,7 +832,7 @@ _libelf_write_scn(Elf *e, char *nf, struct _Elf_Extent *ex) */ static off_t -_libelf_write_ehdr(Elf *e, char *nf, struct _Elf_Extent *ex) +_libelf_write_ehdr(Elf *e, unsigned char *nf, struct _Elf_Extent *ex) { int ec; void *ehdr; @@ -860,7 +873,7 @@ _libelf_write_ehdr(Elf *e, char *nf, struct _Elf_Extent *ex) */ static off_t -_libelf_write_phdr(Elf *e, char *nf, struct _Elf_Extent *ex) +_libelf_write_phdr(Elf *e, unsigned char *nf, struct _Elf_Extent *ex) { int ec; void *ehdr; @@ -909,7 +922,7 @@ _libelf_write_phdr(Elf *e, char *nf, struct _Elf_Extent *ex) NULL) return ((off_t) -1); - return (phoff + fsz); + return ((off_t) (phoff + fsz)); } /* @@ -917,7 +930,7 @@ _libelf_write_phdr(Elf *e, char *nf, struct _Elf_Extent *ex) */ static off_t -_libelf_write_shdr(Elf *e, char *nf, struct _Elf_Extent *ex) +_libelf_write_shdr(Elf *e, unsigned char *nf, struct _Elf_Extent *ex) { int ec; void *ehdr; @@ -969,7 +982,7 @@ _libelf_write_shdr(Elf *e, char *nf, struct _Elf_Extent *ex) return ((off_t) -1); } - return (ex->ex_start + nscn * fsz); + return ((off_t) (ex->ex_start + nscn * fsz)); } /* @@ -993,9 +1006,9 @@ static off_t _libelf_write_elf(Elf *e, off_t newsize, struct _Elf_Extent_List *extents) { off_t nrc, rc; - char *newfile; Elf_Scn *scn, *tscn; struct _Elf_Extent *ex; + unsigned char *newfile; assert(e->e_kind == ELF_K_ELF); assert(e->e_cmd == ELF_C_RDWR || e->e_cmd == ELF_C_WRITE); @@ -1012,7 +1025,7 @@ _libelf_write_elf(Elf *e, off_t newsize, struct _Elf_Extent_List *extents) /* Fill inter-extent gaps. */ if (ex->ex_start > (size_t) rc) (void) memset(newfile + rc, LIBELF_PRIVATE(fillchar), - ex->ex_start - rc); + (size_t) (ex->ex_start - (uint64_t) rc)); switch (ex->ex_type) { case ELF_EXTENT_EHDR: @@ -1103,7 +1116,7 @@ _libelf_write_elf(Elf *e, off_t newsize, struct _Elf_Extent_List *extents) #endif /* ELFTC_HAVE_MMAP */ /* Record the new size of the file. */ - e->e_rawsize = newsize; + e->e_rawsize = (size_t) newsize; } else { /* File opened in ELF_C_WRITE mode. */ assert(e->e_rawfile == NULL); diff --git a/contrib/elftoolchain/libelf/elf_version.c b/contrib/elftoolchain/libelf/elf_version.c index 3aa466ad3c4f..cd25954b44e9 100644 --- a/contrib/elftoolchain/libelf/elf_version.c +++ b/contrib/elftoolchain/libelf/elf_version.c @@ -24,13 +24,11 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> - #include <libelf.h> #include "_libelf.h" -ELFTC_VCSID("$Id: elf_version.c 2225 2011-11-26 18:55:54Z jkoshy $"); +ELFTC_VCSID("$Id: elf_version.c 3174 2015-03-27 17:13:41Z emaste $"); unsigned int elf_version(unsigned int v) diff --git a/contrib/elftoolchain/libelf/gelf.3 b/contrib/elftoolchain/libelf/gelf.3 index a5d68cefc4ab..d00d5b330980 100644 --- a/contrib/elftoolchain/libelf/gelf.3 +++ b/contrib/elftoolchain/libelf/gelf.3 @@ -21,7 +21,7 @@ .\" out of the use of this software, even if advised of the possibility of .\" such damage. .\" -.\" $Id: gelf.3 189 2008-07-20 10:38:08Z jkoshy $ +.\" $Id: gelf.3 3195 2015-05-12 17:22:19Z emaste $ .\" .Dd September 1, 2006 .Os @@ -197,5 +197,4 @@ This implementation of the API first appeared in .Fx 7.0 . .Sh AUTHORS The GElf API was implemented by -.An "Joseph Koshy" -.Aq jkoshy@FreeBSD.org . +.An Joseph Koshy Aq Mt jkoshy@FreeBSD.org . diff --git a/contrib/elftoolchain/libelf/gelf.h b/contrib/elftoolchain/libelf/gelf.h index 0a7dc2494024..6eae0a5f27f5 100644 --- a/contrib/elftoolchain/libelf/gelf.h +++ b/contrib/elftoolchain/libelf/gelf.h @@ -23,14 +23,12 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: gelf.h 1168 2010-09-04 01:03:25Z jkoshy $ + * $Id: gelf.h 3174 2015-03-27 17:13:41Z emaste $ */ #ifndef _GELF_H_ #define _GELF_H_ -#include <sys/cdefs.h> - #include <libelf.h> typedef Elf64_Addr GElf_Addr; /* Addresses */ @@ -69,7 +67,9 @@ typedef Elf64_Syminfo GElf_Syminfo; /* Symbol information */ #define GELF_ST_TYPE ELF64_ST_TYPE #define GELF_ST_VISIBILITY ELF64_ST_VISIBILITY -__BEGIN_DECLS +#ifdef __cplusplus +extern "C" { +#endif long gelf_checksum(Elf *_elf); size_t gelf_fsize(Elf *_elf, Elf_Type _type, size_t _count, unsigned int _version); @@ -103,6 +103,8 @@ GElf_Syminfo *gelf_getsyminfo(Elf_Data *_src, int _index, GElf_Syminfo *_dst); int gelf_update_cap(Elf_Data *_dst, int _index, GElf_Cap *_src); int gelf_update_move(Elf_Data *_dst, int _index, GElf_Move *_src); int gelf_update_syminfo(Elf_Data *_dst, int _index, GElf_Syminfo *_src); -__END_DECLS +#ifdef __cplusplus +} +#endif #endif /* _GELF_H_ */ diff --git a/contrib/elftoolchain/libelf/gelf_cap.c b/contrib/elftoolchain/libelf/gelf_cap.c index a1c1417be1fa..f509c69c2ba5 100644 --- a/contrib/elftoolchain/libelf/gelf_cap.c +++ b/contrib/elftoolchain/libelf/gelf_cap.c @@ -24,15 +24,14 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> - #include <assert.h> #include <gelf.h> #include <limits.h> +#include <stdint.h> #include "_libelf.h" -ELFTC_VCSID("$Id: gelf_cap.c 2272 2011-12-03 17:07:31Z jkoshy $"); +ELFTC_VCSID("$Id: gelf_cap.c 3177 2015-03-30 18:19:41Z emaste $"); GElf_Cap * gelf_getcap(Elf_Data *ed, int ndx, GElf_Cap *dst) @@ -72,7 +71,7 @@ gelf_getcap(Elf_Data *ed, int ndx, GElf_Cap *dst) assert(msz > 0); - if (msz * ndx >= d->d_data.d_size) { + if (msz * (size_t) ndx >= d->d_data.d_size) { LIBELF_SET_ERROR(ARGUMENT, 0); return (NULL); } @@ -131,7 +130,7 @@ gelf_update_cap(Elf_Data *ed, int ndx, GElf_Cap *gc) msz = _libelf_msize(ELF_T_CAP, ec, e->e_version); assert(msz > 0); - if (msz * ndx >= d->d_data.d_size) { + if (msz * (size_t) ndx >= d->d_data.d_size) { LIBELF_SET_ERROR(ARGUMENT, 0); return (0); } diff --git a/contrib/elftoolchain/libelf/gelf_checksum.c b/contrib/elftoolchain/libelf/gelf_checksum.c index af707daeba1b..b16272f39610 100644 --- a/contrib/elftoolchain/libelf/gelf_checksum.c +++ b/contrib/elftoolchain/libelf/gelf_checksum.c @@ -24,14 +24,12 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> - #include <gelf.h> #include <libelf.h> #include "_libelf.h" -ELFTC_VCSID("$Id: gelf_checksum.c 2225 2011-11-26 18:55:54Z jkoshy $"); +ELFTC_VCSID("$Id: gelf_checksum.c 3174 2015-03-27 17:13:41Z emaste $"); long elf32_checksum(Elf *e) diff --git a/contrib/elftoolchain/libelf/gelf_dyn.c b/contrib/elftoolchain/libelf/gelf_dyn.c index 263102603397..74233094bf6c 100644 --- a/contrib/elftoolchain/libelf/gelf_dyn.c +++ b/contrib/elftoolchain/libelf/gelf_dyn.c @@ -24,15 +24,14 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> - #include <assert.h> #include <gelf.h> #include <limits.h> +#include <stdint.h> #include "_libelf.h" -ELFTC_VCSID("$Id: gelf_dyn.c 2272 2011-12-03 17:07:31Z jkoshy $"); +ELFTC_VCSID("$Id: gelf_dyn.c 3177 2015-03-30 18:19:41Z emaste $"); GElf_Dyn * gelf_getdyn(Elf_Data *ed, int ndx, GElf_Dyn *dst) @@ -71,8 +70,9 @@ gelf_getdyn(Elf_Data *ed, int ndx, GElf_Dyn *dst) msz = _libelf_msize(ELF_T_DYN, ec, e->e_version); assert(msz > 0); + assert(ndx >= 0); - if (msz * ndx >= d->d_data.d_size) { + if (msz * (size_t) ndx >= d->d_data.d_size) { LIBELF_SET_ERROR(ARGUMENT, 0); return (NULL); } @@ -128,9 +128,11 @@ gelf_update_dyn(Elf_Data *ed, int ndx, GElf_Dyn *ds) } msz = _libelf_msize(ELF_T_DYN, ec, e->e_version); + assert(msz > 0); + assert(ndx >= 0); - if (msz * ndx >= d->d_data.d_size) { + if (msz * (size_t) ndx >= d->d_data.d_size) { LIBELF_SET_ERROR(ARGUMENT, 0); return (0); } diff --git a/contrib/elftoolchain/libelf/gelf_ehdr.c b/contrib/elftoolchain/libelf/gelf_ehdr.c index 1efd8178c560..dcd260e14ba2 100644 --- a/contrib/elftoolchain/libelf/gelf_ehdr.c +++ b/contrib/elftoolchain/libelf/gelf_ehdr.c @@ -24,17 +24,16 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> - #include <assert.h> #include <gelf.h> #include <libelf.h> #include <limits.h> +#include <stdint.h> #include <string.h> #include "_libelf.h" -ELFTC_VCSID("$Id: gelf_ehdr.c 2268 2011-12-03 17:05:11Z jkoshy $"); +ELFTC_VCSID("$Id: gelf_ehdr.c 3177 2015-03-30 18:19:41Z emaste $"); Elf32_Ehdr * elf32_getehdr(Elf *e) diff --git a/contrib/elftoolchain/libelf/gelf_fsize.c b/contrib/elftoolchain/libelf/gelf_fsize.c index 7eeac69bf8f1..afdea7381fc9 100644 --- a/contrib/elftoolchain/libelf/gelf_fsize.c +++ b/contrib/elftoolchain/libelf/gelf_fsize.c @@ -24,14 +24,12 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> - #include <gelf.h> #include <libelf.h> #include "_libelf.h" -ELFTC_VCSID("$Id: gelf_fsize.c 2225 2011-11-26 18:55:54Z jkoshy $"); +ELFTC_VCSID("$Id: gelf_fsize.c 3174 2015-03-27 17:13:41Z emaste $"); size_t elf32_fsize(Elf_Type t, size_t c, unsigned int v) diff --git a/contrib/elftoolchain/libelf/gelf_getclass.c b/contrib/elftoolchain/libelf/gelf_getclass.c index 214e60282ce4..d7f680382077 100644 --- a/contrib/elftoolchain/libelf/gelf_getclass.c +++ b/contrib/elftoolchain/libelf/gelf_getclass.c @@ -24,13 +24,11 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> - #include <gelf.h> #include "_libelf.h" -ELFTC_VCSID("$Id: gelf_getclass.c 2225 2011-11-26 18:55:54Z jkoshy $"); +ELFTC_VCSID("$Id: gelf_getclass.c 3174 2015-03-27 17:13:41Z emaste $"); int gelf_getclass(Elf *e) diff --git a/contrib/elftoolchain/libelf/gelf_move.c b/contrib/elftoolchain/libelf/gelf_move.c index d9e8993deee4..2ec8f251f8fa 100644 --- a/contrib/elftoolchain/libelf/gelf_move.c +++ b/contrib/elftoolchain/libelf/gelf_move.c @@ -24,15 +24,14 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> - #include <assert.h> #include <gelf.h> #include <limits.h> +#include <stdint.h> #include "_libelf.h" -ELFTC_VCSID("$Id: gelf_move.c 2272 2011-12-03 17:07:31Z jkoshy $"); +ELFTC_VCSID("$Id: gelf_move.c 3177 2015-03-30 18:19:41Z emaste $"); GElf_Move * gelf_getmove(Elf_Data *ed, int ndx, GElf_Move *dst) @@ -71,8 +70,9 @@ gelf_getmove(Elf_Data *ed, int ndx, GElf_Move *dst) msz = _libelf_msize(ELF_T_MOVE, ec, e->e_version); assert(msz > 0); + assert(ndx >= 0); - if (msz * ndx >= d->d_data.d_size) { + if (msz * (size_t) ndx >= d->d_data.d_size) { LIBELF_SET_ERROR(ARGUMENT, 0); return (NULL); } @@ -131,9 +131,11 @@ gelf_update_move(Elf_Data *ed, int ndx, GElf_Move *gm) } msz = _libelf_msize(ELF_T_MOVE, ec, e->e_version); + assert(msz > 0); + assert(ndx >= 0); - if (msz * ndx >= d->d_data.d_size) { + if (msz * (size_t) ndx >= d->d_data.d_size) { LIBELF_SET_ERROR(ARGUMENT, 0); return (0); } diff --git a/contrib/elftoolchain/libelf/gelf_phdr.c b/contrib/elftoolchain/libelf/gelf_phdr.c index ade82dbdf877..31e20b0680c5 100644 --- a/contrib/elftoolchain/libelf/gelf_phdr.c +++ b/contrib/elftoolchain/libelf/gelf_phdr.c @@ -24,15 +24,14 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> - #include <gelf.h> #include <libelf.h> #include <limits.h> +#include <stdint.h> #include "_libelf.h" -ELFTC_VCSID("$Id: gelf_phdr.c 2268 2011-12-03 17:05:11Z jkoshy $"); +ELFTC_VCSID("$Id: gelf_phdr.c 3177 2015-03-30 18:19:41Z emaste $"); Elf32_Phdr * elf32_getphdr(Elf *e) diff --git a/contrib/elftoolchain/libelf/gelf_rel.c b/contrib/elftoolchain/libelf/gelf_rel.c index eb057177d4cc..02a613383dfb 100644 --- a/contrib/elftoolchain/libelf/gelf_rel.c +++ b/contrib/elftoolchain/libelf/gelf_rel.c @@ -24,15 +24,14 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> - #include <assert.h> #include <gelf.h> #include <limits.h> +#include <stdint.h> #include "_libelf.h" -ELFTC_VCSID("$Id: gelf_rel.c 2272 2011-12-03 17:07:31Z jkoshy $"); +ELFTC_VCSID("$Id: gelf_rel.c 3177 2015-03-30 18:19:41Z emaste $"); GElf_Rel * gelf_getrel(Elf_Data *ed, int ndx, GElf_Rel *dst) @@ -71,8 +70,9 @@ gelf_getrel(Elf_Data *ed, int ndx, GElf_Rel *dst) msz = _libelf_msize(ELF_T_REL, ec, e->e_version); assert(msz > 0); + assert(ndx >= 0); - if (msz * ndx >= d->d_data.d_size) { + if (msz * (size_t) ndx >= d->d_data.d_size) { LIBELF_SET_ERROR(ARGUMENT, 0); return (NULL); } @@ -130,9 +130,11 @@ gelf_update_rel(Elf_Data *ed, int ndx, GElf_Rel *dr) } msz = _libelf_msize(ELF_T_REL, ec, e->e_version); + assert(msz > 0); + assert(ndx >= 0); - if (msz * ndx >= d->d_data.d_size) { + if (msz * (size_t) ndx >= d->d_data.d_size) { LIBELF_SET_ERROR(ARGUMENT, 0); return (0); } @@ -147,8 +149,9 @@ gelf_update_rel(Elf_Data *ed, int ndx, GElf_Rel *dr) LIBELF_SET_ERROR(RANGE, 0); return (0); } - rel32->r_info = ELF32_R_INFO(ELF64_R_SYM(dr->r_info), - ELF64_R_TYPE(dr->r_info)); + rel32->r_info = ELF32_R_INFO( + (Elf32_Word) ELF64_R_SYM(dr->r_info), + (Elf32_Word) ELF64_R_TYPE(dr->r_info)); } else { rel64 = (Elf64_Rel *) d->d_data.d_buf + ndx; diff --git a/contrib/elftoolchain/libelf/gelf_rela.c b/contrib/elftoolchain/libelf/gelf_rela.c index cb61bdc2e2df..d485ab909299 100644 --- a/contrib/elftoolchain/libelf/gelf_rela.c +++ b/contrib/elftoolchain/libelf/gelf_rela.c @@ -24,15 +24,14 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> - #include <assert.h> #include <gelf.h> #include <limits.h> +#include <stdint.h> #include "_libelf.h" -ELFTC_VCSID("$Id: gelf_rela.c 2272 2011-12-03 17:07:31Z jkoshy $"); +ELFTC_VCSID("$Id: gelf_rela.c 3177 2015-03-30 18:19:41Z emaste $"); GElf_Rela * gelf_getrela(Elf_Data *ed, int ndx, GElf_Rela *dst) @@ -71,8 +70,9 @@ gelf_getrela(Elf_Data *ed, int ndx, GElf_Rela *dst) msz = _libelf_msize(ELF_T_RELA, ec, e->e_version); assert(msz > 0); + assert(ndx >= 0); - if (msz * ndx >= d->d_data.d_size) { + if (msz * (size_t) ndx >= d->d_data.d_size) { LIBELF_SET_ERROR(ARGUMENT, 0); return (NULL); } @@ -131,9 +131,11 @@ gelf_update_rela(Elf_Data *ed, int ndx, GElf_Rela *dr) } msz = _libelf_msize(ELF_T_RELA, ec, e->e_version); + assert(msz > 0); + assert(ndx >= 0); - if (msz * ndx >= d->d_data.d_size) { + if (msz * (size_t) ndx >= d->d_data.d_size) { LIBELF_SET_ERROR(ARGUMENT, 0); return (0); } @@ -148,8 +150,9 @@ gelf_update_rela(Elf_Data *ed, int ndx, GElf_Rela *dr) LIBELF_SET_ERROR(RANGE, 0); return (0); } - rela32->r_info = ELF32_R_INFO(ELF64_R_SYM(dr->r_info), - ELF64_R_TYPE(dr->r_info)); + rela32->r_info = ELF32_R_INFO( + (Elf32_Word) ELF64_R_SYM(dr->r_info), + (Elf32_Word) ELF64_R_TYPE(dr->r_info)); LIBELF_COPY_S32(rela32, dr, r_addend); } else { diff --git a/contrib/elftoolchain/libelf/gelf_shdr.c b/contrib/elftoolchain/libelf/gelf_shdr.c index d4024895c602..1a9d949e42eb 100644 --- a/contrib/elftoolchain/libelf/gelf_shdr.c +++ b/contrib/elftoolchain/libelf/gelf_shdr.c @@ -24,16 +24,15 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> - #include <assert.h> #include <gelf.h> #include <libelf.h> #include <limits.h> +#include <stdint.h> #include "_libelf.h" -ELFTC_VCSID("$Id: gelf_shdr.c 2268 2011-12-03 17:05:11Z jkoshy $"); +ELFTC_VCSID("$Id: gelf_shdr.c 3177 2015-03-30 18:19:41Z emaste $"); Elf32_Shdr * elf32_getshdr(Elf_Scn *s) diff --git a/contrib/elftoolchain/libelf/gelf_sym.c b/contrib/elftoolchain/libelf/gelf_sym.c index 4a490d9ee079..ecd85fceefb4 100644 --- a/contrib/elftoolchain/libelf/gelf_sym.c +++ b/contrib/elftoolchain/libelf/gelf_sym.c @@ -24,15 +24,14 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> - #include <assert.h> #include <gelf.h> #include <limits.h> +#include <stdint.h> #include "_libelf.h" -ELFTC_VCSID("$Id: gelf_sym.c 2272 2011-12-03 17:07:31Z jkoshy $"); +ELFTC_VCSID("$Id: gelf_sym.c 3177 2015-03-30 18:19:41Z emaste $"); GElf_Sym * gelf_getsym(Elf_Data *ed, int ndx, GElf_Sym *dst) @@ -71,25 +70,23 @@ gelf_getsym(Elf_Data *ed, int ndx, GElf_Sym *dst) msz = _libelf_msize(ELF_T_SYM, ec, e->e_version); assert(msz > 0); + assert(ndx >= 0); - if (msz * ndx >= d->d_data.d_size) { + if (msz * (size_t) ndx >= d->d_data.d_size) { LIBELF_SET_ERROR(ARGUMENT, 0); return (NULL); } if (ec == ELFCLASS32) { - sym32 = (Elf32_Sym *) d->d_data.d_buf + ndx; dst->st_name = sym32->st_name; dst->st_value = (Elf64_Addr) sym32->st_value; dst->st_size = (Elf64_Xword) sym32->st_size; - dst->st_info = ELF64_ST_INFO(ELF32_ST_BIND(sym32->st_info), - ELF32_ST_TYPE(sym32->st_info)); + dst->st_info = sym32->st_info; dst->st_other = sym32->st_other; dst->st_shndx = sym32->st_shndx; } else { - sym64 = (Elf64_Sym *) d->d_data.d_buf + ndx; *dst = *sym64; @@ -133,9 +130,11 @@ gelf_update_sym(Elf_Data *ed, int ndx, GElf_Sym *gs) } msz = _libelf_msize(ELF_T_SYM, ec, e->e_version); + assert(msz > 0); + assert(ndx >= 0); - if (msz * ndx >= d->d_data.d_size) { + if (msz * (size_t) ndx >= d->d_data.d_size) { LIBELF_SET_ERROR(ARGUMENT, 0); return (0); } diff --git a/contrib/elftoolchain/libelf/gelf_syminfo.c b/contrib/elftoolchain/libelf/gelf_syminfo.c index bb2063d9a2d4..f6d7b6c367fa 100644 --- a/contrib/elftoolchain/libelf/gelf_syminfo.c +++ b/contrib/elftoolchain/libelf/gelf_syminfo.c @@ -24,14 +24,12 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> - #include <assert.h> #include <gelf.h> #include "_libelf.h" -ELFTC_VCSID("$Id: gelf_syminfo.c 2272 2011-12-03 17:07:31Z jkoshy $"); +ELFTC_VCSID("$Id: gelf_syminfo.c 3174 2015-03-27 17:13:41Z emaste $"); GElf_Syminfo * gelf_getsyminfo(Elf_Data *ed, int ndx, GElf_Syminfo *dst) @@ -70,8 +68,9 @@ gelf_getsyminfo(Elf_Data *ed, int ndx, GElf_Syminfo *dst) msz = _libelf_msize(ELF_T_SYMINFO, ec, e->e_version); assert(msz > 0); + assert(ndx >= 0); - if (msz * ndx >= d->d_data.d_size) { + if (msz * (size_t) ndx >= d->d_data.d_size) { LIBELF_SET_ERROR(ARGUMENT, 0); return (NULL); } @@ -128,9 +127,11 @@ gelf_update_syminfo(Elf_Data *ed, int ndx, GElf_Syminfo *gs) } msz = _libelf_msize(ELF_T_SYMINFO, ec, e->e_version); + assert(msz > 0); + assert(ndx >= 0); - if (msz * ndx >= d->d_data.d_size) { + if (msz * (size_t) ndx >= d->d_data.d_size) { LIBELF_SET_ERROR(ARGUMENT, 0); return (0); } diff --git a/contrib/elftoolchain/libelf/gelf_symshndx.c b/contrib/elftoolchain/libelf/gelf_symshndx.c index 9cf3b7578183..b490aa2bb11f 100644 --- a/contrib/elftoolchain/libelf/gelf_symshndx.c +++ b/contrib/elftoolchain/libelf/gelf_symshndx.c @@ -24,14 +24,12 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> - #include <assert.h> #include <gelf.h> #include "_libelf.h" -ELFTC_VCSID("$Id: gelf_symshndx.c 2283 2011-12-04 04:07:24Z jkoshy $"); +ELFTC_VCSID("$Id: gelf_symshndx.c 3174 2015-03-27 17:13:41Z emaste $"); GElf_Sym * gelf_getsymshndx(Elf_Data *d, Elf_Data *id, int ndx, GElf_Sym *dst, @@ -74,8 +72,9 @@ gelf_getsymshndx(Elf_Data *d, Elf_Data *id, int ndx, GElf_Sym *dst, msz = _libelf_msize(ELF_T_WORD, ec, e->e_version); assert(msz > 0); + assert(ndx >= 0); - if (msz * ndx >= id->d_size) { + if (msz * (size_t) ndx >= id->d_size) { LIBELF_SET_ERROR(ARGUMENT, 0); return (NULL); } @@ -123,9 +122,11 @@ gelf_update_symshndx(Elf_Data *d, Elf_Data *id, int ndx, GElf_Sym *gs, } msz = _libelf_msize(ELF_T_WORD, ec, e->e_version); + assert(msz > 0); + assert(ndx >= 0); - if (msz * ndx >= id->d_size) { + if (msz * (size_t) ndx >= id->d_size) { LIBELF_SET_ERROR(ARGUMENT, 0); return (0); } diff --git a/contrib/elftoolchain/libelf/gelf_xlate.c b/contrib/elftoolchain/libelf/gelf_xlate.c index 15f30eebc264..f50181925f21 100644 --- a/contrib/elftoolchain/libelf/gelf_xlate.c +++ b/contrib/elftoolchain/libelf/gelf_xlate.c @@ -24,15 +24,13 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> - #include <gelf.h> #include <libelf.h> #include <string.h> #include "_libelf.h" -ELFTC_VCSID("$Id: gelf_xlate.c 2225 2011-11-26 18:55:54Z jkoshy $"); +ELFTC_VCSID("$Id: gelf_xlate.c 3174 2015-03-27 17:13:41Z emaste $"); Elf_Data * elf32_xlatetof(Elf_Data *dst, const Elf_Data *src, unsigned int encoding) diff --git a/contrib/elftoolchain/libelf/libelf.h b/contrib/elftoolchain/libelf/libelf.h index cfe73502c5c9..151651d56473 100644 --- a/contrib/elftoolchain/libelf/libelf.h +++ b/contrib/elftoolchain/libelf/libelf.h @@ -23,7 +23,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: libelf.h 2366 2011-12-29 06:12:14Z jkoshy $ + * $Id: libelf.h 3174 2015-03-27 17:13:41Z emaste $ */ #ifndef _LIBELF_H_ @@ -129,7 +129,7 @@ typedef struct { /* * Members that are not part of the public API. */ - int ar_flags; + unsigned int ar_flags; } Elf_Arhdr; /* @@ -176,7 +176,9 @@ enum Elf_Error { #define ELF_F_ARCHIVE 0x100U /* archive creation */ #define ELF_F_ARCHIVE_SYSV 0x200U /* SYSV style archive */ -__BEGIN_DECLS +#ifdef __cplusplus +extern "C" { +#endif Elf *elf_begin(int _fd, Elf_Cmd _cmd, Elf *_elf); int elf_cntl(Elf *_elf, Elf_Cmd _cmd); int elf_end(Elf *_elf); @@ -247,6 +249,8 @@ Elf_Data *elf64_xlatetof(Elf_Data *_dst, const Elf_Data *_src, unsigned int _enc); Elf_Data *elf64_xlatetom(Elf_Data *_dst, const Elf_Data *_src, unsigned int _enc); -__END_DECLS +#ifdef __cplusplus +} +#endif #endif /* _LIBELF_H_ */ diff --git a/contrib/elftoolchain/libelf/libelf_align.c b/contrib/elftoolchain/libelf/libelf_align.c index 58a2adf0386e..ea6a601e3bb0 100644 --- a/contrib/elftoolchain/libelf/libelf_align.c +++ b/contrib/elftoolchain/libelf/libelf_align.c @@ -24,19 +24,17 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> - #include <sys/types.h> #include <libelf.h> #include "_libelf.h" -ELFTC_VCSID("$Id: libelf_align.c 2225 2011-11-26 18:55:54Z jkoshy $"); +ELFTC_VCSID("$Id: libelf_align.c 3174 2015-03-27 17:13:41Z emaste $"); struct align { - int a32; - int a64; + unsigned int a32; + unsigned int a64; }; #ifdef __GNUC__ @@ -87,7 +85,7 @@ static struct align malign[ELF_T_NUM] = { [ELF_T_GNUHASH] = MALIGN_WORD() }; -int +unsigned int _libelf_malign(Elf_Type t, int elfclass) { if (t >= ELF_T_NUM || (int) t < 0) @@ -126,7 +124,7 @@ static struct align falign[ELF_T_NUM] = { [ELF_T_GNUHASH] = FALIGN(4,8) }; -int +unsigned int _libelf_falign(Elf_Type t, int elfclass) { if (t >= ELF_T_NUM || (int) t < 0) diff --git a/contrib/elftoolchain/libelf/libelf_allocate.c b/contrib/elftoolchain/libelf/libelf_allocate.c index 9a5df6bb495c..0a74facc9fc4 100644 --- a/contrib/elftoolchain/libelf/libelf_allocate.c +++ b/contrib/elftoolchain/libelf/libelf_allocate.c @@ -28,8 +28,6 @@ * Internal APIs */ -#include <sys/cdefs.h> - #include <assert.h> #include <errno.h> #include <libelf.h> @@ -38,7 +36,7 @@ #include "_libelf.h" -ELFTC_VCSID("$Id: libelf_allocate.c 2272 2011-12-03 17:07:31Z jkoshy $"); +ELFTC_VCSID("$Id: libelf_allocate.c 3174 2015-03-27 17:13:41Z emaste $"); Elf * _libelf_allocate_elf(void) diff --git a/contrib/elftoolchain/libelf/libelf_ar.c b/contrib/elftoolchain/libelf/libelf_ar.c index c990d6dc971a..faeec2c60765 100644 --- a/contrib/elftoolchain/libelf/libelf_ar.c +++ b/contrib/elftoolchain/libelf/libelf_ar.c @@ -24,8 +24,6 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> - #include <assert.h> #include <ctype.h> #include <libelf.h> @@ -35,7 +33,7 @@ #include "_libelf.h" #include "_libelf_ar.h" -ELFTC_VCSID("$Id: libelf_ar.c 2225 2011-11-26 18:55:54Z jkoshy $"); +ELFTC_VCSID("$Id: libelf_ar.c 3174 2015-03-27 17:13:41Z emaste $"); #define LIBELF_NALLOC_SIZE 16 @@ -110,8 +108,8 @@ Elf_Arhdr * _libelf_ar_gethdr(Elf *e) { Elf *parent; - char *namelen; Elf_Arhdr *eh; + char *namelen; size_t n, nlen; struct ar_hdr *arh; @@ -192,7 +190,7 @@ _libelf_ar_gethdr(Elf *e) } e->e_flags &= ~LIBELF_F_AR_HEADER; - e->e_hdr.e_rawhdr = (char *) arh; + e->e_hdr.e_rawhdr = (unsigned char *) arh; return (NULL); } @@ -201,10 +199,10 @@ Elf * _libelf_ar_open_member(int fd, Elf_Cmd c, Elf *elf) { Elf *e; - char *member, *namelen; - size_t nsz, sz; off_t next; + size_t nsz, sz; struct ar_hdr *arh; + char *member, *namelen; assert(elf->e_kind == ELF_K_AR); @@ -249,12 +247,12 @@ _libelf_ar_open_member(int fd, Elf_Cmd c, Elf *elf) member = (char *) (arh + 1); - if ((e = elf_memory((char *) member, sz)) == NULL) + if ((e = elf_memory(member, sz)) == NULL) return (NULL); e->e_fd = fd; e->e_cmd = c; - e->e_hdr.e_rawhdr = (char *) arh; + e->e_hdr.e_rawhdr = (unsigned char *) arh; elf->e_u.e_ar.e_nchildren++; e->e_parent = elf; @@ -274,9 +272,10 @@ _libelf_ar_open_member(int fd, Elf_Cmd c, Elf *elf) */ /* - * A helper macro to read in a 'long' value from the archive. We use - * memcpy() since the source pointer may be misaligned with respect to - * the natural alignment for a C 'long'. + * A helper macro to read in a 'long' value from the archive. + * + * We use memcpy() since the source pointer may be misaligned with + * respect to the natural alignment for a C 'long'. */ #define GET_LONG(P, V)do { \ memcpy(&(V), (P), sizeof(long)); \ @@ -287,9 +286,10 @@ Elf_Arsym * _libelf_ar_process_bsd_symtab(Elf *e, size_t *count) { Elf_Arsym *symtab, *sym; + unsigned int n, nentries; unsigned char *end, *p, *p0, *s, *s0; - const unsigned int entrysize = 2 * sizeof(long); - long arraysize, fileoffset, n, nentries, stroffset, strtabsize; + const size_t entrysize = 2 * sizeof(long); + long arraysize, fileoffset, stroffset, strtabsize; assert(e != NULL); assert(count != NULL); @@ -313,7 +313,8 @@ _libelf_ar_process_bsd_symtab(Elf *e, size_t *count) */ GET_LONG(p, arraysize); - if (p0 + arraysize >= end || (arraysize % entrysize != 0)) + if (arraysize < 0 || p0 + arraysize >= end || + ((size_t) arraysize % entrysize != 0)) goto symtaberror; /* @@ -323,10 +324,10 @@ _libelf_ar_process_bsd_symtab(Elf *e, size_t *count) GET_LONG(s, strtabsize); s0 = s; /* Start of string table. */ - if (s0 + strtabsize > end) + if (strtabsize < 0 || s0 + strtabsize > end) goto symtaberror; - nentries = arraysize / entrysize; + nentries = (size_t) arraysize / entrysize; /* * Allocate space for the returned Elf_Arsym array. @@ -341,12 +342,16 @@ _libelf_ar_process_bsd_symtab(Elf *e, size_t *count) GET_LONG(p, stroffset); GET_LONG(p, fileoffset); + if (stroffset < 0 || fileoffset < 0 || + (size_t) fileoffset >= e->e_rawsize) + goto symtaberror; + s = s0 + stroffset; if (s >= end) goto symtaberror; - sym->as_off = fileoffset; + sym->as_off = (off_t) fileoffset; sym->as_hash = elf_hash((char *) s); sym->as_name = (char *) s; } @@ -393,7 +398,8 @@ symtaberror: Elf_Arsym * _libelf_ar_process_svr4_symtab(Elf *e, size_t *count) { - size_t n, nentries, off; + uint32_t off; + size_t n, nentries; Elf_Arsym *symtab, *sym; unsigned char *p, *s, *end; @@ -424,15 +430,14 @@ _libelf_ar_process_svr4_symtab(Elf *e, size_t *count) s = p + (nentries * INTSZ); /* start of the string table. */ for (n = nentries, sym = symtab; n > 0; n--) { - if (s >= end) goto symtaberror; - off = 0; - GET_WORD(p, off); + if (off >= e->e_rawsize) + goto symtaberror; - sym->as_off = off; + sym->as_off = (off_t) off; sym->as_hash = elf_hash((char *) s); sym->as_name = (char *) s; diff --git a/contrib/elftoolchain/libelf/libelf_ar_util.c b/contrib/elftoolchain/libelf/libelf_ar_util.c index 958fbbf49022..239612e7915a 100644 --- a/contrib/elftoolchain/libelf/libelf_ar_util.c +++ b/contrib/elftoolchain/libelf/libelf_ar_util.c @@ -24,8 +24,6 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> - #include <assert.h> #include <libelf.h> #include <stdlib.h> @@ -34,21 +32,23 @@ #include "_libelf.h" #include "_libelf_ar.h" -ELFTC_VCSID("$Id: libelf_ar_util.c 2365 2011-12-29 04:36:44Z jkoshy $"); +ELFTC_VCSID("$Id: libelf_ar_util.c 3174 2015-03-27 17:13:41Z emaste $"); /* * Convert a string bounded by `start' and `start+sz' (exclusive) to a * number in the specified base. */ int -_libelf_ar_get_number(const char *s, size_t sz, int base, size_t *ret) +_libelf_ar_get_number(const char *src, size_t sz, unsigned int base, + size_t *ret) { - int c, v; size_t r; - const char *e; + unsigned int c, v; + const unsigned char *e, *s; assert(base <= 10); + s = (const unsigned char *) src; e = s + sz; /* skip leading blanks */ @@ -79,17 +79,18 @@ _libelf_ar_get_number(const char *s, size_t sz, int base, size_t *ret) char * _libelf_ar_get_translated_name(const struct ar_hdr *arh, Elf *ar) { - char c, *s; + char *s; + unsigned char c; size_t len, offset; - const char *buf, *p, *q, *r; + const unsigned char *buf, *p, *q, *r; const size_t bufsize = sizeof(arh->ar_name); assert(arh != NULL); assert(ar->e_kind == ELF_K_AR); - assert((const char *) arh >= ar->e_rawfile && - (const char *) arh < ar->e_rawfile + ar->e_rawsize); + assert((const unsigned char *) arh >= ar->e_rawfile && + (const unsigned char *) arh < ar->e_rawfile + ar->e_rawsize); - buf = arh->ar_name; + buf = (const unsigned char *) arh->ar_name; /* * Check for extended naming. @@ -104,8 +105,8 @@ _libelf_ar_get_translated_name(const struct ar_hdr *arh, Elf *ar) * the archive string table where the actual name * resides. */ - if (_libelf_ar_get_number(buf + 1, bufsize - 1, 10, - &offset) == 0) { + if (_libelf_ar_get_number((const char *) (buf + 1), + bufsize - 1, 10, &offset) == 0) { LIBELF_SET_ERROR(ARCHIVE, 0); return (NULL); } @@ -120,21 +121,21 @@ _libelf_ar_get_translated_name(const struct ar_hdr *arh, Elf *ar) for (; p < r && *p != '/'; p++) ; - len = p - q + 1; /* space for the trailing NUL */ + len = (size_t) (p - q + 1); /* space for the trailing NUL */ if ((s = malloc(len)) == NULL) { LIBELF_SET_ERROR(RESOURCE, 0); return (NULL); } - (void) strncpy(s, q, len - 1); + (void) strncpy(s, (const char *) q, len - 1); s[len - 1] = '\0'; return (s); } else if (IS_EXTENDED_BSD_NAME(buf)) { r = buf + LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE; - if (_libelf_ar_get_number(r, bufsize - + if (_libelf_ar_get_number((const char *) r, bufsize - LIBELF_AR_BSD_EXTENDED_NAME_PREFIX_SIZE, 10, &len) == 0) { LIBELF_SET_ERROR(ARCHIVE, 0); @@ -153,9 +154,9 @@ _libelf_ar_get_translated_name(const struct ar_hdr *arh, Elf *ar) /* * The file name follows the archive header. */ - q = (const char *) (arh + 1); + q = (const unsigned char *) (arh + 1); - (void) strncpy(s, q, len); + (void) strncpy(s, (const char *) q, len); s[len] = '\0'; return (s); @@ -183,10 +184,10 @@ _libelf_ar_get_translated_name(const struct ar_hdr *arh, Elf *ar) q--; } - len = q - buf + 2; /* Add space for a trailing NUL. */ + len = (size_t) (q - buf + 2); /* Space for a trailing NUL. */ } else { /* The buffer only had blanks. */ - buf = ""; + buf = (const unsigned char *) ""; len = 1; } @@ -195,7 +196,7 @@ _libelf_ar_get_translated_name(const struct ar_hdr *arh, Elf *ar) return (NULL); } - (void) strncpy(s, buf, len - 1); + (void) strncpy(s, (const char *) buf, len - 1); s[len - 1] = '\0'; return (s); @@ -229,8 +230,8 @@ _libelf_ar_open(Elf *e, int reporterror) { size_t sz; int scanahead; - char *s, *end; struct ar_hdr arh; + unsigned char *s, *end; _libelf_init_elf(e, ELF_K_AR); @@ -264,7 +265,7 @@ _libelf_ar_open(Elf *e, int reporterror) (void) memcpy(&(ARH), (S), sizeof((ARH))); \ if ((ARH).ar_fmag[0] != '`' || (ARH).ar_fmag[1] != '\n') \ goto error; \ - if (_libelf_ar_get_number((ARH).ar_size, \ + if (_libelf_ar_get_number((char *) (ARH).ar_size, \ sizeof((ARH).ar_size), 10, &(SZ)) == 0) \ goto error; \ } while (0) @@ -275,8 +276,8 @@ _libelf_ar_open(Elf *e, int reporterror) * Handle special archive members for the SVR4 format. */ if (arh.ar_name[0] == '/') { - - assert(sz > 0); + if (sz == 0) + goto error; e->e_flags |= LIBELF_F_AR_VARIANT_SVR4; diff --git a/contrib/elftoolchain/libelf/libelf_checksum.c b/contrib/elftoolchain/libelf/libelf_checksum.c index 8f84aa4d0f05..ef868773105e 100644 --- a/contrib/elftoolchain/libelf/libelf_checksum.c +++ b/contrib/elftoolchain/libelf/libelf_checksum.c @@ -24,13 +24,11 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> - #include <gelf.h> #include "_libelf.h" -ELFTC_VCSID("$Id: libelf_checksum.c 2225 2011-11-26 18:55:54Z jkoshy $"); +ELFTC_VCSID("$Id: libelf_checksum.c 3174 2015-03-27 17:13:41Z emaste $"); static unsigned long _libelf_sum(unsigned long c, const unsigned char *s, size_t size) @@ -44,7 +42,7 @@ _libelf_sum(unsigned long c, const unsigned char *s, size_t size) return (c); } -unsigned long +long _libelf_checksum(Elf *e, int elfclass) { size_t shn; @@ -90,11 +88,11 @@ _libelf_checksum(Elf *e, int elfclass) d = NULL; while ((d = elf_rawdata(scn, d)) != NULL) checksum = _libelf_sum(checksum, - (unsigned char *) d->d_buf, d->d_size); + (unsigned char *) d->d_buf, (size_t) d->d_size); } /* * Return a 16-bit checksum compatible with Solaris. */ - return (((checksum >> 16) & 0xFFFFUL) + (checksum & 0xFFFFUL)); + return (long) (((checksum >> 16) & 0xFFFFUL) + (checksum & 0xFFFFUL)); } diff --git a/contrib/elftoolchain/libelf/libelf_convert.m4 b/contrib/elftoolchain/libelf/libelf_convert.m4 index fda9392a4d8f..9f99f1ead866 100644 --- a/contrib/elftoolchain/libelf/libelf_convert.m4 +++ b/contrib/elftoolchain/libelf/libelf_convert.m4 @@ -24,15 +24,13 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> - #include <assert.h> #include <libelf.h> #include <string.h> #include "_libelf.h" -ELFTC_VCSID("$Id: libelf_convert.m4 2361 2011-12-28 12:03:05Z jkoshy $"); +ELFTC_VCSID("$Id: libelf_convert.m4 3174 2015-03-27 17:13:41Z emaste $"); /* WARNING: GENERATED FROM __file__. */ @@ -143,8 +141,8 @@ define(`SIZEDEP_OFF', 1) # Generates a pair of conversion functions. define(`MAKEPRIMFUNCS',` static int -_libelf_cvt_$1$4_tof(char *dst, size_t dsz, char *src, size_t count, - int byteswap) +_libelf_cvt_$1$4_tof(unsigned char *dst, size_t dsz, unsigned char *src, + size_t count, int byteswap) { Elf$3_$2 t, *s = (Elf$3_$2 *) (uintptr_t) src; size_t c; @@ -166,8 +164,8 @@ _libelf_cvt_$1$4_tof(char *dst, size_t dsz, char *src, size_t count, } static int -_libelf_cvt_$1$4_tom(char *dst, size_t dsz, char *src, size_t count, - int byteswap) +_libelf_cvt_$1$4_tom(unsigned char *dst, size_t dsz, unsigned char *src, + size_t count, int byteswap) { Elf$3_$2 t, *d = (Elf$3_$2 *) (uintptr_t) dst; size_t c; @@ -267,8 +265,8 @@ define(`READ_STRUCT', # `$3': ELF class specifier, one of [`', `32', `64'] define(`MAKECOMPFUNCS', `ifdef(`NOFUNC_'$1$3,`',` static int -_libelf_cvt_$1$3_tof(char *dst, size_t dsz, char *src, size_t count, - int byteswap) +_libelf_cvt_$1$3_tof(unsigned char *dst, size_t dsz, unsigned char *src, + size_t count, int byteswap) { Elf$3_$2 t, *s; size_t c; @@ -288,16 +286,16 @@ _libelf_cvt_$1$3_tof(char *dst, size_t dsz, char *src, size_t count, } static int -_libelf_cvt_$1$3_tom(char *dst, size_t dsz, char *src, size_t count, - int byteswap) +_libelf_cvt_$1$3_tom(unsigned char *dst, size_t dsz, unsigned char *src, + size_t count, int byteswap) { - Elf$3_$2 t, *d; - char *s,*s0; + Elf$3_$2 t, *d; + unsigned char *s,*s0; size_t fsz; fsz = elf$3_fsize(ELF_T_$1, (size_t) 1, EV_CURRENT); d = ((Elf$3_$2 *) (uintptr_t) dst) + (count - 1); - s0 = (char *) src + (count - 1) * fsz; + s0 = src + (count - 1) * fsz; if (dsz < count * sizeof(Elf$3_$2)) return (0); @@ -398,8 +396,8 @@ define(`MAKE_VERSION_CONVERTERS', # conversion function. define(`MAKE_VERSION_CONVERTER',` static int -_libelf_cvt_$1$5_tof(char *dst, size_t dsz, char *src, size_t count, - int byteswap) +_libelf_cvt_$1$5_tof(unsigned char *dst, size_t dsz, unsigned char *src, + size_t count, int byteswap) { Elf$5_$2 t; Elf$5_$3 a; @@ -407,12 +405,12 @@ _libelf_cvt_$1$5_tof(char *dst, size_t dsz, char *src, size_t count, const size_t auxfsz = FSZ(Elf$5_$3_DEF); const size_t vermsz = sizeof(Elf$5_$2); const size_t auxmsz = sizeof(Elf$5_$3); - char * const dstend = dst + dsz; - char * const srcend = src + count; - char *dtmp, *dstaux, *srcaux; + unsigned char * const dstend = dst + dsz; + unsigned char * const srcend = src + count; + unsigned char *dtmp, *dstaux, *srcaux; Elf$5_Word aux, anext, cnt, vnext; - for (dtmp = dst, vnext = ~0; + for (dtmp = dst, vnext = ~0U; vnext != 0 && dtmp + verfsz <= dstend && src + vermsz <= srcend; dtmp += vnext, src += vnext) { @@ -434,7 +432,7 @@ _libelf_cvt_$1$5_tof(char *dst, size_t dsz, char *src, size_t count, return (0); /* Process AUX entries. */ - for (anext = ~0, dstaux = dtmp + aux, srcaux = src + aux; + for (anext = ~0U, dstaux = dtmp + aux, srcaux = src + aux; cnt != 0 && anext != 0 && dstaux + auxfsz <= dstend && srcaux + auxmsz <= srcend; dstaux += anext, srcaux += anext, cnt--) { @@ -462,8 +460,8 @@ _libelf_cvt_$1$5_tof(char *dst, size_t dsz, char *src, size_t count, } static int -_libelf_cvt_$1$5_tom(char *dst, size_t dsz, char *src, size_t count, - int byteswap) +_libelf_cvt_$1$5_tom(unsigned char *dst, size_t dsz, unsigned char *src, + size_t count, int byteswap) { Elf$5_$2 t, *dp; Elf$5_$3 a, *ap; @@ -471,12 +469,12 @@ _libelf_cvt_$1$5_tom(char *dst, size_t dsz, char *src, size_t count, const size_t auxfsz = FSZ(Elf$5_$3_DEF); const size_t vermsz = sizeof(Elf$5_$2); const size_t auxmsz = sizeof(Elf$5_$3); - char * const dstend = dst + dsz; - char * const srcend = src + count; - char *dstaux, *s, *srcaux, *stmp; + unsigned char * const dstend = dst + dsz; + unsigned char * const srcend = src + count; + unsigned char *dstaux, *s, *srcaux, *stmp; Elf$5_Word aux, anext, cnt, vnext; - for (stmp = src, vnext = ~0; + for (stmp = src, vnext = ~0U; vnext != 0 && stmp + verfsz <= srcend && dst + vermsz <= dstend; stmp += vnext, dst += vnext) { @@ -498,7 +496,7 @@ _libelf_cvt_$1$5_tom(char *dst, size_t dsz, char *src, size_t count, return (0); /* Process AUX entries. */ - for (anext = ~0, dstaux = dst + aux, srcaux = stmp + aux; + for (anext = ~0U, dstaux = dst + aux, srcaux = stmp + aux; cnt != 0 && anext != 0 && dstaux + auxmsz <= dstend && srcaux + auxfsz <= srcend; dstaux += anext, srcaux += anext, cnt--) { @@ -536,22 +534,23 @@ divert(0) #define SWAP_IDENT(X) do { (void) (X); } while (0) #define SWAP_HALF(X) do { \ uint16_t _x = (uint16_t) (X); \ - uint16_t _t = _x & 0xFF; \ - _t <<= 8; _x >>= 8; _t |= _x & 0xFF; \ - (X) = _t; \ + uint32_t _t = _x & 0xFFU; \ + _t <<= 8U; _x >>= 8U; _t |= _x & 0xFFU; \ + (X) = (uint16_t) _t; \ } while (0) -#define SWAP_WORD(X) do { \ +#define _SWAP_WORD(X, T) do { \ uint32_t _x = (uint32_t) (X); \ uint32_t _t = _x & 0xFF; \ _t <<= 8; _x >>= 8; _t |= _x & 0xFF; \ _t <<= 8; _x >>= 8; _t |= _x & 0xFF; \ _t <<= 8; _x >>= 8; _t |= _x & 0xFF; \ - (X) = _t; \ + (X) = (T) _t; \ } while (0) -#define SWAP_ADDR32(X) SWAP_WORD(X) -#define SWAP_OFF32(X) SWAP_WORD(X) -#define SWAP_SWORD(X) SWAP_WORD(X) -#define SWAP_WORD64(X) do { \ +#define SWAP_ADDR32(X) _SWAP_WORD(X, Elf32_Addr) +#define SWAP_OFF32(X) _SWAP_WORD(X, Elf32_Off) +#define SWAP_SWORD(X) _SWAP_WORD(X, Elf32_Sword) +#define SWAP_WORD(X) _SWAP_WORD(X, Elf32_Word) +#define _SWAP_WORD64(X, T) do { \ uint64_t _x = (uint64_t) (X); \ uint64_t _t = _x & 0xFF; \ _t <<= 8; _x >>= 8; _t |= _x & 0xFF; \ @@ -561,13 +560,13 @@ divert(0) _t <<= 8; _x >>= 8; _t |= _x & 0xFF; \ _t <<= 8; _x >>= 8; _t |= _x & 0xFF; \ _t <<= 8; _x >>= 8; _t |= _x & 0xFF; \ - (X) = _t; \ + (X) = (T) _t; \ } while (0) -#define SWAP_ADDR64(X) SWAP_WORD64(X) -#define SWAP_LWORD(X) SWAP_WORD64(X) -#define SWAP_OFF64(X) SWAP_WORD64(X) -#define SWAP_SXWORD(X) SWAP_WORD64(X) -#define SWAP_XWORD(X) SWAP_WORD64(X) +#define SWAP_ADDR64(X) _SWAP_WORD64(X, Elf64_Addr) +#define SWAP_LWORD(X) _SWAP_WORD64(X, Elf64_Lword) +#define SWAP_OFF64(X) _SWAP_WORD64(X, Elf64_Off) +#define SWAP_SXWORD(X) _SWAP_WORD64(X, Elf64_Sxword) +#define SWAP_XWORD(X) _SWAP_WORD64(X, Elf64_Xword) /* * C macros to write out various integral values. @@ -578,22 +577,22 @@ divert(0) * - The destination pointer is incremented after the write. */ #define WRITE_BYTE(P,X) do { \ - char *const _p = (char *) (P); \ - _p[0] = (char) (X); \ + unsigned char *const _p = (unsigned char *) (P); \ + _p[0] = (unsigned char) (X); \ (P) = _p + 1; \ } while (0) #define WRITE_HALF(P,X) do { \ uint16_t _t = (X); \ - char *const _p = (char *) (P); \ - const char *const _q = (char *) &_t; \ + unsigned char *const _p = (unsigned char *) (P); \ + const unsigned char *const _q = (unsigned char *) &_t; \ _p[0] = _q[0]; \ _p[1] = _q[1]; \ (P) = _p + 2; \ } while (0) -#define WRITE_WORD(P,X) do { \ - uint32_t _t = (X); \ - char *const _p = (char *) (P); \ - const char *const _q = (char *) &_t; \ +#define WRITE_WORD(P,X) do { \ + uint32_t _t = (uint32_t) (X); \ + unsigned char *const _p = (unsigned char *) (P); \ + const unsigned char *const _q = (unsigned char *) &_t; \ _p[0] = _q[0]; \ _p[1] = _q[1]; \ _p[2] = _q[2]; \ @@ -604,9 +603,9 @@ divert(0) #define WRITE_OFF32(P,X) WRITE_WORD(P,X) #define WRITE_SWORD(P,X) WRITE_WORD(P,X) #define WRITE_WORD64(P,X) do { \ - uint64_t _t = (X); \ - char *const _p = (char *) (P); \ - const char *const _q = (char *) &_t; \ + uint64_t _t = (uint64_t) (X); \ + unsigned char *const _p = (unsigned char *) (P); \ + const unsigned char *const _q = (unsigned char *) &_t; \ _p[0] = _q[0]; \ _p[1] = _q[1]; \ _p[2] = _q[2]; \ @@ -637,41 +636,42 @@ divert(0) */ #define READ_BYTE(P,X) do { \ - const char *const _p = \ - (const char *) (P); \ + const unsigned char *const _p = \ + (const unsigned char *) (P); \ (X) = _p[0]; \ (P) = (P) + 1; \ } while (0) #define READ_HALF(P,X) do { \ uint16_t _t; \ - char *const _q = (char *) &_t; \ - const char *const _p = \ - (const char *) (P); \ + unsigned char *const _q = (unsigned char *) &_t; \ + const unsigned char *const _p = \ + (const unsigned char *) (P); \ _q[0] = _p[0]; \ _q[1] = _p[1]; \ (P) = (P) + 2; \ (X) = _t; \ } while (0) -#define READ_WORD(P,X) do { \ +#define _READ_WORD(P,X,T) do { \ uint32_t _t; \ - char *const _q = (char *) &_t; \ - const char *const _p = \ - (const char *) (P); \ + unsigned char *const _q = (unsigned char *) &_t; \ + const unsigned char *const _p = \ + (const unsigned char *) (P); \ _q[0] = _p[0]; \ _q[1] = _p[1]; \ _q[2] = _p[2]; \ _q[3] = _p[3]; \ (P) = (P) + 4; \ - (X) = _t; \ + (X) = (T) _t; \ } while (0) -#define READ_ADDR32(P,X) READ_WORD(P,X) -#define READ_OFF32(P,X) READ_WORD(P,X) -#define READ_SWORD(P,X) READ_WORD(P,X) -#define READ_WORD64(P,X) do { \ +#define READ_ADDR32(P,X) _READ_WORD(P, X, Elf32_Addr) +#define READ_OFF32(P,X) _READ_WORD(P, X, Elf32_Off) +#define READ_SWORD(P,X) _READ_WORD(P, X, Elf32_Sword) +#define READ_WORD(P,X) _READ_WORD(P, X, Elf32_Word) +#define _READ_WORD64(P,X,T) do { \ uint64_t _t; \ - char *const _q = (char *) &_t; \ - const char *const _p = \ - (const char *) (P); \ + unsigned char *const _q = (unsigned char *) &_t; \ + const unsigned char *const _p = \ + (const unsigned char *) (P); \ _q[0] = _p[0]; \ _q[1] = _p[1]; \ _q[2] = _p[2]; \ @@ -681,13 +681,13 @@ divert(0) _q[6] = _p[6]; \ _q[7] = _p[7]; \ (P) = (P) + 8; \ - (X) = _t; \ + (X) = (T) _t; \ } while (0) -#define READ_ADDR64(P,X) READ_WORD64(P,X) -#define READ_LWORD(P,X) READ_WORD64(P,X) -#define READ_OFF64(P,X) READ_WORD64(P,X) -#define READ_SXWORD(P,X) READ_WORD64(P,X) -#define READ_XWORD(P,X) READ_WORD64(P,X) +#define READ_ADDR64(P,X) _READ_WORD64(P, X, Elf64_Addr) +#define READ_LWORD(P,X) _READ_WORD64(P, X, Elf64_Lword) +#define READ_OFF64(P,X) _READ_WORD64(P, X, Elf64_Off) +#define READ_SXWORD(P,X) _READ_WORD64(P, X, Elf64_Sxword) +#define READ_XWORD(P,X) _READ_WORD64(P, X, Elf64_Xword) #define READ_IDENT(P,X) do { \ (void) memcpy((X), (P), sizeof((X))); \ (P) = (P) + EI_NIDENT; \ @@ -707,8 +707,8 @@ MAKE_VERSION_CONVERTERS(VNEED,Verneed,Vernaux,vn) */ static int -_libelf_cvt_BYTE_tox(char *dst, size_t dsz, char *src, size_t count, - int byteswap) +_libelf_cvt_BYTE_tox(unsigned char *dst, size_t dsz, unsigned char *src, + size_t count, int byteswap) { (void) byteswap; if (dsz < count) @@ -732,24 +732,24 @@ _libelf_cvt_BYTE_tox(char *dst, size_t dsz, char *src, size_t count, */ static int -_libelf_cvt_GNUHASH32_tom(char *dst, size_t dsz, char *src, size_t srcsz, - int byteswap) +_libelf_cvt_GNUHASH32_tom(unsigned char *dst, size_t dsz, unsigned char *src, + size_t srcsz, int byteswap) { return (_libelf_cvt_WORD_tom(dst, dsz, src, srcsz / sizeof(uint32_t), byteswap)); } static int -_libelf_cvt_GNUHASH32_tof(char *dst, size_t dsz, char *src, size_t srcsz, - int byteswap) +_libelf_cvt_GNUHASH32_tof(unsigned char *dst, size_t dsz, unsigned char *src, + size_t srcsz, int byteswap) { return (_libelf_cvt_WORD_tof(dst, dsz, src, srcsz / sizeof(uint32_t), byteswap)); } static int -_libelf_cvt_GNUHASH64_tom(char *dst, size_t dsz, char *src, size_t srcsz, - int byteswap) +_libelf_cvt_GNUHASH64_tom(unsigned char *dst, size_t dsz, unsigned char *src, + size_t srcsz, int byteswap) { size_t sz; uint64_t t64, *bloom64; @@ -834,8 +834,8 @@ _libelf_cvt_GNUHASH64_tom(char *dst, size_t dsz, char *src, size_t srcsz, } static int -_libelf_cvt_GNUHASH64_tof(char *dst, size_t dsz, char *src, size_t srcsz, - int byteswap) +_libelf_cvt_GNUHASH64_tof(unsigned char *dst, size_t dsz, unsigned char *src, + size_t srcsz, int byteswap) { uint32_t *s32; size_t sz, hdrsz; @@ -921,8 +921,8 @@ _libelf_cvt_GNUHASH64_tof(char *dst, size_t dsz, char *src, size_t srcsz, * The destination buffer needs to be at least `count' bytes in size. */ static int -_libelf_cvt_NOTE_tom(char *dst, size_t dsz, char *src, size_t count, - int byteswap) +_libelf_cvt_NOTE_tom(unsigned char *dst, size_t dsz, unsigned char *src, + size_t count, int byteswap) { uint32_t namesz, descsz, type; Elf_Note *en; @@ -947,11 +947,6 @@ _libelf_cvt_NOTE_tom(char *dst, size_t dsz, char *src, size_t count, READ_WORD(src, descsz); READ_WORD(src, type); - sz = namesz; - ROUNDUP2(sz, 4); - sz += descsz; - ROUNDUP2(sz, 4); - /* Translate. */ SWAP_WORD(namesz); SWAP_WORD(descsz); @@ -967,6 +962,11 @@ _libelf_cvt_NOTE_tom(char *dst, size_t dsz, char *src, size_t count, dst += sizeof(Elf_Note); count -= hdrsz; + ROUNDUP2(namesz, 4U); + ROUNDUP2(descsz, 4U); + + sz = namesz + descsz; + if (count < sz || dsz < sz) /* Buffers are too small. */ return (0); @@ -983,8 +983,8 @@ _libelf_cvt_NOTE_tom(char *dst, size_t dsz, char *src, size_t count, } static int -_libelf_cvt_NOTE_tof(char *dst, size_t dsz, char *src, size_t count, - int byteswap) +_libelf_cvt_NOTE_tof(unsigned char *dst, size_t dsz, unsigned char *src, + size_t count, int byteswap) { uint32_t namesz, descsz, type; Elf_Note *en; @@ -1005,6 +1005,11 @@ _libelf_cvt_NOTE_tof(char *dst, size_t dsz, char *src, size_t count, descsz = en->n_descsz; type = en->n_type; + sz = namesz; + ROUNDUP2(sz, 4U); + sz += descsz; + ROUNDUP2(sz, 4U); + SWAP_WORD(namesz); SWAP_WORD(descsz); SWAP_WORD(type); @@ -1015,11 +1020,6 @@ _libelf_cvt_NOTE_tof(char *dst, size_t dsz, char *src, size_t count, src += sizeof(Elf_Note); - ROUNDUP2(namesz, 4); - ROUNDUP2(descsz, 4); - - sz = namesz + descsz; - if (count < sz) sz = count; @@ -1034,14 +1034,14 @@ _libelf_cvt_NOTE_tof(char *dst, size_t dsz, char *src, size_t count, } struct converters { - int (*tof32)(char *dst, size_t dsz, char *src, size_t cnt, - int byteswap); - int (*tom32)(char *dst, size_t dsz, char *src, size_t cnt, - int byteswap); - int (*tof64)(char *dst, size_t dsz, char *src, size_t cnt, - int byteswap); - int (*tom64)(char *dst, size_t dsz, char *src, size_t cnt, - int byteswap); + int (*tof32)(unsigned char *dst, size_t dsz, unsigned char *src, + size_t cnt, int byteswap); + int (*tom32)(unsigned char *dst, size_t dsz, unsigned char *src, + size_t cnt, int byteswap); + int (*tof64)(unsigned char *dst, size_t dsz, unsigned char *src, + size_t cnt, int byteswap); + int (*tom64)(unsigned char *dst, size_t dsz, unsigned char *src, + size_t cnt, int byteswap); }; @@ -1070,7 +1070,8 @@ CONVERTER_NAMES(ELF_TYPE_LIST) }; int (*_libelf_get_translator(Elf_Type t, int direction, int elfclass)) - (char *_dst, size_t dsz, char *_src, size_t _cnt, int _byteswap) + (unsigned char *_dst, size_t dsz, unsigned char *_src, size_t _cnt, + int _byteswap) { assert(elfclass == ELFCLASS32 || elfclass == ELFCLASS64); assert(direction == ELF_TOFILE || direction == ELF_TOMEMORY); diff --git a/contrib/elftoolchain/libelf/libelf_data.c b/contrib/elftoolchain/libelf/libelf_data.c index 75775e2a2c0b..fcffd8bd9d1c 100644 --- a/contrib/elftoolchain/libelf/libelf_data.c +++ b/contrib/elftoolchain/libelf/libelf_data.c @@ -24,17 +24,18 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> - #include <libelf.h> #include "_libelf.h" -ELFTC_VCSID("$Id: libelf_data.c 2225 2011-11-26 18:55:54Z jkoshy $"); +ELFTC_VCSID("$Id: libelf_data.c 3174 2015-03-27 17:13:41Z emaste $"); int _libelf_xlate_shtype(uint32_t sht) { + /* + * Look for known section types. + */ switch (sht) { case SHT_DYNAMIC: return (ELF_T_DYN); @@ -82,18 +83,19 @@ _libelf_xlate_shtype(uint32_t sht) return (ELF_T_VNEED); case SHT_SUNW_versym: /* == SHT_GNU_versym */ return (ELF_T_HALF); - - case SHT_ARM_PREEMPTMAP: - case SHT_ARM_ATTRIBUTES: - case SHT_ARM_DEBUGOVERLAY: - case SHT_ARM_OVERLAYSECTION: - case SHT_MIPS_DWARF: - case SHT_MIPS_REGINFO: - case SHT_MIPS_OPTIONS: - case SHT_AMD64_UNWIND: /* == SHT_IA_64_UNWIND == SHT_ARM_EXIDX */ - return (ELF_T_BYTE); - default: + /* + * Values in the range [SHT_LOOS..SHT_HIUSER] (i.e., + * OS, processor and user-defined section types) are + * legal, but since we do not know anything more about + * their semantics, we return a type of ELF_T_BYTE. + */ + if (sht >= SHT_LOOS && sht <= SHT_HIUSER) + return (ELF_T_BYTE); + + /* + * Other values are unsupported. + */ return (-1); } } diff --git a/contrib/elftoolchain/libelf/libelf_ehdr.c b/contrib/elftoolchain/libelf/libelf_ehdr.c index 363a0a834b9f..d375fb30028c 100644 --- a/contrib/elftoolchain/libelf/libelf_ehdr.c +++ b/contrib/elftoolchain/libelf/libelf_ehdr.c @@ -24,8 +24,6 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> - #include <assert.h> #include <gelf.h> #include <libelf.h> @@ -33,7 +31,7 @@ #include "_libelf.h" -ELFTC_VCSID("$Id: libelf_ehdr.c 2225 2011-11-26 18:55:54Z jkoshy $"); +ELFTC_VCSID("$Id: libelf_ehdr.c 3174 2015-03-27 17:13:41Z emaste $"); /* * Retrieve counts for sections, phdrs and the section string table index @@ -45,7 +43,8 @@ _libelf_load_extended(Elf *e, int ec, uint64_t shoff, uint16_t phnum, { Elf_Scn *scn; size_t fsz; - int (*xlator)(char *_d, size_t _dsz, char *_s, size_t _c, int _swap); + int (*xlator)(unsigned char *_d, size_t _dsz, unsigned char *_s, + size_t _c, int _swap); uint32_t shtype; assert(STAILQ_EMPTY(&e->e_u.e_elf.e_scn)); @@ -62,8 +61,8 @@ _libelf_load_extended(Elf *e, int ec, uint64_t shoff, uint16_t phnum, return (0); xlator = _libelf_get_translator(ELF_T_SHDR, ELF_TOMEMORY, ec); - (*xlator)((char *) &scn->s_shdr, sizeof(scn->s_shdr), - e->e_rawfile + shoff, (size_t) 1, + (*xlator)((unsigned char *) &scn->s_shdr, sizeof(scn->s_shdr), + (unsigned char *) e->e_rawfile + shoff, (size_t) 1, e->e_byteorder != LIBELF_PRIVATE(byteorder)); #define GET_SHDR_MEMBER(M) ((ec == ELFCLASS32) ? scn->s_shdr.s_shdr32.M : \ @@ -74,7 +73,7 @@ _libelf_load_extended(Elf *e, int ec, uint64_t shoff, uint16_t phnum, return (0); } - e->e_u.e_elf.e_nscn = GET_SHDR_MEMBER(sh_size); + e->e_u.e_elf.e_nscn = (size_t) GET_SHDR_MEMBER(sh_size); e->e_u.e_elf.e_nphdr = (phnum != PN_XNUM) ? phnum : GET_SHDR_MEMBER(sh_info); e->e_u.e_elf.e_strndx = (strndx != SHN_XINDEX) ? strndx : @@ -92,7 +91,7 @@ _libelf_load_extended(Elf *e, int ec, uint64_t shoff, uint16_t phnum, eh->e_ident[EI_MAG3] = ELFMAG3; \ eh->e_ident[EI_CLASS] = ELFCLASS##SZ; \ eh->e_ident[EI_DATA] = ELFDATANONE; \ - eh->e_ident[EI_VERSION] = LIBELF_PRIVATE(version); \ + eh->e_ident[EI_VERSION] = LIBELF_PRIVATE(version) & 0xFFU; \ eh->e_machine = EM_NONE; \ eh->e_type = ELF_K_NONE; \ eh->e_version = LIBELF_PRIVATE(version); \ @@ -105,7 +104,8 @@ _libelf_ehdr(Elf *e, int ec, int allocate) size_t fsz, msz; uint16_t phnum, shnum, strndx; uint64_t shoff; - int (*xlator)(char *_d, size_t _dsz, char *_s, size_t _c, int _swap); + int (*xlator)(unsigned char *_d, size_t _dsz, unsigned char *_s, + size_t _c, int _swap); assert(ec == ELFCLASS32 || ec == ELFCLASS64); @@ -167,7 +167,7 @@ _libelf_ehdr(Elf *e, int ec, int allocate) return (ehdr); xlator = _libelf_get_translator(ELF_T_EHDR, ELF_TOMEMORY, ec); - (*xlator)(ehdr, msz, e->e_rawfile, (size_t) 1, + (*xlator)((unsigned char*) ehdr, msz, e->e_rawfile, (size_t) 1, e->e_byteorder != LIBELF_PRIVATE(byteorder)); /* diff --git a/contrib/elftoolchain/libelf/libelf_extended.c b/contrib/elftoolchain/libelf/libelf_extended.c index 5343696548d4..96765a8293e1 100644 --- a/contrib/elftoolchain/libelf/libelf_extended.c +++ b/contrib/elftoolchain/libelf/libelf_extended.c @@ -24,14 +24,12 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> - #include <assert.h> #include <libelf.h> #include "_libelf.h" -ELFTC_VCSID("$Id: libelf_extended.c 2225 2011-11-26 18:55:54Z jkoshy $"); +ELFTC_VCSID("$Id: libelf_extended.c 3174 2015-03-27 17:13:41Z emaste $"); /* * Retrieve section #0, allocating a new section if needed. @@ -69,9 +67,9 @@ _libelf_setshnum(Elf *e, void *eh, int ec, size_t shnum) } if (ec == ELFCLASS32) - ((Elf32_Ehdr *) eh)->e_shnum = shnum; + ((Elf32_Ehdr *) eh)->e_shnum = shnum & 0xFFFFU; else - ((Elf64_Ehdr *) eh)->e_shnum = shnum; + ((Elf64_Ehdr *) eh)->e_shnum = shnum & 0xFFFFU; return (1); @@ -99,9 +97,9 @@ _libelf_setshstrndx(Elf *e, void *eh, int ec, size_t shstrndx) } if (ec == ELFCLASS32) - ((Elf32_Ehdr *) eh)->e_shstrndx = shstrndx; + ((Elf32_Ehdr *) eh)->e_shstrndx = shstrndx & 0xFFFFU; else - ((Elf64_Ehdr *) eh)->e_shstrndx = shstrndx; + ((Elf64_Ehdr *) eh)->e_shstrndx = shstrndx & 0xFFFFU; return (1); } @@ -128,9 +126,9 @@ _libelf_setphnum(Elf *e, void *eh, int ec, size_t phnum) } if (ec == ELFCLASS32) - ((Elf32_Ehdr *) eh)->e_phnum = phnum; + ((Elf32_Ehdr *) eh)->e_phnum = phnum & 0xFFFFU; else - ((Elf64_Ehdr *) eh)->e_phnum = phnum; + ((Elf64_Ehdr *) eh)->e_phnum = phnum & 0xFFFFU; return (1); } diff --git a/contrib/elftoolchain/libelf/libelf_memory.c b/contrib/elftoolchain/libelf/libelf_memory.c index 892e909b079c..cb8e8f20438a 100644 --- a/contrib/elftoolchain/libelf/libelf_memory.c +++ b/contrib/elftoolchain/libelf/libelf_memory.c @@ -31,7 +31,7 @@ #include "_libelf.h" -ELFTC_VCSID("$Id: libelf_memory.c 2368 2011-12-29 06:34:28Z jkoshy $"); +ELFTC_VCSID("$Id: libelf_memory.c 3013 2014-03-23 06:16:59Z jkoshy $"); /* * Create an ELF descriptor for a memory image, optionally reporting @@ -39,7 +39,7 @@ ELFTC_VCSID("$Id: libelf_memory.c 2368 2011-12-29 06:34:28Z jkoshy $"); */ Elf * -_libelf_memory(char *image, size_t sz, int reporterror) +_libelf_memory(unsigned char *image, size_t sz, int reporterror) { Elf *e; int e_class; @@ -89,7 +89,7 @@ _libelf_memory(char *image, size_t sz, int reporterror) e->e_version = e_version; } } else if (sz >= SARMAG && - strncmp(image, ARMAG, (size_t) SARMAG) == 0) + strncmp((const char *) image, ARMAG, (size_t) SARMAG) == 0) return (_libelf_ar_open(e, reporterror)); return (e); diff --git a/contrib/elftoolchain/libelf/libelf_msize.m4 b/contrib/elftoolchain/libelf/libelf_msize.m4 index 411019bd9127..179880c43278 100644 --- a/contrib/elftoolchain/libelf/libelf_msize.m4 +++ b/contrib/elftoolchain/libelf/libelf_msize.m4 @@ -24,15 +24,13 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> - #include <assert.h> #include <libelf.h> #include <string.h> #include "_libelf.h" -ELFTC_VCSID("$Id: libelf_msize.m4 2225 2011-11-26 18:55:54Z jkoshy $"); +ELFTC_VCSID("$Id: libelf_msize.m4 3174 2015-03-27 17:13:41Z emaste $"); /* WARNING: GENERATED FROM __file__. */ diff --git a/contrib/elftoolchain/libelf/libelf_open.c b/contrib/elftoolchain/libelf/libelf_open.c index 6d93c73a0312..7ec33952dfe1 100644 --- a/contrib/elftoolchain/libelf/libelf_open.c +++ b/contrib/elftoolchain/libelf/libelf_open.c @@ -39,7 +39,7 @@ #include <sys/mman.h> #endif -ELFTC_VCSID("$Id: libelf_open.c 2932 2013-03-30 01:26:04Z jkoshy $"); +ELFTC_VCSID("$Id: libelf_open.c 3007 2014-03-22 08:10:14Z jkoshy $"); #define _LIBELF_INITSIZE (64*1024) @@ -73,11 +73,11 @@ _libelf_read_special_file(int fd, size_t *fsz) } do { - readsz = bufsz - datasz; + assert(bufsz - datasz > 0); t = buf + datasz; - if ((readsz = read(fd, t, readsz)) <= 0) + if ((readsz = read(fd, t, bufsz - datasz)) <= 0) break; - datasz += readsz; + datasz += (size_t) readsz; } while (datasz < bufsz); } while (readsz > 0); diff --git a/contrib/elftoolchain/libelf/libelf_phdr.c b/contrib/elftoolchain/libelf/libelf_phdr.c index f2eb697fbc04..ba872bbb9744 100644 --- a/contrib/elftoolchain/libelf/libelf_phdr.c +++ b/contrib/elftoolchain/libelf/libelf_phdr.c @@ -24,8 +24,6 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> - #include <assert.h> #include <gelf.h> #include <libelf.h> @@ -33,7 +31,7 @@ #include "_libelf.h" -ELFTC_VCSID("$Id: libelf_phdr.c 2931 2013-03-23 11:41:07Z jkoshy $"); +ELFTC_VCSID("$Id: libelf_phdr.c 3174 2015-03-27 17:13:41Z emaste $"); void * _libelf_getphdr(Elf *e, int ec) @@ -44,7 +42,8 @@ _libelf_getphdr(Elf *e, int ec) Elf32_Ehdr *eh32; Elf64_Ehdr *eh64; void *ehdr, *phdr; - int (*xlator)(char *_d, size_t _dsz, char *_s, size_t _c, int _swap); + int (*xlator)(unsigned char *_d, size_t _dsz, unsigned char *_s, + size_t _c, int _swap); assert(ec == ELFCLASS32 || ec == ELFCLASS64); diff --git a/contrib/elftoolchain/libelf/libelf_shdr.c b/contrib/elftoolchain/libelf/libelf_shdr.c index fc48423a99dd..862264b50439 100644 --- a/contrib/elftoolchain/libelf/libelf_shdr.c +++ b/contrib/elftoolchain/libelf/libelf_shdr.c @@ -24,14 +24,12 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> - #include <gelf.h> #include <libelf.h> #include "_libelf.h" -ELFTC_VCSID("$Id: libelf_shdr.c 2225 2011-11-26 18:55:54Z jkoshy $"); +ELFTC_VCSID("$Id: libelf_shdr.c 3174 2015-03-27 17:13:41Z emaste $"); void * _libelf_getshdr(Elf_Scn *s, int ec) diff --git a/contrib/elftoolchain/libelf/libelf_xlate.c b/contrib/elftoolchain/libelf/libelf_xlate.c index eda6df8c2260..6ee76244b83e 100644 --- a/contrib/elftoolchain/libelf/libelf_xlate.c +++ b/contrib/elftoolchain/libelf/libelf_xlate.c @@ -24,14 +24,12 @@ * SUCH DAMAGE. */ -#include <sys/cdefs.h> - #include <assert.h> #include <libelf.h> #include "_libelf.h" -ELFTC_VCSID("$Id: libelf_xlate.c 2225 2011-11-26 18:55:54Z jkoshy $"); +ELFTC_VCSID("$Id: libelf_xlate.c 3174 2015-03-27 17:13:41Z emaste $"); /* * Translate to/from the file representation of ELF objects. @@ -99,10 +97,10 @@ _libelf_xlate(Elf_Data *dst, const Elf_Data *src, unsigned int encoding, * buffer. */ if (direction == ELF_TOMEMORY) { - cnt = src->d_size / fsz; + cnt = (size_t) src->d_size / fsz; dsz = cnt * msz; } else { - cnt = src->d_size / msz; + cnt = (size_t) src->d_size / msz; dsz = cnt * fsz; } @@ -112,9 +110,9 @@ _libelf_xlate(Elf_Data *dst, const Elf_Data *src, unsigned int encoding, } sb = (uintptr_t) src->d_buf; - se = sb + src->d_size; + se = sb + (size_t) src->d_size; db = (uintptr_t) dst->d_buf; - de = db + dst->d_size; + de = db + (size_t) dst->d_size; /* * Check for overlapping buffers. Note that db == sb is diff --git a/contrib/elftoolchain/libelftc/Makefile b/contrib/elftoolchain/libelftc/Makefile new file mode 100644 index 000000000000..a5fc2975097f --- /dev/null +++ b/contrib/elftoolchain/libelftc/Makefile @@ -0,0 +1,65 @@ +# $Id: Makefile 2859 2013-01-05 09:21:54Z jkoshy $ + +TOP= ${.CURDIR}/.. + +LIB= elftc + +SRCS= elftc_bfdtarget.c \ + elftc_copyfile.c \ + elftc_demangle.c \ + elftc_set_timestamps.c \ + elftc_string_table.c \ + elftc_version.c \ + libelftc_bfdtarget.c \ + libelftc_dem_arm.c \ + libelftc_dem_gnu2.c \ + libelftc_dem_gnu3.c \ + libelftc_hash.c \ + libelftc_vstr.c + +INCS= libelftc.h +INCSDIR= /usr/include + +RELEASE= HEAD # Change this on release branches. + +SHLIB_MAJOR= 1 + +WARNS?= 6 + +CLEANFILES+= elftc_version.c + +LDADD+= -lelf + +MAN= elftc.3 \ + elftc_bfd_find_target.3 \ + elftc_copyfile.3 \ + elftc_demangle.3 \ + elftc_set_timestamps.3 \ + elftc_string_table_create.3 \ + elftc_version.3 + +MLINKS= elftc_bfd_find_target.3 elftc_bfd_target_byteorder.3 \ + elftc_bfd_find_target.3 elftc_bfd_target_class.3 \ + elftc_bfd_find_target.3 elftc_bfd_target_flavor.3 \ + elftc_string_table_create.3 elftc_string_table_from_section.3 \ + elftc_string_table_create.3 elftc_string_table_destroy.3 \ + elftc_string_table_create.3 elftc_string_table_image.3 \ + elftc_string_table_create.3 elftc_string_table_insert.3 \ + elftc_string_table_create.3 elftc_string_table_lookup.3 \ + elftc_symbol_table_create.3 elftc_symbol_table_create_nested.3 \ + elftc_symbol_table_create.3 elftc_symbol_table_delete_name.3 \ + elftc_symbol_table_create.3 elftc_symbol_table_delete_entry.3 \ + elftc_symbol_table_create.3 elftc_symbol_table_destroy.3 \ + elftc_symbol_table_create.3 elftc_symbol_table_from_section.3 \ + elftc_symbol_table_create.3 elftc_symbol_table_insert.3 \ + elftc_symbol_table_create.3 elftc_symbol_table_iterate.3 \ + elftc_symbol_table_create.3 elftc_symbol_table_lookup.3 \ + elftc_symbol_table_create.3 elftc_symbol_table_to_image.3 + +.if !make(clean) && !make(clobber) +.BEGIN: .SILENT + ${.CURDIR}/make-toolchain-version -t ${TOP} -r ${RELEASE} \ + -h ${OS_HOST} +.endif + +.include "${TOP}/mk/elftoolchain.lib.mk" diff --git a/contrib/elftoolchain/libelftc/Version.map b/contrib/elftoolchain/libelftc/Version.map new file mode 100644 index 000000000000..43f9e823b407 --- /dev/null +++ b/contrib/elftoolchain/libelftc/Version.map @@ -0,0 +1,18 @@ +/* + * $Id: Version.map 2574 2012-09-11 15:11:59Z jkoshy $ + */ + +R1.0 { +global: + elftc_bfd_find_target; + elftc_bfd_target_byteorder; + elftc_bfd_target_class; + elftc_bfd_target_flavor; + elftc_bfd_target_machine; + elftc_copyfile; + elftc_demangle; + elftc_set_timestamps; + elftc_version; +local: + *; +}; diff --git a/contrib/elftoolchain/libelftc/_libelftc.h b/contrib/elftoolchain/libelftc/_libelftc.h new file mode 100644 index 000000000000..ba0b10112a57 --- /dev/null +++ b/contrib/elftoolchain/libelftc/_libelftc.h @@ -0,0 +1,93 @@ +/*- + * Copyright (c) 2009 Kai Wang + * Copyright (c) 2007,2008 Hyogeol Lee <hyogeollee@gmail.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer + * in this position and unchanged. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (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: _libelftc.h 3174 2015-03-27 17:13:41Z emaste $ + */ + +#ifndef __LIBELFTC_H_ +#define __LIBELFTC_H_ + +#include <stdbool.h> + +#include "_elftc.h" + +struct _Elftc_Bfd_Target { + const char *bt_name; /* target name. */ + unsigned int bt_type; /* target type. */ + unsigned int bt_byteorder; /* elf target byteorder. */ + unsigned int bt_elfclass; /* elf target class (32/64bit). */ + unsigned int bt_machine; /* elf target arch. */ + unsigned int bt_osabi; /* elf target abi. */ +}; + +extern struct _Elftc_Bfd_Target _libelftc_targets[]; + +/** @brief Dynamic vector data for string. */ +struct vector_str { + /** Current size */ + size_t size; + /** Total capacity */ + size_t capacity; + /** String array */ + char **container; +}; + +#define BUFFER_GROWFACTOR 1.618 + +#define ELFTC_FAILURE 0 +#define ELFTC_ISDIGIT(C) (isdigit((C) & 0xFF)) +#define ELFTC_SUCCESS 1 + +#define VECTOR_DEF_CAPACITY 8 + + +#ifdef __cplusplus +extern "C" { +#endif +char *cpp_demangle_ARM(const char *_org); +char *cpp_demangle_gnu2(const char *_org); +char *cpp_demangle_gnu3(const char *_org); +bool is_cpp_mangled_ARM(const char *_org); +bool is_cpp_mangled_gnu2(const char *_org); +bool is_cpp_mangled_gnu3(const char *_org); +unsigned int libelftc_hash_string(const char *); +void vector_str_dest(struct vector_str *_vec); +int vector_str_find(const struct vector_str *_vs, const char *_str, + size_t _len); +char *vector_str_get_flat(const struct vector_str *_vs, size_t *_len); +bool vector_str_init(struct vector_str *_vs); +bool vector_str_pop(struct vector_str *_vs); +bool vector_str_push(struct vector_str *_vs, const char *_str, + size_t _len); +bool vector_str_push_vector_head(struct vector_str *_dst, + struct vector_str *_org); +char *vector_str_substr(const struct vector_str *_vs, size_t _begin, + size_t _end, size_t *_rlen); +#ifdef __cplusplus +} +#endif + +#endif /* __LIBELFTC_H */ diff --git a/contrib/elftoolchain/libelftc/elftc.3 b/contrib/elftoolchain/libelftc/elftc.3 new file mode 100644 index 000000000000..bbf51738bb20 --- /dev/null +++ b/contrib/elftoolchain/libelftc/elftc.3 @@ -0,0 +1,83 @@ +.\" Copyright (c) 2012 Joseph Koshy. All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" This software is provided by Joseph Koshy ``as is'' and +.\" any express or implied warranties, including, but not limited to, the +.\" implied warranties of merchantability and fitness for a particular purpose +.\" are disclaimed. in no event shall Joseph Koshy be liable +.\" for any direct, indirect, incidental, special, exemplary, or consequential +.\" damages (including, but not limited to, procurement of substitute goods +.\" or services; loss of use, data, or profits; or business interruption) +.\" however caused and on any theory of liability, whether in contract, strict +.\" liability, or tort (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: elftc.3 2818 2012-12-24 12:32:48Z jkoshy $ +.\" +.Dd December 24, 2012 +.Os +.Dt ELFTC 3 +.Sh NAME +.Nm elftc +.Nd support routines used in the Elftoolchain project +.Sh LIBRARY +.Lb libelftc +.Sh SYNOPSIS +.In libelftc.h +.Sh DESCRIPTION +The +.Lb libelftc +provides support routines used for developing the utilities in the +Elftoolchain source tree. +.Pp +This manual page serves as an overview of the functionality in this +library. +Additional reference information may be found in the individual +manual pages for the functions listed below. +.Ss Functional Grouping +.Bl -tag -width indent +.It "Binary Object Handling" +.Bl -tag -compact +.It Fn elftc_bfd_find_target +Locate a binary object descriptor. +.It Fn elftc_bfd_target_class +Query the ELF class for a binary object descriptor. +.It Fn elftc_bfd_target_byteorder +Query the byte order for a binary object descriptor. +.It Fn elftc_bfd_target_flavor +Query the object format for a binary object descriptor. +.It Fn elftc_bfd_target_machine +Query the target machine for a binary object descriptor. +.El +.It "C++ support" +.Bl -tag -compact +.It Fn elftc_demangle +Decodes a symbol name encoded according to the encoding rules for the +C++ language. +.El +.It "Programming conveniences" +.Bl -tag -compact +.It Fn elftc_copyfile +Copies the contents of a file to another. +.It Fn elftc_set_timestamp +Portably set the time stamps on a file. +.El +.It "Project Configuration" +.Bl -tag -compact +.It Fn elftc_version +Returns a project-wide identifier string that encodes the source +revision of the source tree. +.El +.El +.Sh SEE ALSO +.Xr dwarf 3 , +.Xr elf 3 diff --git a/contrib/elftoolchain/libelftc/elftc_bfd_find_target.3 b/contrib/elftoolchain/libelftc/elftc_bfd_find_target.3 new file mode 100644 index 000000000000..851ae314ea7e --- /dev/null +++ b/contrib/elftoolchain/libelftc/elftc_bfd_find_target.3 @@ -0,0 +1,189 @@ +.\" Copyright (c) 2010-2011 Joseph Koshy. All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" This software is provided by Joseph Koshy ``as is'' and +.\" any express or implied warranties, including, but not limited to, the +.\" implied warranties of merchantability and fitness for a particular purpose +.\" are disclaimed. in no event shall Joseph Koshy be liable +.\" for any direct, indirect, incidental, special, exemplary, or consequential +.\" damages (including, but not limited to, procurement of substitute goods +.\" or services; loss of use, data, or profits; or business interruption) +.\" however caused and on any theory of liability, whether in contract, strict +.\" liability, or tort (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: elftc_bfd_find_target.3 2251 2011-11-30 16:50:06Z jkoshy $ +.\" +.Dd November 30, 2011 +.Os +.Dt ELFTC_BFD_FIND_TARGET +.Sh NAME +.Nm elftc_bfd_find_target , +.Nm elftc_bfd_target_byteorder , +.Nm elftc_bfd_target_class , +.Nm elftc_bfd_target_flavor , +.Nm elftc_bfd_target_machine +.Nd binary object descriptor handling +.Sh LIBRARY +.Lb libelftc +.Sh SYNOPSIS +.In libelftc.h +.Vt struct Elftc_Bfd_Target; +.Ft "Elftc_Bfd_Target *" +.Fn elftc_bfd_find_target "const char *target_name" +.Ft "unsigned int" +.Fn elftc_bfd_target_class "Elftc_Bfd_Target *target" +.Ft "unsigned int" +.Fn elftc_bfd_target_byteorder "Elftc_Bfd_Target *target" +.Ft Elftc_Bfd_Target_Flavor +.Fn elftc_bfd_target_flavor "Elftc_Bfd_Target *target" +.Ft "unsigned int" +.Fn elftc_bfd_target_machine "Elftc_Bfd_Target *target" +.Sh DESCRIPTION +Function +.Fn elftc_bfd_find_target +locates a binary object descriptor corresponding to the descriptor +name in argument +.Ar "target_name" . +Binary object descriptors encapsulate properties of an object format +such as its file representation, ELF class, and byte endianness. +.Pp +Known descriptor names and their properties include: +.Bl -column -offset "XXXX" ".Li elf32-x86-64-freebsd" "Object format" "Byte Order" "ELF Class" +.It Em Name Ta Em "Object Format" Ta Em "Byte Order" Ta Em "ELF Class" +.It Li binary Ta Binary Ta - Ta - +.It Li elf32-avr Ta ELF Ta LSB Ta 32 +.It Li elf32-big Ta ELF Ta MSB Ta 32 +.It Li elf32-bigarm Ta ELF Ta MSB Ta 32 +.It Li elf32-bigmips Ta ELF Ta MSB Ta 32 +.It Li elf32-i386 Ta ELF Ta LSB Ta 32 +.It Li elf32-i386-freebsd Ta ELF Ta LSB Ta 32 +.It Li elf32-ia64-big Ta ELF Ta MSB Ta 32 +.It Li elf32-little Ta ELF Ta LSB Ta 32 +.It Li elf32-littlearm Ta ELF Ta LSB Ta 32 +.It Li elf32-littlemips Ta ELF Ta LSB Ta 32 +.It Li elf32-powerpc Ta ELF Ta MSB Ta 32 +.It Li elf32-powerpcle Ta ELF Ta LSB Ta 32 +.It Li elf32-sh Ta ELF Ta MSB Ta 32 +.It Li elf32-shl Ta ELF Ta LSB Ta 32 +.It Li elf32-sh-nbsd Ta ELF Ta MSB Ta 32 +.It Li elf32-shl-nbsd Ta ELF Ta LSB Ta 32 +.It Li elf32-shbig-linux Ta ELF Ta MSB Ta 32 +.It Li elf32-shl-linux Ta ELF Ta LSB Ta 32 +.It Li elf32-sparc Ta ELF Ta MSB Ta 32 +.It Li elf64-alpha Ta ELF Ta LSB Ta 64 +.It Li elf64-alpha-freebsd Ta ELF Ta LSB Ta 64 +.It Li elf64-big Ta ELF Ta MSB Ta 64 +.It Li elf64-bigmips Ta ELF Ta MSB Ta 64 +.It Li elf64-ia64-big Ta ELF Ta MSB Ta 64 +.It Li elf64-ia64-little Ta ELF Ta LSB Ta 64 +.It Li elf64-little Ta ELF Ta LSB Ta 64 +.It Li elf64-littlemips Ta ELF Ta LSB Ta 64 +.It Li elf64-powerpc Ta ELF Ta MSB Ta 64 +.It Li elf64-powerpcle Ta ELF Ta LSB Ta 64 +.It Li elf64-sh64 Ta ELF Ta MSB Ta 64 +.It Li elf64-sh64l Ta ELF Ta LSB Ta 64 +.It Li elf64-sh64-nbsd Ta ELF Ta MSB Ta 64 +.It Li elf64-sh64l-nbsd Ta ELF Ta LSB Ta 64 +.It Li elf64-sh64big-linux Ta ELF Ta MSB Ta 64 +.It Li elf64-sh64-linux Ta ELF Ta LSB Ta 64 +.It Li elf64-sparc Ta ELF Ta MSB Ta 64 +.It Li elf64-sparc-freebsd Ta ELF Ta MSB Ta 64 +.It Li elf64-x86-64 Ta ELF Ta LSB Ta 64 +.It Li elf64-x86-64-freebsd Ta ELF Ta LSB Ta 64 +.It Li ihex Ta IHEX Ta - Ta - +.It Li srec Ta SREC Ta - Ta - +.It Li symbolsrec Ta SREC Ta - Ta - +.El +.Pp +Function +.Fn elftc_bfd_target_byteorder +returns the ELF byte order associated with target descriptor +.Ar target . +.Pp +Function +.Fn elftc_bfd_target_class +returns the ELF class associated with target descriptor +.Ar target . +.Pp +Function +.Fn elftc_bfd_target_flavor +returns the object format associated with target descriptor +.Ar target . +The known object formats are: +.Bl -tag -offset "XXXX" -width ".Dv ETF_BINARY" -compact +.It Dv ETF_ELF +An ELF object. +.It Dv ETF_BINARY +Raw binary. +.It Dv ETF_IHEX +An object encoded in +.Tn Intel +hex format. +.It Dv ETF_NONE +An unknown object format. +.It Dv ETF_SREC +An object encoded as S-records. +.El +.Sh RETURN VALUES +Function +.Fn elftc_bfd_find_target +returns a valid pointer to an opaque binary target descriptor if +successful, or NULL in case of an error. +.Pp +Function +.Fn elftc_bfd_target_byteorder +returns the ELF byte order associated with the target descriptor; one of +.Dv ELFDATA2MSB +or +.Dv ELFDATA2LSB . +.Pp +Function +.Fn elftc_bfd_target_class +returns the ELF class associated with the target descriptor; one of +.Dv ELFCLASS32 +or +.Dv ELFCLASS64 . +.Pp +Function +.Fn elftc_bfd_target_machine +returns the ELF architecture associated with the target descriptor. +.Pp +Function +.Fn elftc_bfd_target_flavor +returns one of +.Dv ETF_BINARY , +.Dv ETF_ELF , +.Dv ETF_IHEX +or +.Dv ETF_SREC +if successful or +.Dv ETF_NONE +in case of error. +.Sh EXAMPLES +To return descriptor information associated with target name +.Dq elf64-big +use: +.Bd -literal -offset indent +struct Elftc_Bfd_Target *t; + +if ((t = elftc_bfd_find_target("elf64-big")) == NULL) + errx(EXIT_FAILURE, "Cannot find target descriptor"); + +printf("Class: %s\\n", elftc_bfd_target_class(t) == ELFCLASS32 ? + "ELFCLASS32" : "ELFCLASS64"); +printf("Byteorder: %s\\n", + elftc_bfd_target_byteorder(t) == ELFDATA2LSB ? "LSB" : "MSB"); +printf("Flavor: %d\\n", elftc_bfd_target_flavor(t)); +.Ed +.Sh SEE ALSO +.Xr elf 3 diff --git a/contrib/elftoolchain/libelftc/elftc_bfdtarget.c b/contrib/elftoolchain/libelftc/elftc_bfdtarget.c new file mode 100644 index 000000000000..a5ae1a671893 --- /dev/null +++ b/contrib/elftoolchain/libelftc/elftc_bfdtarget.c @@ -0,0 +1,73 @@ +/*- + * Copyright (c) 2008,2009 Kai Wang + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer + * in this position and unchanged. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/param.h> +#include <string.h> +#include <libelftc.h> + +#include "_libelftc.h" + +ELFTC_VCSID("$Id: elftc_bfdtarget.c 3174 2015-03-27 17:13:41Z emaste $"); + +Elftc_Bfd_Target * +elftc_bfd_find_target(const char *tgt_name) +{ + Elftc_Bfd_Target *tgt; + + for (tgt = _libelftc_targets; tgt->bt_name; tgt++) + if (!strcmp(tgt_name, tgt->bt_name)) + return (tgt); + + return (NULL); /* not found */ +} + +Elftc_Bfd_Target_Flavor +elftc_bfd_target_flavor(Elftc_Bfd_Target *tgt) +{ + + return (tgt->bt_type); +} + +unsigned int +elftc_bfd_target_byteorder(Elftc_Bfd_Target *tgt) +{ + + return (tgt->bt_byteorder); +} + +unsigned int +elftc_bfd_target_class(Elftc_Bfd_Target *tgt) +{ + + return (tgt->bt_elfclass); +} + +unsigned int +elftc_bfd_target_machine(Elftc_Bfd_Target *tgt) +{ + + return (tgt->bt_machine); +} diff --git a/contrib/elftoolchain/libelftc/elftc_copyfile.3 b/contrib/elftoolchain/libelftc/elftc_copyfile.3 new file mode 100644 index 000000000000..45204338bac7 --- /dev/null +++ b/contrib/elftoolchain/libelftc/elftc_copyfile.3 @@ -0,0 +1,73 @@ +.\" Copyright (c) 2011 Joseph Koshy. All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" This software is provided by Joseph Koshy ``as is'' and +.\" any express or implied warranties, including, but not limited to, the +.\" implied warranties of merchantability and fitness for a particular purpose +.\" are disclaimed. in no event shall Joseph Koshy be liable +.\" for any direct, indirect, incidental, special, exemplary, or consequential +.\" damages (including, but not limited to, procurement of substitute goods +.\" or services; loss of use, data, or profits; or business interruption) +.\" however caused and on any theory of liability, whether in contract, strict +.\" liability, or tort (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: elftc_copyfile.3 2315 2011-12-11 09:28:55Z jkoshy $ +.\" +.Dd December 11, 2011 +.Os +.Dt ELFTC_COPYFILE 3 +.Sh NAME +.Nm elftc_copyfile +.Nd convenience function to copy data +.Sh LIBRARY +.Lb libelftc +.Sh SYNOPSIS +.In libelftc.h +.Ft in +.Fn elftc_copyfile "int ifd" "int ofd" +.Sh DESCRIPTION +The function +.Fn elftc_copyfile +copies the contents of the file referenced by argument +.Ar ifd +to the file referenced by argument +.Ar ofd . +.Pp +The argument +.Ar ifd +should contain a file descriptor opened for reading, with its file +offset at the beginning of the file. +.Pp +The argument +.Ar ofd +should contain a file descriptor opened for writing. +.Sh RETURN VALUE +.Rv -std +.Sh ERRORS +The function +.Fn elftc_copyfile +may fail with any of the errors returned by +.Xr fstat 2 , +.Xr malloc 3 , +.Xr mmap 2 , +.Xr munmap 2 , +.Xr read 2 +or +.Xr write 2 . +.Sh SEE ALSO +.Xr fstat 2 , +.Xr malloc 3 , +.Xr mmap 2 , +.Xr munmap 2 , +.Xr read 2 , +.Xr write 2 diff --git a/contrib/elftoolchain/libelftc/elftc_copyfile.c b/contrib/elftoolchain/libelftc/elftc_copyfile.c new file mode 100644 index 000000000000..7df1678e702d --- /dev/null +++ b/contrib/elftoolchain/libelftc/elftc_copyfile.c @@ -0,0 +1,109 @@ +/*- + * Copyright (c) 2011, Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer + * in this position and unchanged. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/types.h> +#include <sys/stat.h> + +#include <stdlib.h> +#include <unistd.h> + +#include "libelftc.h" +#include "_libelftc.h" + +#if ELFTC_HAVE_MMAP +#include <sys/mman.h> +#endif + +ELFTC_VCSID("$Id: elftc_copyfile.c 2981 2014-02-01 02:41:13Z jkoshy $"); + +/* + * Copy the contents referenced by 'ifd' to 'ofd'. Returns 0 on + * success and -1 on error. + */ + +int +elftc_copyfile(int ifd, int ofd) +{ + int buf_mmapped; + struct stat sb; + char *b, *buf; + ssize_t nw; + size_t n; + + /* Determine the input file's size. */ + if (fstat(ifd, &sb) < 0) + return (-1); + + /* Skip files without content. */ + if (sb.st_size == 0) + return (0); + + buf = NULL; + buf_mmapped = 0; + +#if ELFTC_HAVE_MMAP + /* + * Prefer mmap() if it is available. + */ + buf = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, ifd, (off_t) 0); + if (buf != MAP_FAILED) + buf_mmapped = 1; + else + buf = NULL; +#endif + + /* + * If mmap() is not available, or if the mmap() operation + * failed, allocate a buffer, and read in input data. + */ + if (buf_mmapped == false) { + if ((buf = malloc(sb.st_size)) == NULL) + return (-1); + if (read(ifd, buf, sb.st_size) != sb.st_size) { + free(buf); + return (-1); + } + } + + /* + * Write data to the output file descriptor. + */ + for (n = sb.st_size, b = buf; n > 0; n -= nw, b += nw) + if ((nw = write(ofd, b, n)) <= 0) + break; + + /* Release the input buffer. */ +#if ELFTC_HAVE_MMAP + if (buf_mmapped && munmap(buf, sb.st_size) < 0) + return (-1); +#endif + + if (!buf_mmapped) + free(buf); + + return (n > 0 ? -1 : 0); +} + diff --git a/contrib/elftoolchain/libelftc/elftc_demangle.3 b/contrib/elftoolchain/libelftc/elftc_demangle.3 new file mode 100644 index 000000000000..44cf0073ca55 --- /dev/null +++ b/contrib/elftoolchain/libelftc/elftc_demangle.3 @@ -0,0 +1,116 @@ +.\" Copyright (c) 2009,2011 Joseph Koshy. All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" This software is provided by Joseph Koshy ``as is'' and +.\" any express or implied warranties, including, but not limited to, the +.\" implied warranties of merchantability and fitness for a particular purpose +.\" are disclaimed. in no event shall Joseph Koshy be liable +.\" for any direct, indirect, incidental, special, exemplary, or consequential +.\" damages (including, but not limited to, procurement of substitute goods +.\" or services; loss of use, data, or profits; or business interruption) +.\" however caused and on any theory of liability, whether in contract, strict +.\" liability, or tort (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: elftc_demangle.3 3182 2015-04-10 16:08:10Z emaste $ +.\" +.Dd August 24, 2011 +.Os +.Dt ELFTC_DEMANGLE 3 +.Sh NAME +.Nm elftc_demangle +.Nd demangle a C++ name +.Sh LIBRARY +.Lb libelftc +.Sh SYNOPSIS +.In libelftc.h +.Ft int +.Fo elftc_demangle +.Fa "const char *encodedname" +.Fa "char *buffer" +.Fa "size_t bufsize" +.Fa "unsigned int flags" +.Fc +.Sh DESCRIPTION +Function +.Fn elftc_demangle +decodes a symbol name encoded according to the type encoding rules +for the C++ language and returns a string denoting an equivalent +C++ prototype. +.Pp +Argument +.Ar encodedname +specifies the encoded symbol name. +Argument +.Ar buffer +denotes a programmer-specified area to place the prototype string in. +Argument +.Ar bufsize +specifies the size of the programmer-specified area. +Argument +.Ar flags +specifies the encoding style in use for argument +.Ar encodedname . +Supported encoding styles are: +.Bl -tag -width ".Dv ELFTC_DEM_GNU3" +.It Dv ELFTC_DEM_ARM +The encoding style used by compilers adhering to the conventions of the +C++ Annotated Reference Manual. +.It Dv ELFTC_DEM_GNU2 +The encoding style by GNU C++ version 2. +.It Dv ELFTC_DEM_GNU3 +The encoding style by GNU C++ version 3 and later. +.El +.Pp +Argument +.Ar flags +may be zero, in which case the function will attempt to guess the +encoding scheme from the contents of +.Ar encodedname . +.Sh RETURN VALUE +Function +.Fn elftc_demangle +returns 0 on success. +In case of an error it returns -1 and sets the +.Va errno +variable. +.Sh EXAMPLES +To decode a name that uses an unknown encoding style use: +.Bd -literal -offset indent +char buffer[1024]; +const char *funcname; + +funcname = ...; /* points to string to be demangled */ +if (elftc_demangle(funcname, buffer, sizeof(buffer), 0) == 0) + printf("Demangled name: %\\n", buffer); +else + perror("Cannot demangle %s", funcname); +.Ed +.Sh ERRORS +Function +.Fn elftc_demangle +may fail with the following errors: +.Bl -tag -width ".Bq Er ENAMETOOLONG" +.It Bq Er EINVAL +Argument +.Ar encodedname +was not a valid encoded name. +.It Bq Er ENAMETOOLONG +The output buffer specified by arguments +.Ar buffer +and +.Ar bufsize +was too small to hold the decoded function prototype. +.El +.Sh SEE ALSO +.Xr elf 3 , +.Xr elf_strptr 3 diff --git a/contrib/elftoolchain/libelftc/elftc_demangle.c b/contrib/elftoolchain/libelftc/elftc_demangle.c new file mode 100644 index 000000000000..ff3095504286 --- /dev/null +++ b/contrib/elftoolchain/libelftc/elftc_demangle.c @@ -0,0 +1,107 @@ +/*- + * Copyright (c) 2009 Kai Wang + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer + * in this position and unchanged. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/param.h> +#include <assert.h> +#include <errno.h> +#include <libelftc.h> +#include <stdlib.h> +#include <string.h> + +#include "_libelftc.h" + +ELFTC_VCSID("$Id: elftc_demangle.c 3174 2015-03-27 17:13:41Z emaste $"); + +static int +is_mangled(const char *s, int style) +{ + + switch (style) { + case ELFTC_DEM_ARM: return (is_cpp_mangled_ARM(s) ? style : 0); + case ELFTC_DEM_GNU2: return (is_cpp_mangled_gnu2(s) ? style : 0); + case ELFTC_DEM_GNU3: return (is_cpp_mangled_gnu3(s) ? style : 0); + } + + /* No style or invalid style spcified, try to guess. */ + if (is_cpp_mangled_gnu3(s)) + return (ELFTC_DEM_GNU3); + if (is_cpp_mangled_gnu2(s)) + return (ELFTC_DEM_GNU2); + if (is_cpp_mangled_ARM(s)) + return (ELFTC_DEM_ARM); + + /* Cannot be demangled. */ + return (0); +} + +static char * +demangle(const char *s, int style, int rc) +{ + + (void) rc; /* XXX */ + switch (style) { + case ELFTC_DEM_ARM: return (cpp_demangle_ARM(s)); + case ELFTC_DEM_GNU2: return (cpp_demangle_gnu2(s)); + case ELFTC_DEM_GNU3: return (cpp_demangle_gnu3(s)); + default: + assert(0); + return (NULL); + } +} + +int +elftc_demangle(const char *mangledname, char *buffer, size_t bufsize, + unsigned int flags) +{ + int style, rc; + char *rlt; + + style = flags & 0xFFFF; + rc = flags >> 16; + + if (mangledname == NULL || + ((style = is_mangled(mangledname, style)) == 0)) { + errno = EINVAL; + return (-1); + } + + if ((rlt = demangle(mangledname, style, rc)) == NULL) { + errno = EINVAL; + return (-1); + } + + if (buffer == NULL || bufsize < strlen(rlt) + 1) { + free(rlt); + errno = ENAMETOOLONG; + return (-1); + } + + strncpy(buffer, rlt, bufsize); + buffer[bufsize - 1] = '\0'; + free(rlt); + + return (0); +} diff --git a/contrib/elftoolchain/libelftc/elftc_set_timestamps.3 b/contrib/elftoolchain/libelftc/elftc_set_timestamps.3 new file mode 100644 index 000000000000..115fe92707a3 --- /dev/null +++ b/contrib/elftoolchain/libelftc/elftc_set_timestamps.3 @@ -0,0 +1,84 @@ +.\" Copyright (c) 2011 Joseph Koshy. All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" This software is provided by Joseph Koshy ``as is'' and +.\" any express or implied warranties, including, but not limited to, the +.\" implied warranties of merchantability and fitness for a particular purpose +.\" are disclaimed. in no event shall Joseph Koshy be liable +.\" for any direct, indirect, incidental, special, exemplary, or consequential +.\" damages (including, but not limited to, procurement of substitute goods +.\" or services; loss of use, data, or profits; or business interruption) +.\" however caused and on any theory of liability, whether in contract, strict +.\" liability, or tort (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$ +.\" +.Dd December 15, 2011 +.Os +.Dt ELFTC_SET_TIMESTAMPS 3 +.Sh NAME +.Nm elftc_set_timestamps +.Nd set file timestamps +.Sh LIBRARY +.Lb libelftc +.Sh SYNOPSIS +.In libelftc.h +.Ft int +.Fn elftc_set_timestamps "const char *filename" "struct stat *sb" +.Sh DESCRIPTION +The +.Fn elftc_set_timestamps +function is used to set the access and modified time stamps on a file +based on the contents of a +.Vt "struct stat" +descriptor. +.Pp +Argument +.Ar filename +names an existing file in the file system. +.Pp +Argument +.Ar sb +points to structure of type +.Vt "struct stat" +populated by a prior call to +.Xr fstat 2 +or +.Xr stat 2 . +.Sh IMPLEMENTATION NOTES +This function will invoke the high-resolution +.Xr utimes 2 +system call if the underlying operating system supports it. +On operating systems lacking support for +.Xr utimes 2 , +the function will use lower resolution +.Xr utime 2 +system call. +.Sh EXAMPLES +To set the access and modified times for a new file to those of an +existing file, use: +.Bd -literal -offset indent +struct stat sb; +const char *existing_filename, *new_filename; + +if (stat(existing_filename, &sb) < 0) + err(EXIT_FAILURE, "stat failed"); + +if (elftc_set_timestamps(new_filename, &sb) < 0) + err(EXIT_FAILURE, "timestamps could not be set"); +.Ed +.Sh SEE ALSO +.Xr fstat 2 , +.Xr stat 2 , +.Xr utime 2 , +.Xr utimes 2 . diff --git a/contrib/elftoolchain/libelftc/elftc_set_timestamps.c b/contrib/elftoolchain/libelftc/elftc_set_timestamps.c new file mode 100644 index 000000000000..cb28c351e0d4 --- /dev/null +++ b/contrib/elftoolchain/libelftc/elftc_set_timestamps.c @@ -0,0 +1,85 @@ +/*- + * Copyright (c) 2011 Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <sys/types.h> + +#include "libelftc.h" + +#include "_libelftc.h" + +ELFTC_VCSID("$Id$"); + +/* + * Determine the field name for the timestamp fields inside a 'struct + * stat'. + */ + +#if defined(__FreeBSD__) || defined(__NetBSD__) +#define ATIME st_atimespec +#define MTIME st_mtimespec +#define LIBELFTC_HAVE_UTIMES 1 +#endif + +#if defined(__DragonFly__) || defined(__linux__) || defined(__OpenBSD__) +#define ATIME st_atim +#define MTIME st_mtim +#define LIBELFTC_HAVE_UTIMES 1 +#endif + +#if LIBELFTC_HAVE_UTIMES +#include <sys/time.h> +#else +#include <utime.h> +#endif + +int +elftc_set_timestamps(const char *fn, struct stat *sb) +{ +#if LIBELFTC_HAVE_UTIMES + /* + * The BSD utimes() system call offers timestamps + * 1-microsecond granularity. + */ + struct timeval tv[2]; + + tv[0].tv_sec = sb->ATIME.tv_sec; + tv[0].tv_usec = sb->ATIME.tv_nsec / 1000; + tv[1].tv_sec = sb->MTIME.tv_sec; + tv[1].tv_usec = sb->MTIME.tv_nsec / 1000; + + return (utimes(fn, tv)); +#else + /* + * On OSes without utimes(), fall back to the POSIX utime() + * call, which offers 1-second granularity. + */ + struct utimbuf utb; + + utb.actime = sb->st_atime; + utb.modtime = sb->st_mtime; + return (utime(fn, &utb)); +#endif +} diff --git a/contrib/elftoolchain/libelftc/elftc_string_table.c b/contrib/elftoolchain/libelftc/elftc_string_table.c new file mode 100644 index 000000000000..bba9ac6a76cd --- /dev/null +++ b/contrib/elftoolchain/libelftc/elftc_string_table.c @@ -0,0 +1,392 @@ +/*- + * Copyright (c) 2013, Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer + * in this position and unchanged. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/param.h> +#include <sys/queue.h> + +#include <assert.h> +#include <errno.h> +#include <gelf.h> +#include <stdlib.h> +#include <string.h> + +#include "libelftc.h" +#include "_libelftc.h" + +ELFTC_VCSID("$Id: elftc_string_table.c 2869 2013-01-06 13:29:18Z jkoshy $"); + +#define ELFTC_STRING_TABLE_DEFAULT_SIZE (4*1024) +#define ELFTC_STRING_TABLE_EXPECTED_STRING_SIZE 16 +#define ELFTC_STRING_TABLE_EXPECTED_CHAIN_LENGTH 8 +#define ELFTC_STRING_TABLE_POOL_SIZE_INCREMENT (4*1024) + +struct _Elftc_String_Table_Entry { + int ste_idx; + SLIST_ENTRY(_Elftc_String_Table_Entry) ste_next; +}; + +#define ELFTC_STRING_TABLE_COMPACTION_FLAG 0x1 +#define ELFTC_STRING_TABLE_LENGTH(st) ((st)->st_len >> 1) +#define ELFTC_STRING_TABLE_CLEAR_COMPACTION_FLAG(st) do { \ + (st)->st_len &= ~ELFTC_STRING_TABLE_COMPACTION_FLAG; \ + } while (0) +#define ELFTC_STRING_TABLE_SET_COMPACTION_FLAG(st) do { \ + (st)->st_len |= ELFTC_STRING_TABLE_COMPACTION_FLAG; \ + } while (0) +#define ELFTC_STRING_TABLE_UPDATE_LENGTH(st, len) do { \ + (st)->st_len = \ + ((st)->st_len & \ + ELFTC_STRING_TABLE_COMPACTION_FLAG) | \ + ((len) << 1); \ + } while (0) + +struct _Elftc_String_Table { + unsigned int st_len; /* length and flags */ + int st_nbuckets; + int st_string_pool_size; + char *st_string_pool; + SLIST_HEAD(_Elftc_String_Table_Bucket, + _Elftc_String_Table_Entry) st_buckets[]; +}; + +static struct _Elftc_String_Table_Entry * +elftc_string_table_find_hash_entry(Elftc_String_Table *st, const char *string, + int *rhashindex) +{ + struct _Elftc_String_Table_Entry *ste; + int hashindex; + char *s; + + hashindex = libelftc_hash_string(string) % st->st_nbuckets; + + if (rhashindex) + *rhashindex = hashindex; + + SLIST_FOREACH(ste, &st->st_buckets[hashindex], ste_next) { + s = st->st_string_pool + abs(ste->ste_idx); + + assert(s > st->st_string_pool && + s < st->st_string_pool + st->st_string_pool_size); + + if (strcmp(s, string) == 0) + return (ste); + } + + return (NULL); +} + +static int +elftc_string_table_add_to_pool(Elftc_String_Table *st, const char *string) +{ + char *newpool; + int len, newsize, stlen; + + len = strlen(string) + 1; /* length, including the trailing NUL */ + stlen = ELFTC_STRING_TABLE_LENGTH(st); + + /* Resize the pool, if needed. */ + if (stlen + len >= st->st_string_pool_size) { + newsize = roundup(st->st_string_pool_size + + ELFTC_STRING_TABLE_POOL_SIZE_INCREMENT, + ELFTC_STRING_TABLE_POOL_SIZE_INCREMENT); + if ((newpool = realloc(st->st_string_pool, newsize)) == + NULL) + return (0); + st->st_string_pool = newpool; + st->st_string_pool_size = newsize; + } + + strcpy(st->st_string_pool + stlen, string); + ELFTC_STRING_TABLE_UPDATE_LENGTH(st, stlen + len); + + return (stlen); +} + +Elftc_String_Table * +elftc_string_table_create(int sizehint) +{ + int n, nbuckets, tablesize; + struct _Elftc_String_Table *st; + + if (sizehint < ELFTC_STRING_TABLE_DEFAULT_SIZE) + sizehint = ELFTC_STRING_TABLE_DEFAULT_SIZE; + + nbuckets = sizehint / (ELFTC_STRING_TABLE_EXPECTED_CHAIN_LENGTH * + ELFTC_STRING_TABLE_EXPECTED_STRING_SIZE); + + tablesize = sizeof(struct _Elftc_String_Table) + + nbuckets * sizeof(struct _Elftc_String_Table_Bucket); + + if ((st = malloc(tablesize)) == NULL) + return (NULL); + if ((st->st_string_pool = malloc(sizehint)) == NULL) { + free(st); + return (NULL); + } + + for (n = 0; n < nbuckets; n++) + SLIST_INIT(&st->st_buckets[n]); + + st->st_len = 0; + st->st_nbuckets = nbuckets; + st->st_string_pool_size = sizehint; + *st->st_string_pool = '\0'; + ELFTC_STRING_TABLE_UPDATE_LENGTH(st, 1); + + return (st); +} + +void +elftc_string_table_destroy(Elftc_String_Table *st) +{ + int n; + struct _Elftc_String_Table_Entry *s, *t; + + for (n = 0; n < st->st_nbuckets; n++) + SLIST_FOREACH_SAFE(s, &st->st_buckets[n], ste_next, t) + free(s); + free(st->st_string_pool); + free(st); + + return; +} + +Elftc_String_Table * +elftc_string_table_from_section(Elf_Scn *scn, int sizehint) +{ + int len; + Elf_Data *d; + GElf_Shdr sh; + const char *s, *end; + Elftc_String_Table *st; + + /* Verify the type of the section passed in. */ + if (gelf_getshdr(scn, &sh) == NULL || + sh.sh_type != SHT_STRTAB) { + errno = EINVAL; + return (NULL); + } + + if ((d = elf_getdata(scn, NULL)) == NULL || + d->d_size == 0) { + errno = EINVAL; + return (NULL); + } + + if ((st = elftc_string_table_create(sizehint)) == NULL) + return (NULL); + + s = d->d_buf; + + /* + * Verify that the first byte of the data buffer is '\0'. + */ + if (*s != '\0') { + errno = EINVAL; + goto fail; + } + + end = s + d->d_size; + + /* + * Skip the first '\0' and insert the strings in the buffer, + * in order. + */ + for (s += 1; s < end; s += len) { + if (elftc_string_table_insert(st, s) == 0) + goto fail; + + len = strlen(s) + 1; /* Include space for the trailing NUL. */ + } + + return (st); + +fail: + if (st) + (void) elftc_string_table_destroy(st); + + return (NULL); +} + +const char * +elftc_string_table_image(Elftc_String_Table *st, size_t *size) +{ + char *r, *s, *end; + struct _Elftc_String_Table_Entry *ste; + struct _Elftc_String_Table_Bucket *head; + int copied, hashindex, offset, length, newsize; + + /* + * For the common case of a string table has not seen + * a string deletion, we can just export the current + * pool. + */ + if ((st->st_len & ELFTC_STRING_TABLE_COMPACTION_FLAG) == 0) { + if (size) + *size = ELFTC_STRING_TABLE_LENGTH(st); + return (st->st_string_pool); + } + + /* + * Otherwise, compact the string table in-place. + */ + assert(*st->st_string_pool == '\0'); + + newsize = 1; + end = st->st_string_pool + ELFTC_STRING_TABLE_LENGTH(st); + + for (r = s = st->st_string_pool + 1; + s < end; + s += length, r += copied) { + + copied = 0; + length = strlen(s) + 1; + + ste = elftc_string_table_find_hash_entry(st, s, + &hashindex); + head = &st->st_buckets[hashindex]; + + assert(ste != NULL); + + /* Ignore deleted strings. */ + if (ste->ste_idx < 0) { + SLIST_REMOVE(head, ste, _Elftc_String_Table_Entry, + ste_next); + free(ste); + continue; + } + + /* Move 'live' strings up. */ + offset = newsize; + newsize += length; + copied = length; + + if (r == s) /* Nothing removed yet. */ + continue; + + memmove(r, s, copied); + + /* Update the index for this entry. */ + ste->ste_idx = offset; + } + + ELFTC_STRING_TABLE_CLEAR_COMPACTION_FLAG(st); + ELFTC_STRING_TABLE_UPDATE_LENGTH(st, newsize); + + if (size) + *size = newsize; + + return (st->st_string_pool); +} + +size_t +elftc_string_table_insert(Elftc_String_Table *st, const char *string) +{ + int hashindex, idx; + struct _Elftc_String_Table_Entry *ste; + + hashindex = 0; + + ste = elftc_string_table_find_hash_entry(st, string, &hashindex); + + assert(hashindex >= 0 && hashindex < st->st_nbuckets); + + if (ste == NULL) { + if ((ste = malloc(sizeof(*ste))) == NULL) + return (0); + if ((ste->ste_idx = elftc_string_table_add_to_pool(st, + string)) == 0) { + free(ste); + return (0); + } + + SLIST_INSERT_HEAD(&st->st_buckets[hashindex], ste, ste_next); + } + + idx = ste->ste_idx; + if (idx < 0) /* Undelete. */ + ste->ste_idx = idx = (- idx); + + return (idx); +} + +size_t +elftc_string_table_lookup(Elftc_String_Table *st, const char *string) +{ + int hashindex, idx; + struct _Elftc_String_Table_Entry *ste; + + ste = elftc_string_table_find_hash_entry(st, string, &hashindex); + + assert(hashindex >= 0 && hashindex < st->st_nbuckets); + + if (ste == NULL || (idx = ste->ste_idx) < 0) + return (0); + + return (idx); +} + +int +elftc_string_table_remove(Elftc_String_Table *st, const char *string) +{ + int idx; + struct _Elftc_String_Table_Entry *ste; + + ste = elftc_string_table_find_hash_entry(st, string, NULL); + + if (ste == NULL || (idx = ste->ste_idx) < 0) + return (ELFTC_FAILURE); + + assert(idx > 0 && idx < (int) ELFTC_STRING_TABLE_LENGTH(st)); + + ste->ste_idx = (- idx); + + ELFTC_STRING_TABLE_SET_COMPACTION_FLAG(st); + + return (ELFTC_SUCCESS); +} + +const char * +elftc_string_table_to_string(Elftc_String_Table *st, size_t offset) +{ + const char *s; + + s = st->st_string_pool + offset; + + /* + * Check for: + * - An offset value within pool bounds. + * - A non-NUL byte at the specified offset. + * - The end of the prior string at offset - 1. + */ + if (offset == 0 || offset >= ELFTC_STRING_TABLE_LENGTH(st) || + *s == '\0' || *(s - 1) != '\0') { + errno = EINVAL; + return (NULL); + } + + return (s); +} diff --git a/contrib/elftoolchain/libelftc/elftc_string_table_create.3 b/contrib/elftoolchain/libelftc/elftc_string_table_create.3 new file mode 100644 index 000000000000..e11e73cc4a51 --- /dev/null +++ b/contrib/elftoolchain/libelftc/elftc_string_table_create.3 @@ -0,0 +1,227 @@ +.\" Copyright (c) 2012-2013 Joseph Koshy. +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" This software is provided by Joseph Koshy ``as is'' and +.\" any express or implied warranties, including, but not limited to, the +.\" implied warranties of merchantability and fitness for a particular purpose +.\" are disclaimed. in no event shall Joseph Koshy be liable +.\" for any direct, indirect, incidental, special, exemplary, or consequential +.\" damages (including, but not limited to, procurement of substitute goods +.\" or services; loss of use, data, or profits; or business interruption) +.\" however caused and on any theory of liability, whether in contract, strict +.\" liability, or tort (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: elftc_string_table_create.3 2866 2013-01-06 03:20:14Z jkoshy $ +.\" +.Dd January 5, 2013 +.Os +.Dt ELFTC_STRING_TABLE_CREATE 3 +.Sh NAME +.Nm elftc_string_table_create , +.Nm elftc_string_table_destroy , +.Nm elftc_string_table_from_section , +.Nm elftc_string_table_image , +.Nm elftc_string_table_insert , +.Nm elftc_string_table_lookup , +.Nm elftc_string_table_remove , +.Nm elftc_string_table_to_string +.Nd convenience routines for handling ELF string tables +.Sh SYNOPSIS +.In libelftc.h +.Ft "Elftc_String_Table *" +.Fn elftc_string_table_create "int sizehint" +.Ft int +.Fn elftc_string_table_destroy "Elftc_String_Table *table" +.Ft "Elftc_String_Table *" +.Fn elftc_string_table_from_section "Elf_Scn *scn" "int sizehint" +.Ft "const char *" +.Fo elftc_string_table_image +.Fa "Elftc_String_Table *table" +.Fa "size_t *size" +.Fc +.Ft size_t +.Fo elftc_string_table_insert +.Fa "Elftc_String_Table *table" +.Fa "const char *string" +.Fc +.Ft size_t +.Fo elftc_string_table_lookup +.Fa "Elftc_String_Table *table" +.Fa "const char *string" +.Fc +.Ft int +.Fo elftc_string_table_remove +.Fa "Elftc_String_Table *table" +.Fa "const char *string" +.Fc +.Ft "const char *" +.Fo elftc_string_table_to_string +.Fa "Elftc_String_Table *table" +.Fa "size_t offset" +.Fc +.Sh DESCRIPTION +This manual page documents convenience routines for handling ELF +string tables. +.Pp +Function +.Fn elftc_string_table_create +creates a new, empty string table. +The argument +.Ar sizehint +provides a hint about the expected number of bytes of string data in +the table. +If the argument +.Ar sizehint +is zero, an implementation-defined default will be used instead. +.Pp +Function +.Fn elftc_string_table_destroy +destroys the previously allocated string table specified by +argument +.Ar table , +and frees the internal resources allocated for it. +.Pp +Function +.Fn elftc_string_table_from_section +creates a new string table and initializes it based on the +contents of the section specified by argument +.Ar scn . +This section must be of type +.Dv SHT_STRTAB . +The argument +.Ar sizehint +provides a hint about expected number of bytes of string data in the +table. +If the value of +.Ar sizehint +is zero, an implementation-default will be used instead. +.Pp +Function +.Fn elftc_string_table_image +returns a pointer to the ELF representation of the contents of the +string table specified by argument +.Ar table . +If argument +.Ar size +is not NULL, the size of the ELF representation of the string table is +stored in the location pointed to by argument +.Ar size . +The function +.Fn elftc_string_table_image +will compact the string table if the table contains deleted strings. +The string offsets returned by prior calls to +.Fn elftc_string_table_insert +and +.Fn elftc_string_table_lookup +should be treated as invalid after a call to this function. +.Pp +Function +.Fn elftc_string_table_insert +inserts the NUL-terminated string pointed to by argument +.Ar string +into the string table specified by argument +.Ar table , +and returns an offset value usable in ELF data structures. +Multiple insertions of the same content will return the same offset. +The offset returned will remain valid until the next call to +.Fn elftc_string_table_image . +.Pp +Function +.Fn elftc_string_table_lookup +looks up the string referenced by argument +.Ar string +in the string table specified by argument +.Ar table , +and if found, returns the offset associated with the string. +The returned offset will be valid till the next call to function +.Fn elftc_string_table_image . +.Pp +Function +.Fn elftc_string_table_remove +removes the string pointed by argument +.Ar string +from the string table referenced by argument +.Ar table , +if it is present in the string table. +.Pp +Function +.Fn elftc_string_table_to_string +returns a pointer to the NUL-terminated string residing at argument +.Ar offset +in the string table specified by argument +.Ar table . +The value of argument +.Ar offset +should be one returned by a prior call to +.Fn elftc_string_table_insert +or +.Fn elftc_string_table_lookup . +The returned pointer will remain valid until the next call to +.Fn elftc_string_table_insert +or +.Fn elftc_string_table_image . +.Ss Memory Management +The +.Lb libelftc +library manages its own memory allocations. +The application should not free the pointers returned by the string +table functions. +.El +.Sh IMPLEMENTATION NOTES +The current implementation is optimized for the case where strings are +added to a string table, but rarely removed from it. +.Pp +The functions +.Fn elftc_string_table_insert , +.Fn elftc_string_table_lookup , +.Fn elftc_string_table_remove +and +.Fn elftc_string_table_to_string +have O(1) asymptotic behavior. +The function +.Fn elftc_string_table_image +can have O(size) asymptotic behavior, where +.Ar size +denotes the size of the string table. +.Sh RETURN VALUES +Functions +.Fn elftc_string_table_create +and +.Fn elftc_string_table_from_section +return a valid pointer to an opaque structure of type +.Vt Elftc_String_Table +on success, or NULL in case of an error. +.Pp +The function +.Fn elftc_string_table_image +returns a pointer to an in-memory representation of an ELF string +table on success, or NULL in case of an error. +.Pp +Functions +.Fn elftc_string_table_insert +and +.Fn elftc_string_table_lookup +return a non-zero offset on success, or zero in case of an error. +.Pp +Function +.Fn elftc_string_table_remove +returns a positive value on success, or zero in case of an error. +.Pp +Function +.Fn elftc_string_table_to_string +returns a valid pointer on success, or NULL in case of an error. +.Sh SEE ALSO +.Xr dwarf 3 , +.Xr elf 3 , +.Xr elftc 3 diff --git a/contrib/elftoolchain/libelftc/elftc_symbol_table_create.3 b/contrib/elftoolchain/libelftc/elftc_symbol_table_create.3 new file mode 100644 index 000000000000..76f90e9c643d --- /dev/null +++ b/contrib/elftoolchain/libelftc/elftc_symbol_table_create.3 @@ -0,0 +1,529 @@ +.\" Copyright (c) 2012 Joseph Koshy. All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" This software is provided by Joseph Koshy ``as is'' and +.\" any express or implied warranties, including, but not limited to, the +.\" implied warranties of merchantability and fitness for a particular purpose +.\" are disclaimed. in no event shall Joseph Koshy be liable +.\" for any direct, indirect, incidental, special, exemplary, or consequential +.\" damages (including, but not limited to, procurement of substitute goods +.\" or services; loss of use, data, or profits; or business interruption) +.\" however caused and on any theory of liability, whether in contract, strict +.\" liability, or tort (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: elftc_symbol_table_create.3 3182 2015-04-10 16:08:10Z emaste $ +.\" +.Dd December 29, 2012 +.Os +.Dt ELFTC_SYMBOL_TABLE_CREATE 3 +.Sh NAME +.Nm elftc_elf_symbol_table_from_section , +.Nm elftc_symbol_table_count , +.Nm elftc_symbol_table_create , +.Nm elftc_symbol_table_create_nested , +.Nm elftc_symbol_table_delete_name , +.Nm elftc_symbol_table_delete_entry , +.Nm elftc_symbol_table_destroy , +.Nm elftc_symbol_table_insert , +.Nm elftc_symbol_table_iterate , +.Nm elftc_symbol_table_lookup , +.Nm elftc_symbol_table_lookup_value , +.Nm elftc_symbol_table_replace , +.Nm elftc_symbol_table_sort , +.Nm elftc_symbol_table_step +.Nd symbol table management routines +.Sh SYNOPSIS +.In libelftc.h +.Bd -literal +typedef struct _Elftc_Symbol_Table Elftc_Symbol_Table; + +typedef struct _Elftc_Symbol { + ... library private fields ... + const char *sym_name; + uintptr_t sym_value; +} Elftc_Symbol; +.Ed +.Ft size_t +.Fn elftc_symbol_table_count "Elftc_Symbol_Table *table" +.Ft "Elftc_Symbol_Table *" +.Fo elftc_symbol_table_create +.Fa "size_t entrysize" +.Fa "int sizehint" +.Fc +.Ft "Elftc_Symbol_Table *" +.Fo elftc_symbol_table_create_nested +.Fa "Elftc_Symbol_Table *table" +.Fa "int sizehint" +.Fc +.Ft int +.Fo elftc_symbol_table_delete_name +.Fa "Elftc_Symbol_Table *table" +.Fa "const char *name" +.Fc +.Ft int +.Fo elftc_symbol_table_delete_entry +.Fa "Elftc_Symbol_Table *table" +.Fa "Elftc_Symbol *entry" +.Fc +.Ft int +.Fn elftc_symbol_table_destroy "Elftc_Symbol_Table *table" +.Ft "Elftc_Symbol *entry" +.Fo elftc_symbol_table_insert +.Fa "Elftc_Symbol_Table *table" +.Fa "const char *symbolname" +.Fa "int *status" +.Fc +.Ft int +.Fo elftc_symbol_table_iterate +.Fa "Elftc_Symbol_Table *table" +.Fa "int (*iterfn)(Elftc_Symbol *entry, void *cookie)" +.Fa "void *cookie" +.Fc +.Ft "Elftc_Symbol *" +.Fo elftc_symbol_table_lookup +.Fa "Elftc_Symbol_Table *table" +.Fa "const char *symbolname" +.Fc +.Ft "Elftc_Elf_Symbol *" +.Fo elftc_symbol_table_lookup_value +.Fa "Elftc_Symbol_Table *table" +.Fa "uintptr_t value" +.Fa "int searchflags" +.Fc +.Ft int +.Fo elftc_symbol_table_replace +.Fa "Elftc_Symbol_Table *table" +.Fa "Elftc_Symbol *sym1" +.Fa "Elftc_Symbol *sym2" +.Fc +.Ft int +.Fo elftc_symbol_table_sort +.Fa "Elftc_Symbol_Table *table" +.Fa "int (*cmpfn)(Elftc_Symbol *s1, Elftc_Symbol *s2)" +.Fc +.Ft "Elftc_Symbol *" +.Fo elftc_symbol_table_step +.Fa "Elftc_Symbol_Table *table" +.Fa "Elftc_Symbol *cursym" +.Fa "int direction" +.Fc +.Bd -literal +typedef struct _Elftc_Elf_Symbol { + ... library private fields ... + const char *sym_name; + Gelf_Sym sym_elf; +} Elftc_Elf_Symbol; +.Ed +.Ft "Elftc_Symbol_Table *" +.Fo elftc_elf_symbol_table_from_section +.Fa "Elf_Scn *symscn" +.Fa "Elf_Scn *strscn" +.Fc +.Sh DESCRIPTION +This manual page documents convenience routines for handling symbol +tables. +Two flavors of symbol tables are supported: +.Bl -bullet -compact +.It +.Dq Regular +symbol tables supporting insertion, deletion and lookup of entries by +name or by value, sorting of entries, and stepping through entries in +the table's current traversal order. +.It +.Dq ELF-centric +symbol tables support additional operations for conversions to and +from the symbol table format understood by +.Lb libelf . +.El +The default traversal order for a symbol table is the order in which +entries were inserted into it. +This traversal order may be changed using function +.Fn elftc_symbol_table_sort . +.Ss Operations on Regular Symbol Tables +Regular symbol tables use symbols that are subtypes of +.Vt Elftc_Symbol , +as described in the section +.Sx "Structure of a Symbol Table Entry" +below. +.Pp +Function +.Fn elftc_symbol_table_count +returns the number of entries currently in the symbol table. +.Pp +Function +.Fn elftc_symbol_table_create +creates a new, empty symbol table. +The argument +.Ar entrysize +specifies the size of each symbol table entry, as described +in the section +.Sx "Structure of a Symbol Table Entry" +below. +The argument +.Ar sizehint +specifies the expected number of symbol table entries. +If +.Ar sizehint +is zero, an implementation-defined default will be used. +.Pp +Function +.Fn elftc_symbol_table_create_nested +creates a symbol table whose search scope nests inside that of a +parent symbol table. +The argument +.Ar parent +specifies the parent symbol table to nest under. +The argument +.Ar sizehint +specifies the expected number of symbol table entries. +If +.Ar sizehint +is zero, an implementation-defined default will be used instead. +.Pp +The function +.Fn elftc_symbol_table_delete_name +removes the symbol entry named by the argument +.Ar name +from the symbol table specified by argument +.Ar table , +according to the rules described in section +.Sx "Symbol Search Rules" . +.Pp +The function +.Fn elftc_symbol_table_delete_entry +removes the symbol table entry specified by argument +.Ar entry +from the symbol table specified by argument +.Ar table . +.Pp +Function +.Fn elftc_symbol_table_destroy +is used to destroy a symbol table and free up its internal +resources. +.Pp +The function +.Fn elftc_symbol_table_insert +inserts a symbol entry for the name specified by argument +.Ar symbolname +into the symbol table specified by argument +.Ar table , +returning a pointer to a symbol table entry. +The argument +.Ar status +should point to a location that will be updated with one of +the following values: +.Bl -tag -width indent -compact -offset indent +.It Dv ELFTC_INSERT_ERROR +An error occurred during insertion of the symbol. +.It Dv ELFTC_INSERT_EXISTING +The name in argument +.Ar symbolname +was already in the symbol table, and a pointer to the existing +symbol table entry is being returned. +.It Dv ELFTC_INSERT_NEW +A new symbol table entry was allocated for the symbol name +in +.Ar symbolname . +The application will need to initialize the application-specific +fields of the symbol table entry. +.El +Insertion obeys the scoping rules described in section +.Sx "Symbol Search Rules" . +.Pp +The function +.Fn elftc_symbol_table_iterate +iterates over the symbol table specifed by argument +.Ar table , +applying the function pointed to by argument +.Ar iterfn +to each symbol table entry. +The return value from the function +.Ar iterfn +controls progress of the iteration: +.Bl -tag -width indent -compact -offset indent +.It Dv ELFTC_ITERATE_ABORT +Terminates the iteration. +.It Dv ELFTC_ITERATE_CONTINUE +Iteration will continue on to the next element in the symbol table. +.El +Argument +.Ar cookie +will be passed to each invocation of +.Ar iterfn , +and may be used to track persistent state. +The ordering of symbol table entries presented to function +.Ar iterfn +is not defined. +The behavior of the iteration is undefined if +.Ar iterfn +adds or deletes symbol entries from a symbol table that currently +being iterated through. +.Pp +Function +.Fn elftc_symbol_table_lookup +returns the symbol entry corresponding to the name of the symbol +in argument +.Ar symbolname . +.Pp +Function +.Fn elftc_symbol_table_lookup_value +returns the symbol entry that has a +.Va sym_value +field that is closest to the value specified in argument +.Ar value . +The argument +.Ar table +should point to a symbol table, that has been sorted +by a prior call to +.Fn elftc_symbol_table_sort . +The argument +.Ar searchflags +can be a combination of the following flags: +.Bl -tag -width indent -compact -offset indent +.It Dv ELFTC_SEARCH_FORWARD +Find the symbol entry with the next higher value in its +.Va sym_value +field. +.It Dv ELFTC_SEARCH_BACKWARD +Find the symbol entry with next lower value in its +.Va sym_value +field. +.El +If both +.Dv ELFTC_SEARCH_FORWARD +and +.Dv ELFTC_SEARCH_BACKWARD +are specified, then this function will return the symbol that is +closest to the argument +.Ar value . +.Pp +Function +.Fn elftc_symbol_table_replace +moves the symbol table entry pointed to by argument +.Ar sym2 +into the traversal position for the entry pointed to by +.Ar sym1 , +and implicitly deletes the entry pointed to by argument +.Ar sym1 . +Argument +.Ar table +should point to a valid symbol table. +.Pp +Function +.Fn elftc_symbol_table_sort +is used to define an ordering of symbol entries in a symbol +table. +This ordering will be associated with the symbol table till the next +call to function +.Fn elftc_symbol_table_insert , +.Fn elftc_symbol_table_delete_name +or +.Fn elftc_symbol_table_delete_entry . +The argument +.Ar cmpfn +should point to a function that compares two symbol entries pointed +to by +.Ar s1 +and +.Ar s2 +and returns -1, 0, or 1, depending whether +.Ar s1 +is less, equal to, or greater than +.Ar s2 +respectively. +.Pp +Function +.Fn elftc_symbol_table_step +is used to step to the next symbol in a sorted symbol table. +Argument +.Ar table +should point to a symbol table. +The argument +.Ar cursym +specifies the current symbol. +The argument +.Ar direction +specifies the direction to step: +.Bl -tag -width indent -compact -offset ident +.It Dv ELFTC_STEP_NEXT +Return the symbol which follows the argument +.Ar cursym +in the current traversal order. +If argument +.Ar cursym +is NULL, return the first symbol in the current +traversal order. +.It Dv ELFTC_STEP_PREVIOUS +Return the symbol which precedes the argument +.Ar cursym +in the current traversal order. +If argument +.Ar cursym +is NULL, return the last symbol in the current +traversal order. +.El +.Ss Operations on ELF-centric symbol tables +ELF-centric symbol tables use symbols that are subtypes of +.Vt Elftc_Elf_Symbol , +as described in the section +.Sx "Structure of a Symbol Table Entry" +below. +.Pp +In addition to the operations on regular symbol tables listed above, +these symbol tables may be used with the following additional +functions. +.Pp +The function +.Fn elftc_elf_symbol_table_from_section +builds a symbol table from the contents of an ELF section. +The argument +.Ar symscn +should reference an ELF section of type +.Dv SHT_SYMTAB +or +.Dv SHT_DYNSYM . +The argument +.Ar strscn +should reference an ELF section of type +.Dv SHT_STRTAB +containing the string table associated wit section +.Ar symscn . +.Ss Structure of a Symbol Table Entry +The symbol tables managed by +.Lb libelftc +are collections of symbol table entries. +Each entry should be a subtype of one of the +.Vt Elftc_Symbol +or +.Vt Elftc_Elf_Symbol +types. +In other words, each entry should have an +.Vt Elftc_Symbol +or +.Vt Elftc_Elf_Symbol +structure as its first member, before any application specific +fields. +For example: +.Bd -literal -offset indent +struct _MySymbol { + Elftc_Symbol sym_base; + ... other application-specific fields ... +}; +.Ed +.Pp +The size of the combined entry is indicated to the library +at the time of creating a new symbol table. +Applications may then cast the returned pointers from these +routines to the appropriate type: +.Bd -literal -offset indent +struct _MySymbol *mysym; + +mysym = (struct _MySymbol *) elftc_symbol_table_lookup(table, + name); +.Ed +.Pp +The +.Vt Elftc_Symbol +type has two public fields: +.Bl -tag -width ".Va sym_value" -compact -offset indent +.It Va sym_name +Points to a NUL-terminated string containing the symbol's name. +The application should not change the value of this field. +.It Va sym_value +The value associated with this symbol. +This field is entirely under the application's control. +.El +.Pp +The +.Vt Elftc_Elf_Symbol +type has two public fields: +.Bl -tag -width ".Va sym_value" -compact -offset indent +.It Va sym_name +Points to a NUL-terminated string containing the symbol's name. +The application should not change the value of this field. +.It Va sym_elf +A structure of type +.Vt Gelf_Sym +containing ELF symbol information. +This field is entirely under the application's control. +.El +.Ss Symbol Search Rules +During lookups, symbols are looked up first in the symbol table passed in +to the +.Fn elftc_symbol_table_lookup +function. +If the specified symbol is not found, and if the symbol table has a +parent, then the search continues recursively up the chain of parent +symbol tables till either a matching symbol is found or till there are +no more parent symbol tables to search in. +.Pp +Insertions and deletion only work on the specified symbol table and +do not recurse into parent symbol tables. +.Ss Memory Management +The +.Lb libelftc +manages its memory allocations. +Applications should not free the pointers returned by the +API documented in this manual page. +.Sh RETURN VALUES +Function +.Fn elftc_symbol_table_count +returns a count of the number of symbol table entries as an unsigned +value. +.Pp +Functions +.Fn elftc_symbol_table_create , +.Fn elftc_symbol_table_create_nested +and +.Fn elftc_symbol_table_from_section +return a pointer to an opaque structure of type +.Vt Elftc_Symbol_Table +on success, or return NULL in case of an error. +.Pp +Functions +.Fn elftc_symbol_table_delete_name , +.Fn elftc_symbol_table_delete_name +.Fn elftc_symbol_table_destroy , +.Fn elftc_symbol_table_replace +and +.Fn elftc_symbol_table_sort +return a non-zero value on success, or return zero in case of an error. +.Pp +Functions +.Fn elftc_symbol_table_insert , +.Fn elftc_symbol_table_lookup +and +.Fn elftc_symbol_table_lookup_value +return a pointer to a structure that is a subtype of +.Vt Elftc_Symbol +on success, or return NULL in case of an error. +.Pp +The function +.Fn elftc_symbol_table_step +return a pointer to a structure that is a subtype of +.Vt Elftc_Symbol +on success. +The function returns NULL if there are no more elements in the +specified traversal direction. +.Pp +The function +.Fn elftc_symbol_table_iterate +returns +.Dv ELFTC_ITERATE_SUCCESS +if the symbol table was successfully traversed, or +.Dv ELFTC_ITERATE_ABORT +in case the iteration function aborted the traversal. +.Sh SEE ALSO +.Xr dwarf 3 , +.Xr elf 3 , +.Xr elftc 3 diff --git a/contrib/elftoolchain/libelftc/elftc_version.3 b/contrib/elftoolchain/libelftc/elftc_version.3 new file mode 100644 index 000000000000..f7018a50d087 --- /dev/null +++ b/contrib/elftoolchain/libelftc/elftc_version.3 @@ -0,0 +1,79 @@ +.\" Copyright (c) 2011,2012 Joseph Koshy. All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" This software is provided by Joseph Koshy ``as is'' and +.\" any express or implied warranties, including, but not limited to, the +.\" implied warranties of merchantability and fitness for a particular purpose +.\" are disclaimed. in no event shall Joseph Koshy be liable +.\" for any direct, indirect, incidental, special, exemplary, or consequential +.\" damages (including, but not limited to, procurement of substitute goods +.\" or services; loss of use, data, or profits; or business interruption) +.\" however caused and on any theory of liability, whether in contract, strict +.\" liability, or tort (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: elftc_version.3 2828 2012-12-30 04:41:27Z jkoshy $ +.\" +.Dd December 30, 2012 +.Os +.Dt ELFTC_VERSION 3 +.Sh NAME +.Nm elftc_version +.Nd return a project-wide version identifier string +.Sh LIBRARY +.Lb libelftc +.Sh SYNOPSIS +.In libelftc.h +.Ft const char * +.Fn elftc_version void +.Sh DESCRIPTION +Function +.Fn elftc_version +returns a project-wide identifier string that encodes the source +revision of the project source tree. +.Pp +The returned identifier has four space-separated fields: +.Bl -tag -width ".Em Project Branch" +.It Em "Project-Name" +This is always +.Dq elftoolchain . +.It Em "Project-Branch" +The branch name for the project source tree. +.It Em "Build-OS" +The operating system that the tool chain was compiled for. +.It Em "Version-Number" +A tree-wide version number extracted from the version control +system in use. +.El +.Sh RETURN VALUE +Function +.Fn elftc_program_version +returns a pointer to an internal character buffer. +.Sh EXAMPLES +To retrieve and print the current toolchain version identifier, use: +.Bd -literal -offset indent +#include <sys/types.h> +#include <libelftc.h> + +(void) printf("%s\en", elftc_version()); +.Ed +.Pp +On the HEAD branch of the project's sources, when checked out using +Subversion and compiled on a NetBSD host, this would print: +.D1 Dq elftoolchain HEAD NetBSD svn: Ns Em REVINFO +where +.Em REVINFO +would be the current revision information for the project source tree. +.Sh ERRORS +Function +.Fn elftc_program_version +always succeeds. diff --git a/contrib/elftoolchain/libelftc/libelftc.h b/contrib/elftoolchain/libelftc/libelftc.h new file mode 100644 index 000000000000..062db318c4d2 --- /dev/null +++ b/contrib/elftoolchain/libelftc/libelftc.h @@ -0,0 +1,96 @@ +/*- + * Copyright (c) 2009 Kai Wang + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer + * in this position and unchanged. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * $FreeBSD: users/kaiwang27/elftc/libelftc.h 392 2009-05-31 19:17:46Z kaiwang27 $ + * $Id: libelftc.h 3174 2015-03-27 17:13:41Z emaste $ + */ + +#ifndef _LIBELFTC_H_ +#define _LIBELFTC_H_ + +#include <sys/stat.h> + +#include <libelf.h> + +/* + * Types meant to be opaque to the consumers of these APIs. + */ +typedef struct _Elftc_Bfd_Target Elftc_Bfd_Target; +typedef struct _Elftc_String_Table Elftc_String_Table; + +/* Target types. */ +typedef enum { + ETF_NONE, + ETF_ELF, + ETF_BINARY, + ETF_SREC, + ETF_IHEX +} Elftc_Bfd_Target_Flavor; + +/* + * Demangler flags. + */ + +/* Name mangling style. */ +#define ELFTC_DEM_UNKNOWN 0x00000000U /* Not specified. */ +#define ELFTC_DEM_ARM 0x00000001U /* C++ Ann. Ref. Manual. */ +#define ELFTC_DEM_GNU2 0x00000002U /* GNU version 2. */ +#define ELFTC_DEM_GNU3 0x00000004U /* GNU version 3. */ + +/* Demangling behaviour control. */ +#define ELFTC_DEM_NOPARAM 0x00010000U + +#ifdef __cplusplus +extern "C" { +#endif +Elftc_Bfd_Target *elftc_bfd_find_target(const char *_tgt_name); +Elftc_Bfd_Target_Flavor elftc_bfd_target_flavor(Elftc_Bfd_Target *_tgt); +unsigned int elftc_bfd_target_byteorder(Elftc_Bfd_Target *_tgt); +unsigned int elftc_bfd_target_class(Elftc_Bfd_Target *_tgt); +unsigned int elftc_bfd_target_machine(Elftc_Bfd_Target *_tgt); +int elftc_copyfile(int _srcfd, int _dstfd); +int elftc_demangle(const char *_mangledname, char *_buffer, + size_t _bufsize, unsigned int _flags); +int elftc_set_timestamps(const char *_filename, struct stat *_sb); +Elftc_String_Table *elftc_string_table_create(int _hint); +void elftc_string_table_destroy(Elftc_String_Table *_table); +Elftc_String_Table *elftc_string_table_from_section(Elf_Scn *_scn, + int _hint); +const char *elftc_string_table_image(Elftc_String_Table *_table, + size_t *_sz); +size_t elftc_string_table_insert(Elftc_String_Table *_table, + const char *_string); +size_t elftc_string_table_lookup(Elftc_String_Table *_table, + const char *_string); +int elftc_string_table_remove(Elftc_String_Table *_table, + const char *_string); +const char *elftc_string_table_to_string(Elftc_String_Table *_table, + size_t offset); +const char *elftc_version(void); +#ifdef __cplusplus +} +#endif + +#endif /* _LIBELFTC_H_ */ diff --git a/contrib/elftoolchain/libelftc/libelftc_bfdtarget.c b/contrib/elftoolchain/libelftc/libelftc_bfdtarget.c new file mode 100644 index 000000000000..00ba2254fc16 --- /dev/null +++ b/contrib/elftoolchain/libelftc/libelftc_bfdtarget.c @@ -0,0 +1,381 @@ +/*- + * Copyright (c) 2008,2009 Kai Wang + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer + * in this position and unchanged. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/param.h> +#include <libelf.h> +#include <libelftc.h> + +#include "_libelftc.h" + +ELFTC_VCSID("$Id: libelftc_bfdtarget.c 3174 2015-03-27 17:13:41Z emaste $"); + +struct _Elftc_Bfd_Target _libelftc_targets[] = { + + { + .bt_name = "binary", + .bt_type = ETF_BINARY, + }, + + { + .bt_name = "elf32-avr", + .bt_type = ETF_ELF, + .bt_byteorder = ELFDATA2LSB, + .bt_elfclass = ELFCLASS32, + .bt_machine = EM_AVR, + }, + + { + .bt_name = "elf32-big", + .bt_type = ETF_ELF, + .bt_byteorder = ELFDATA2MSB, + .bt_elfclass = ELFCLASS32, + }, + + { + .bt_name = "elf32-bigarm", + .bt_type = ETF_ELF, + .bt_byteorder = ELFDATA2MSB, + .bt_elfclass = ELFCLASS32, + .bt_machine = EM_ARM, + }, + + { + .bt_name = "elf32-bigmips", + .bt_type = ETF_ELF, + .bt_byteorder = ELFDATA2MSB, + .bt_elfclass = ELFCLASS32, + .bt_machine = EM_MIPS, + }, + + { + .bt_name = "elf32-i386", + .bt_type = ETF_ELF, + .bt_byteorder = ELFDATA2LSB, + .bt_elfclass = ELFCLASS32, + .bt_machine = EM_386, + }, + + { + .bt_name = "elf32-i386-freebsd", + .bt_type = ETF_ELF, + .bt_byteorder = ELFDATA2LSB, + .bt_elfclass = ELFCLASS32, + .bt_machine = EM_386, + .bt_osabi = ELFOSABI_FREEBSD, + }, + + { + .bt_name = "elf32-ia64-big", + .bt_type = ETF_ELF, + .bt_byteorder = ELFDATA2MSB, + .bt_elfclass = ELFCLASS32, + .bt_machine = EM_IA_64, + }, + + { + .bt_name = "elf32-little", + .bt_type = ETF_ELF, + .bt_byteorder = ELFDATA2LSB, + .bt_elfclass = ELFCLASS32, + }, + + { + .bt_name = "elf32-littlearm", + .bt_type = ETF_ELF, + .bt_byteorder = ELFDATA2LSB, + .bt_elfclass = ELFCLASS32, + .bt_machine = EM_ARM, + }, + + { + .bt_name = "elf32-littlemips", + .bt_type = ETF_ELF, + .bt_byteorder = ELFDATA2LSB, + .bt_elfclass = ELFCLASS32, + .bt_machine = EM_MIPS, + }, + + { + .bt_name = "elf32-powerpc", + .bt_type = ETF_ELF, + .bt_byteorder = ELFDATA2MSB, + .bt_elfclass = ELFCLASS32, + .bt_machine = EM_PPC, + }, + + { + .bt_name = "elf32-powerpcle", + .bt_type = ETF_ELF, + .bt_byteorder = ELFDATA2LSB, + .bt_elfclass = ELFCLASS32, + .bt_machine = EM_PPC, + }, + + { + .bt_name = "elf32-sh", + .bt_type = ETF_ELF, + .bt_byteorder = ELFDATA2MSB, + .bt_elfclass = ELFCLASS32, + .bt_machine = EM_SH, + }, + + { + .bt_name = "elf32-shl", + .bt_type = ETF_ELF, + .bt_byteorder = ELFDATA2LSB, + .bt_elfclass = ELFCLASS32, + .bt_machine = EM_SH, + }, + + { + .bt_name = "elf32-sh-nbsd", + .bt_type = ETF_ELF, + .bt_byteorder = ELFDATA2MSB, + .bt_elfclass = ELFCLASS32, + .bt_machine = EM_SH, + .bt_osabi = ELFOSABI_NETBSD, + }, + + { + .bt_name = "elf32-shl-nbsd", + .bt_type = ETF_ELF, + .bt_byteorder = ELFDATA2LSB, + .bt_elfclass = ELFCLASS32, + .bt_machine = EM_SH, + .bt_osabi = ELFOSABI_NETBSD, + }, + + { + .bt_name = "elf32-shbig-linux", + .bt_type = ETF_ELF, + .bt_byteorder = ELFDATA2MSB, + .bt_elfclass = ELFCLASS32, + .bt_machine = EM_SH, + .bt_osabi = ELFOSABI_LINUX, + }, + + { + .bt_name = "elf32-sh-linux", + .bt_type = ETF_ELF, + .bt_byteorder = ELFDATA2LSB, + .bt_elfclass = ELFCLASS32, + .bt_machine = EM_SH, + .bt_osabi = ELFOSABI_LINUX, + }, + + { + .bt_name = "elf32-sparc", + .bt_type = ETF_ELF, + .bt_byteorder = ELFDATA2MSB, + .bt_elfclass = ELFCLASS32, + .bt_machine = EM_SPARC, + }, + + { + .bt_name = "elf64-alpha", + .bt_type = ETF_ELF, + .bt_byteorder = ELFDATA2LSB, + .bt_elfclass = ELFCLASS64, + .bt_machine = EM_ALPHA, + }, + + { + .bt_name = "elf64-alpha-freebsd", + .bt_type = ETF_ELF, + .bt_byteorder = ELFDATA2LSB, + .bt_elfclass = ELFCLASS64, + .bt_machine = EM_ALPHA, + .bt_osabi = ELFOSABI_FREEBSD + }, + + { + .bt_name = "elf64-big", + .bt_type = ETF_ELF, + .bt_byteorder = ELFDATA2MSB, + .bt_elfclass = ELFCLASS64, + }, + + { + .bt_name = "elf64-bigmips", + .bt_type = ETF_ELF, + .bt_byteorder = ELFDATA2MSB, + .bt_elfclass = ELFCLASS64, + .bt_machine = EM_MIPS, + }, + + { + .bt_name = "elf64-ia64-big", + .bt_type = ETF_ELF, + .bt_byteorder = ELFDATA2MSB, + .bt_elfclass = ELFCLASS64, + .bt_machine = EM_IA_64, + }, + + { + .bt_name = "elf64-ia64-little", + .bt_type = ETF_ELF, + .bt_byteorder = ELFDATA2LSB, + .bt_elfclass = ELFCLASS64, + .bt_machine = EM_IA_64, + }, + + { + .bt_name = "elf64-little", + .bt_type = ETF_ELF, + .bt_byteorder = ELFDATA2LSB, + .bt_elfclass = ELFCLASS64, + }, + + { + .bt_name = "elf64-littlemips", + .bt_type = ETF_ELF, + .bt_byteorder = ELFDATA2LSB, + .bt_elfclass = ELFCLASS64, + .bt_machine = EM_MIPS, + }, + + { + .bt_name = "elf64-powerpc", + .bt_type = ETF_ELF, + .bt_byteorder = ELFDATA2MSB, + .bt_elfclass = ELFCLASS64, + .bt_machine = EM_PPC64, + }, + + { + .bt_name = "elf64-powerpcle", + .bt_type = ETF_ELF, + .bt_byteorder = ELFDATA2LSB, + .bt_elfclass = ELFCLASS64, + .bt_machine = EM_PPC64, + }, + + { + .bt_name = "elf64-sh64", + .bt_type = ETF_ELF, + .bt_byteorder = ELFDATA2MSB, + .bt_elfclass = ELFCLASS64, + .bt_machine = EM_SH, + }, + + { + .bt_name = "elf64-sh64l", + .bt_type = ETF_ELF, + .bt_byteorder = ELFDATA2LSB, + .bt_elfclass = ELFCLASS64, + .bt_machine = EM_SH, + }, + + { + .bt_name = "elf64-sh64-nbsd", + .bt_type = ETF_ELF, + .bt_byteorder = ELFDATA2MSB, + .bt_elfclass = ELFCLASS64, + .bt_machine = EM_SH, + .bt_osabi = ELFOSABI_NETBSD, + }, + + { + .bt_name = "elf64-sh64l-nbsd", + .bt_type = ETF_ELF, + .bt_byteorder = ELFDATA2LSB, + .bt_elfclass = ELFCLASS64, + .bt_machine = EM_SH, + .bt_osabi = ELFOSABI_NETBSD, + }, + + { + .bt_name = "elf64-sh64big-linux", + .bt_type = ETF_ELF, + .bt_byteorder = ELFDATA2MSB, + .bt_elfclass = ELFCLASS64, + .bt_machine = EM_SH, + .bt_osabi = ELFOSABI_LINUX, + }, + + { + .bt_name = "elf64-sh64-linux", + .bt_type = ETF_ELF, + .bt_byteorder = ELFDATA2LSB, + .bt_elfclass = ELFCLASS64, + .bt_machine = EM_SH, + .bt_osabi = ELFOSABI_LINUX, + }, + + { + .bt_name = "elf64-sparc", + .bt_type = ETF_ELF, + .bt_byteorder = ELFDATA2MSB, + .bt_elfclass = ELFCLASS64, + .bt_machine = EM_SPARCV9, + }, + + { + .bt_name = "elf64-sparc-freebsd", + .bt_type = ETF_ELF, + .bt_byteorder = ELFDATA2MSB, + .bt_elfclass = ELFCLASS64, + .bt_machine = EM_SPARCV9, + .bt_osabi = ELFOSABI_FREEBSD + }, + + { + .bt_name = "elf64-x86-64", + .bt_type = ETF_ELF, + .bt_byteorder = ELFDATA2LSB, + .bt_elfclass = ELFCLASS64, + .bt_machine = EM_X86_64, + }, + + { + .bt_name = "elf64-x86-64-freebsd", + .bt_type = ETF_ELF, + .bt_byteorder = ELFDATA2LSB, + .bt_elfclass = ELFCLASS64, + .bt_machine = EM_X86_64, + .bt_osabi = ELFOSABI_FREEBSD + }, + + { + .bt_name = "ihex", + .bt_type = ETF_IHEX, + }, + + { + .bt_name = "srec", + .bt_type = ETF_SREC, + }, + + { + .bt_name = "symbolsrec", + .bt_type = ETF_SREC, + }, + + { + .bt_name = NULL, + .bt_type = ETF_NONE, + }, +}; diff --git a/contrib/elftoolchain/libelftc/libelftc_dem_arm.c b/contrib/elftoolchain/libelftc/libelftc_dem_arm.c new file mode 100644 index 000000000000..e850b8b5724d --- /dev/null +++ b/contrib/elftoolchain/libelftc/libelftc_dem_arm.c @@ -0,0 +1,1227 @@ +/*- + * Copyright (c) 2008 Hyogeol Lee <hyogeollee@gmail.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer + * in this position and unchanged. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/types.h> +#include <assert.h> +#include <ctype.h> +#include <errno.h> +#include <libelftc.h> +#include <limits.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "_libelftc.h" + +ELFTC_VCSID("$Id: libelftc_dem_arm.c 2065 2011-10-26 15:24:47Z jkoshy $"); + +/** + * @file cpp_demangle_arm.c + * @brief Decode function name encoding in ARM. + * + * Function name encoding in "The Annotated C++ Reference Manual". + * + * Ref : "The Annotated C++ Reference Manual", Margaet A.Ellis, + * Bjarne Stroustrup, AT&T Bell Laboratories 1990, pp 122-126. + */ + +enum encode_type { + ENCODE_FUNC, ENCODE_OP, ENCODE_OP_CT, ENCODE_OP_DT, ENCODE_OP_USER +}; + +struct cstring { + char *buf; + size_t size; +}; + +struct demangle_data { + bool ptr, ref, cnst, array; + struct cstring array_str; + const char *p; + enum encode_type type; + struct vector_str vec; + struct vector_str arg; +}; + +#define SIMPLE_HASH(x,y) (64 * x + y) +#define CPP_DEMANGLE_ARM_TRY 128 + +static void dest_cstring(struct cstring *); +static void dest_demangle_data(struct demangle_data *); +static bool init_cstring(struct cstring *, size_t); +static bool init_demangle_data(struct demangle_data *); +static bool push_CTDT(const char *, size_t, struct vector_str *); +static bool read_array(struct demangle_data *); +static bool read_class(struct demangle_data *); +static bool read_func(struct demangle_data *); +static bool read_func_name(struct demangle_data *); +static bool read_func_ptr(struct demangle_data *); +static bool read_memptr(struct demangle_data *); +static bool read_op(struct demangle_data *); +static bool read_op_user(struct demangle_data *); +static bool read_qual_name(struct demangle_data *); +static int read_subst(struct demangle_data *); +static int read_subst_iter(struct demangle_data *); +static bool read_type(struct demangle_data *); + +/** + * @brief Decode the input string by the ARM style. + * + * @return New allocated demangled string or NULL if failed. + */ +char * +cpp_demangle_ARM(const char *org) +{ + struct demangle_data d; + size_t arg_begin, arg_len; + unsigned int try; + char *rtn, *arg; + + if (org == NULL) + return (NULL); + + if (init_demangle_data(&d) == false) + return (NULL); + + try = 0; + rtn = NULL; + + d.p = org; + if (read_func_name(&d) == false) + goto clean; + + if (d.type == ENCODE_OP_CT) { + if (push_CTDT("::", 2, &d.vec) == false) + goto clean; + + goto flat; + } + + if (d.type == ENCODE_OP_DT) { + if (push_CTDT("::~", 3, &d.vec) == false) + goto clean; + + goto flat; + } + + if (d.type == ENCODE_OP_USER) + goto flat; + + /* function type */ + if (*d.p != 'F') + goto clean; + ++d.p; + + /* start argument types */ + if (vector_str_push(&d.vec, "(", 1) == false) + goto clean; + + for (;;) { + if (*d.p == 'T') { + const int rtn_subst = read_subst(&d); + + if (rtn_subst == -1) + goto clean; + else if (rtn_subst == 1) + break; + + continue; + } + + if (*d.p == 'N') { + const int rtn_subst_iter = read_subst_iter(&d); + + if (rtn_subst_iter == -1) + goto clean; + else if(rtn_subst_iter == 1) + break; + + continue; + } + + arg_begin = d.vec.size; + + if (read_type(&d) == false) + goto clean; + + if (d.ptr == true) { + if (vector_str_push(&d.vec, "*", 1) == false) + goto clean; + + d.ptr = false; + } + + if (d.ref == true) { + if (vector_str_push(&d.vec, "&", 1) == false) + goto clean; + + d.ref = false; + } + + if (d.cnst == true) { + if (vector_str_push(&d.vec, " const", 6) == false) + goto clean; + + d.cnst = false; + } + + if (d.array == true) { + if (vector_str_push(&d.vec, d.array_str.buf, + d.array_str.size) == false) + goto clean; + + dest_cstring(&d.array_str); + d.array = false; + } + + if (*d.p == '\0') + break; + + if ((arg = vector_str_substr(&d.vec, arg_begin, d.vec.size - 1, + &arg_len)) == NULL) + goto clean; + + if (vector_str_push(&d.arg, arg, arg_len) == false) + goto clean; + + free(arg); + + if (vector_str_push(&d.vec, ", ", 2) == false) + goto clean; + + if (++try > CPP_DEMANGLE_ARM_TRY) + goto clean; + } + + /* end argument types */ + if (vector_str_push(&d.vec, ")", 1) == false) + goto clean; + +flat: + rtn = vector_str_get_flat(&d.vec, NULL); +clean: + dest_demangle_data(&d); + + return (rtn); +} + +/** + * @brief Test input string is encoded by the ARM style. + * + * @return True if input string is encoded by the ARM style. + */ +bool +is_cpp_mangled_ARM(const char *org) +{ + + if (org == NULL) + return (false); + + return (strstr(org, "__") != NULL); +} + +static void +dest_cstring(struct cstring *s) +{ + + if (s == NULL) + return; + + free(s->buf); + s->buf = NULL; + s->size = 0; +} + +static void +dest_demangle_data(struct demangle_data *d) +{ + + if (d != NULL) { + vector_str_dest(&d->arg); + vector_str_dest(&d->vec); + + dest_cstring(&d->array_str); + } +} + +static bool +init_cstring(struct cstring *s, size_t len) +{ + + if (s == NULL || len <= 1) + return (false); + + if ((s->buf = malloc(sizeof(char) * len)) == NULL) + return (false); + + s->size = len - 1; + + return (true); +} + +static bool +init_demangle_data(struct demangle_data *d) +{ + + if (d == NULL) + return (false); + + d->ptr = false; + d->ref = false; + d->cnst = false; + d->array = false; + + d->array_str.buf = NULL; + d->array_str.size = 0; + + d->type = ENCODE_FUNC; + + if (vector_str_init(&d->vec) == false) + return (false); + + if (vector_str_init(&d->arg) == false) { + vector_str_dest(&d->vec); + + return (false); + } + + return (true); +} + +static bool +push_CTDT(const char *s, size_t l, struct vector_str *v) +{ + + if (s == NULL || l == 0 || v == NULL) + return (false); + + if (vector_str_push(v, s, l) == false) + return (false); + + assert(v->size > 1); + if (vector_str_push(v, v->container[v->size - 2], + strlen(v->container[v->size - 2])) == false) + return (false); + + if (vector_str_push(v, "()", 2) == false) + return (false); + + return (true); +} + +static bool +read_array(struct demangle_data *d) +{ + size_t len; + const char *end; + + if (d == NULL || d->p == NULL) + return (false); + + end = d->p; + assert(end != NULL); + + for (;;) { + if (*end == '\0') + return (false); + + if (ELFTC_ISDIGIT(*end) == 0) + break; + + ++end; + } + + if (*end != '_') + return (false); + + len = end - d->p; + assert(len > 0); + + dest_cstring(&d->array_str); + if (init_cstring(&d->array_str, len + 3) == false) + return (false); + + strncpy(d->array_str.buf + 1, d->p, len); + *d->array_str.buf = '['; + *(d->array_str.buf + len + 1) = ']'; + + d->array = true; + d->p = end + 1; + + return (true); +} + +static bool +read_class(struct demangle_data *d) +{ + size_t len; + char *str; + + if (d == NULL) + return (false); + + len = strtol(d->p, &str, 10); + if (len == 0 && (errno == EINVAL || errno == ERANGE)) + return (false); + + assert(len > 0); + assert(str != NULL); + + if (vector_str_push(&d->vec, str, len) == false) + return (false); + + d->p = str + len; + + return (true); +} + +static bool +read_func(struct demangle_data *d) +{ + size_t len; + const char *name; + char *delim; + + if (d == NULL) + return (false); + + assert(d->p != NULL && "d->p (org str) is NULL"); + if ((delim = strstr(d->p, "__")) == NULL) + return (false); + + len = delim - d->p; + assert(len != 0); + + name = d->p; + + d->p = delim + 2; + + if (*d->p == 'Q' && ELFTC_ISDIGIT(*(d->p + 1))) { + ++d->p; + + if (read_qual_name(d) == false) + return (false); + } else if (ELFTC_ISDIGIT(*d->p)) { + if (read_class(d) == false) + return (false); + + if (vector_str_push(&d->vec, "::", 2) == false) + return (false); + } + + if (vector_str_push(&d->vec, name, len) == false) + return (false); + + return (true); +} + +static bool +read_func_name(struct demangle_data *d) +{ + size_t len; + bool rtn; + char *op_name; + + if (d == NULL) + return (false); + + rtn = false; + op_name = NULL; + + assert(d->p != NULL && "d->p (org str) is NULL"); + + if (*d->p == '_' && *(d->p + 1) == '_') { + d->p += 2; + + d->type = ENCODE_OP; + if (read_op(d) == false) + return (false); + + if (d->type == ENCODE_OP_CT || d->type == ENCODE_OP_DT || + d->type == ENCODE_OP_USER) + return (true); + + /* skip "__" */ + d->p += 2; + + /* assume delimiter is removed */ + if (*d->p == 'Q' && ELFTC_ISDIGIT(*(d->p + 1))) { + ++d->p; + + assert(d->vec.size > 0); + + len = strlen(d->vec.container[d->vec.size - 1]); + if ((op_name = malloc(sizeof(char) * (len + 1))) + == NULL) + return (false); + + snprintf(op_name, len + 1, "%s", + d->vec.container[d->vec.size - 1]); + vector_str_pop(&d->vec); + + if (read_qual_name(d) == false) + goto clean; + + if (vector_str_push(&d->vec, "::", 2) == false) + goto clean; + + if (vector_str_push(&d->vec, op_name, len) == false) + goto clean; + + rtn = true; + } else if (ELFTC_ISDIGIT(*d->p)) { + assert(d->vec.size > 0); + + len = strlen(d->vec.container[d->vec.size - 1]); + if ((op_name = malloc(sizeof(char) * (len + 1))) + == NULL) + return (false); + + snprintf(op_name, len + 1, "%s", + d->vec.container[d->vec.size - 1]); + vector_str_pop(&d->vec); + + if (read_class(d) == false) + goto clean; + + if (vector_str_push(&d->vec, "::", 2) == false) + goto clean; + + if (vector_str_push(&d->vec, op_name, len) == false) + goto clean; + + rtn = true; + } + } else + return (read_func(d)); + +clean: + free(op_name); + + return (rtn); +} + +/* Read function ptr type */ +static bool +read_func_ptr(struct demangle_data *d) +{ + struct demangle_data fptr; + size_t arg_len, rtn_len; + char *arg_type, *rtn_type; + int lim; + + if (d == NULL) + return (false); + + if (init_demangle_data(&fptr) == false) + return (false); + + fptr.p = d->p + 1; + lim = 0; + arg_type = NULL; + rtn_type = NULL; + + for (;;) { + if (read_type(&fptr) == false) { + dest_demangle_data(&fptr); + + return (false); + } + + if (fptr.ptr == true) { + if (vector_str_push(&fptr.vec, "*", 1) == false) { + dest_demangle_data(&fptr); + + return (false); + } + + fptr.ptr = false; + } + + if (fptr.ref == true) { + if (vector_str_push(&fptr.vec, "&", 1) == false) { + dest_demangle_data(&fptr); + + return (false); + } + + fptr.ref = false; + } + + if (fptr.cnst == true) { + if (vector_str_push(&fptr.vec, " const", 6) == false) { + dest_demangle_data(&fptr); + + return (false); + } + + fptr.cnst = false; + } + + if (*fptr.p == '_') + break; + + if (vector_str_push(&fptr.vec, ", ", 2) == false) { + dest_demangle_data(&fptr); + + return (false); + } + + if (++lim > CPP_DEMANGLE_ARM_TRY) { + + dest_demangle_data(&fptr); + + return (false); + } + } + + arg_type = vector_str_get_flat(&fptr.vec, &arg_len); + /* skip '_' */ + d->p = fptr.p + 1; + + dest_demangle_data(&fptr); + + if (init_demangle_data(&fptr) == false) { + free(arg_type); + + return (false); + } + + fptr.p = d->p; + lim = 0; + + if (read_type(&fptr) == false) { + free(arg_type); + dest_demangle_data(&fptr); + + return (false); + } + + rtn_type = vector_str_get_flat(&fptr.vec, &rtn_len); + d->p = fptr.p; + + + dest_demangle_data(&fptr); + + if (vector_str_push(&d->vec, rtn_type, rtn_len) == false) { + free(rtn_type); + free(arg_type); + + return (false); + } + + free(rtn_type); + + if (vector_str_push(&d->vec, " (*)(", 5) == false) { + free(arg_type); + + return (false); + } + + if (vector_str_push(&d->vec, arg_type, arg_len) == false) { + free(arg_type); + + return (false); + } + + free(arg_type); + + return (vector_str_push(&d->vec, ")", 1)); +} + +static bool +read_memptr(struct demangle_data *d) +{ + struct demangle_data mptr; + size_t len; + bool rtn; + char *mptr_str; + + if (d == NULL || d->p == NULL) + return (false); + + if (init_demangle_data(&mptr) == false) + return (false); + + rtn = false; + mptr_str = NULL; + + mptr.p = d->p; + if (*mptr.p == 'Q') { + ++mptr.p; + + if (read_qual_name(&mptr) == false) + goto clean; + } else { + if (read_class(&mptr) == false) + goto clean; + } + + d->p = mptr.p; + + if ((mptr_str = vector_str_get_flat(&mptr.vec, &len)) == NULL) + goto clean; + + if (vector_str_push(&d->vec, mptr_str, len) == false) + goto clean; + + if (vector_str_push(&d->vec, "::*", 3) == false) + goto clean; + + rtn = true; +clean: + free(mptr_str); + dest_demangle_data(&mptr); + + return (rtn); +} + +static bool +read_op(struct demangle_data *d) +{ + + if (d == NULL) + return (false); + + assert(d->p != NULL && "d->p (org str) is NULL"); + + switch (SIMPLE_HASH(*(d->p), *(d->p+1))) { + case SIMPLE_HASH('m', 'l') : + d->p += 2; + return (vector_str_push(&d->vec, "operator*", 9)); + case SIMPLE_HASH('d', 'v') : + d->p += 2; + return (vector_str_push(&d->vec, "operator/", 9)); + case SIMPLE_HASH('m', 'd') : + d->p += 2; + return (vector_str_push(&d->vec, "operator%", 9)); + case SIMPLE_HASH('p', 'l') : + d->p += 2; + return (vector_str_push(&d->vec, "operator+", 9)); + case SIMPLE_HASH('m', 'i') : + d->p += 2; + return (vector_str_push(&d->vec, "operator-", 9)); + case SIMPLE_HASH('l', 's') : + d->p += 2; + return (vector_str_push(&d->vec, "operator<<", 10)); + case SIMPLE_HASH('r', 's') : + d->p += 2; + return (vector_str_push(&d->vec, "operator>>", 10)); + case SIMPLE_HASH('e', 'q') : + d->p += 2; + return (vector_str_push(&d->vec, "operator==", 10)); + case SIMPLE_HASH('n', 'e') : + d->p += 2; + return (vector_str_push(&d->vec, "operator!=", 10)); + case SIMPLE_HASH('l', 't') : + d->p += 2; + return (vector_str_push(&d->vec, "operator<", 9)); + case SIMPLE_HASH('g', 't') : + d->p += 2; + return (vector_str_push(&d->vec, "operator>", 9)); + case SIMPLE_HASH('l', 'e') : + d->p += 2; + return (vector_str_push(&d->vec, "operator<=", 10)); + case SIMPLE_HASH('g', 'e') : + d->p += 2; + return (vector_str_push(&d->vec, "operator>=", 10)); + case SIMPLE_HASH('a', 'd') : + d->p += 2; + if (*d->p == 'v') { + ++d->p; + return (vector_str_push(&d->vec, "operator/=", + 10)); + } else + return (vector_str_push(&d->vec, "operator&", 9)); + case SIMPLE_HASH('o', 'r') : + d->p += 2; + return (vector_str_push(&d->vec, "operator|", 9)); + case SIMPLE_HASH('e', 'r') : + d->p += 2; + return (vector_str_push(&d->vec, "operator^", 9)); + case SIMPLE_HASH('a', 'a') : + d->p += 2; + if (*d->p == 'd') { + ++d->p; + return (vector_str_push(&d->vec, "operator&=", + 10)); + } else + return (vector_str_push(&d->vec, "operator&&", + 10)); + case SIMPLE_HASH('o', 'o') : + d->p += 2; + return (vector_str_push(&d->vec, "operator||", 10)); + case SIMPLE_HASH('n', 't') : + d->p += 2; + return (vector_str_push(&d->vec, "operator!", 9)); + case SIMPLE_HASH('c', 'o') : + d->p += 2; + return (vector_str_push(&d->vec, "operator~", 9)); + case SIMPLE_HASH('p', 'p') : + d->p += 2; + return (vector_str_push(&d->vec, "operator++", 10)); + case SIMPLE_HASH('m', 'm') : + d->p += 2; + return (vector_str_push(&d->vec, "operator--", 10)); + case SIMPLE_HASH('a', 's') : + d->p += 2; + return (vector_str_push(&d->vec, "operator=", 9)); + case SIMPLE_HASH('r', 'f') : + d->p += 2; + return (vector_str_push(&d->vec, "operator->", 10)); + case SIMPLE_HASH('a', 'p') : + /* apl */ + if (*(d->p + 2) != 'l') + return (false); + + d->p += 3; + return (vector_str_push(&d->vec, "operator+=", 10)); + case SIMPLE_HASH('a', 'm') : + d->p += 2; + if (*d->p == 'i') { + ++d->p; + return (vector_str_push(&d->vec, "operator-=", + 10)); + } else if (*d->p == 'u') { + ++d->p; + return (vector_str_push(&d->vec, "operator*=", + 10)); + } else if (*d->p == 'd') { + ++d->p; + return (vector_str_push(&d->vec, "operator%=", + 10)); + } + + return (false); + case SIMPLE_HASH('a', 'l') : + /* als */ + if (*(d->p + 2) != 's') + return (false); + + d->p += 3; + return (vector_str_push(&d->vec, "operator<<=", 11)); + case SIMPLE_HASH('a', 'r') : + /* ars */ + if (*(d->p + 2) != 's') + return (false); + + d->p += 3; + return (vector_str_push(&d->vec, "operator>>=", 11)); + case SIMPLE_HASH('a', 'o') : + /* aor */ + if (*(d->p + 2) != 'r') + return (false); + + d->p += 3; + return (vector_str_push(&d->vec, "operator|=", 10)); + case SIMPLE_HASH('a', 'e') : + /* aer */ + if (*(d->p + 2) != 'r') + return (false); + + d->p += 3; + return (vector_str_push(&d->vec, "operator^=", 10)); + case SIMPLE_HASH('c', 'm') : + d->p += 2; + return (vector_str_push(&d->vec, "operator,", 9)); + case SIMPLE_HASH('r', 'm') : + d->p += 2; + return (vector_str_push(&d->vec, "operator->*", 11)); + case SIMPLE_HASH('c', 'l') : + d->p += 2; + return (vector_str_push(&d->vec, "()", 2)); + case SIMPLE_HASH('v', 'c') : + d->p += 2; + return (vector_str_push(&d->vec, "[]", 2)); + case SIMPLE_HASH('c', 't') : + d->p += 4; + d->type = ENCODE_OP_CT; + + if (*d->p == 'Q' && ELFTC_ISDIGIT(*(d->p + 1))) { + ++d->p; + + return (read_qual_name(d)); + } else if (ELFTC_ISDIGIT(*d->p)) + return (read_class(d)); + + return (false); + case SIMPLE_HASH('d', 't') : + d->p += 4; + d->type = ENCODE_OP_DT; + + if (*d->p == 'Q' && ELFTC_ISDIGIT(*(d->p + 1))) { + ++d->p; + + return (read_qual_name(d)); + } else if (ELFTC_ISDIGIT(*d->p)) + return (read_class(d)); + + return (false); + case SIMPLE_HASH('n', 'w') : + d->p += 2; + return (vector_str_push(&d->vec, "operator new()", 14)); + case SIMPLE_HASH('d', 'l') : + d->p += 2; + return (vector_str_push(&d->vec, "operator delete()", + 17)); + case SIMPLE_HASH('o', 'p') : + /* __op<TO_TYPE>__<FROM_TYPE> */ + d->p += 2; + + d->type = ENCODE_OP_USER; + + return (read_op_user(d)); + default : + return (false); + }; +} + +static bool +read_op_user(struct demangle_data *d) +{ + struct demangle_data from, to; + size_t from_len, to_len; + bool rtn; + char *from_str, *to_str; + + if (d == NULL) + return (false); + + if (init_demangle_data(&from) == false) + return (false); + + rtn = false; + from_str = NULL; + to_str = NULL; + if (init_demangle_data(&to) == false) + goto clean; + + to.p = d->p; + if (*to.p == 'Q') { + ++to.p; + + if (read_qual_name(&to) == false) + goto clean; + + /* pop last '::' */ + if (vector_str_pop(&to.vec) == false) + goto clean; + } else { + if (read_class(&to) == false) + goto clean; + + /* skip '__' */ + to.p += 2; + } + + if ((to_str = vector_str_get_flat(&to.vec, &to_len)) == NULL) + goto clean; + + from.p = to.p; + if (*from.p == 'Q') { + ++from.p; + + if (read_qual_name(&from) == false) + goto clean; + + /* pop last '::' */ + if (vector_str_pop(&from.vec) == false) + goto clean; + } else { + if (read_class(&from) == false) + goto clean; + } + + if ((from_str = vector_str_get_flat(&from.vec, &from_len)) == NULL) + goto clean; + + if (vector_str_push(&d->vec, from_str, from_len) == false) + goto clean; + + if (vector_str_push(&d->vec, "::operator ", 11) == false) + return (false); + + if (vector_str_push(&d->vec, to_str, to_len) == false) + goto clean; + + rtn = vector_str_push(&d->vec, "()", 2); +clean: + free(to_str); + free(from_str); + dest_demangle_data(&to); + dest_demangle_data(&from); + + return (rtn); +} + +/* single digit + class names */ +static bool +read_qual_name(struct demangle_data *d) +{ + int i; + char num; + + if (d == NULL) + return (false); + + assert(d->p != NULL && "d->p (org str) is NULL"); + assert(*d->p > 48 && *d->p < 58 && "*d->p not in ASCII numeric range"); + + num = *d->p - 48; + + assert(num > 0); + + ++d->p; + for (i = 0; i < num ; ++i) { + if (read_class(d) == false) + return (false); + + if (vector_str_push(&d->vec, "::", 2) == false) + return (false); + } + + if (*d->p != '\0') + d->p = d->p + 2; + + return (true); +} + +/* Return -1 at fail, 0 at success, and 1 at end */ +static int +read_subst(struct demangle_data *d) +{ + size_t idx; + char *str; + + if (d == NULL) + return (-1); + + idx = strtol(d->p + 1, &str, 10); + if (idx == 0 && (errno == EINVAL || errno == ERANGE)) + return (-1); + + assert(idx > 0); + assert(str != NULL); + + d->p = str; + + if (vector_str_push(&d->vec, d->arg.container[idx - 1], + strlen(d->arg.container[idx - 1])) == false) + return (-1); + + if (vector_str_push(&d->arg, d->arg.container[idx - 1], + strlen(d->arg.container[idx - 1])) == false) + return (-1); + + if (*d->p == '\0') + return (1); + + return (0); +} + +static int +read_subst_iter(struct demangle_data *d) +{ + int i; + size_t idx; + char repeat; + char *str; + + if (d == NULL) + return (-1); + + ++d->p; + assert(*d->p > 48 && *d->p < 58 && "*d->p not in ASCII numeric range"); + + repeat = *d->p - 48; + + assert(repeat > 1); + + ++d->p; + + idx = strtol(d->p, &str, 10); + if (idx == 0 && (errno == EINVAL || errno == ERANGE)) + return (-1); + + assert(idx > 0); + assert(str != NULL); + + d->p = str; + + for (i = 0; i < repeat ; ++i) { + if (vector_str_push(&d->vec, d->arg.container[idx - 1], + strlen(d->arg.container[idx - 1])) == false) + return (-1); + + if (vector_str_push(&d->arg, d->arg.container[idx - 1], + strlen(d->arg.container[idx - 1])) == false) + return (-1); + + if (i != repeat - 1 && + vector_str_push(&d->vec, ", ", 2) == false) + return (-1); + } + + if (*d->p == '\0') + return (1); + + return (0); +} + +static bool +read_type(struct demangle_data *d) +{ + + if (d == NULL) + return (false); + + assert(d->p != NULL && "d->p (org str) is NULL"); + + while (*d->p == 'U' || *d->p == 'C' || *d->p == 'V' || *d->p == 'S' || + *d->p == 'P' || *d->p == 'R' || *d->p == 'A' || *d->p == 'F' || + *d->p == 'M') { + switch (*d->p) { + case 'U' : + ++d->p; + + if (vector_str_push(&d->vec, "unsigned ", 9) == false) + return (false); + + break; + case 'C' : + ++d->p; + + if (*d->p == 'P') + d->cnst = true; + else { + if (vector_str_push(&d->vec, "const ", 6) == + false) + return (false); + } + + break; + case 'V' : + ++d->p; + + if (vector_str_push(&d->vec, "volatile ", 9) == false) + return (false); + + break; + case 'S' : + ++d->p; + + if (vector_str_push(&d->vec, "signed ", 7) == false) + return (false); + + break; + case 'P' : + ++d->p; + + if (*d->p == 'F') + return (read_func_ptr(d)); + else + d->ptr = true; + + break; + case 'R' : + ++d->p; + + d->ref = true; + + break; + case 'F' : + break; + case 'A' : + ++d->p; + + if (read_array(d) == false) + return (false); + + break; + case 'M' : + ++d->p; + + if (read_memptr(d) == false) + return (false); + + break; + default : + break; + }; + }; + + if (ELFTC_ISDIGIT(*d->p)) + return (read_class(d)); + + switch (*d->p) { + case 'Q' : + ++d->p; + + return (read_qual_name(d)); + case 'v' : + ++d->p; + + return (vector_str_push(&d->vec, "void", 4)); + case 'c' : + ++d->p; + + return (vector_str_push(&d->vec, "char", 4)); + case 's' : + ++d->p; + + return (vector_str_push(&d->vec, "short", 5)); + case 'i' : + ++d->p; + + return (vector_str_push(&d->vec, "int", 3)); + case 'l' : + ++d->p; + + return (vector_str_push(&d->vec, "long", 4)); + case 'f' : + ++d->p; + + return (vector_str_push(&d->vec, "float", 5)); + case 'd': + ++d->p; + + return (vector_str_push(&d->vec, "double", 6)); + case 'r': + ++d->p; + + return (vector_str_push(&d->vec, "long double", 11)); + case 'e': + ++d->p; + + return (vector_str_push(&d->vec, "...", 3)); + default: + return (false); + }; + + /* NOTREACHED */ + return (false); +} diff --git a/contrib/elftoolchain/libelftc/libelftc_dem_gnu2.c b/contrib/elftoolchain/libelftc/libelftc_dem_gnu2.c new file mode 100644 index 000000000000..4fe141b3b33c --- /dev/null +++ b/contrib/elftoolchain/libelftc/libelftc_dem_gnu2.c @@ -0,0 +1,1376 @@ +/*- + * Copyright (c) 2008 Hyogeol Lee <hyogeollee@gmail.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer + * in this position and unchanged. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/types.h> +#include <assert.h> +#include <ctype.h> +#include <errno.h> +#include <libelftc.h> +#include <limits.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "_libelftc.h" + +ELFTC_VCSID("$Id: libelftc_dem_gnu2.c 2065 2011-10-26 15:24:47Z jkoshy $"); + +/** + * @file cpp_demangle_gnu2.c + * @brief Decode function name encoding in GNU 2. + * + * Function name encoding in GNU 2 based on ARM style. + */ + +enum encode_type { + ENCODE_FUNC, ENCODE_OP, ENCODE_OP_CT, ENCODE_OP_DT, ENCODE_OP_USER, + ENCODE_OP_TF, ENCODE_OP_TI, ENCODE_OP_VT +}; + +struct cstring { + char *buf; + size_t size; +}; + +struct demangle_data { + bool ptr, ref, cnst, array, cnst_fn, class_name; + struct cstring array_str; + const char *p; + enum encode_type type; + struct vector_str vec; + struct vector_str arg; +}; + +#define SIMPLE_HASH(x,y) (64 * x + y) +#define CPP_DEMANGLE_GNU2_TRY 128 + +static void dest_cstring(struct cstring *); +static void dest_demangle_data(struct demangle_data *); +static bool init_cstring(struct cstring *, size_t); +static bool init_demangle_data(struct demangle_data *); +static bool push_CTDT(const char *, size_t, struct vector_str *); +static bool read_array(struct demangle_data *); +static bool read_class(struct demangle_data *); +static bool read_func(struct demangle_data *); +static bool read_func_name(struct demangle_data *); +static bool read_func_ptr(struct demangle_data *); +static bool read_memptr(struct demangle_data *); +static bool read_op(struct demangle_data *); +static bool read_op_user(struct demangle_data *); +static bool read_qual_name(struct demangle_data *); +static int read_subst(struct demangle_data *); +static int read_subst_iter(struct demangle_data *); +static bool read_type(struct demangle_data *); + +/** + * @brief Decode the input string by the GNU 2 style. + * + * @return New allocated demangled string or NULL if failed. + */ +char * +cpp_demangle_gnu2(const char *org) +{ + struct demangle_data d; + size_t arg_begin, arg_len; + unsigned int try; + char *rtn, *arg; + + if (org == NULL) + return (NULL); + + if (init_demangle_data(&d) == false) + return (NULL); + + try = 0; + rtn = NULL; + + d.p = org; + if (read_func_name(&d) == false) + goto clean; + + switch (d.type) { + case ENCODE_FUNC : + case ENCODE_OP : + break; + + case ENCODE_OP_CT : + if (push_CTDT("::", 2, &d.vec) == false) + goto clean; + + break; + case ENCODE_OP_DT : + if (push_CTDT("::~", 3, &d.vec) == false) + goto clean; + + if (vector_str_push(&d.vec, "(void)", 6) == false) + goto clean; + + goto flat; + case ENCODE_OP_USER : + case ENCODE_OP_TF : + case ENCODE_OP_TI : + case ENCODE_OP_VT : + goto flat; + }; + + if (*d.p == 'F') + ++d.p; + else if (*d.p == '\0') { + if (d.class_name == true) { + if (vector_str_push(&d.vec, "(void)", 6) == false) + goto clean; + + goto flat; + } else + goto clean; + } + + /* start argument types */ + if (vector_str_push(&d.vec, "(", 1) == false) + goto clean; + + for (;;) { + if (*d.p == 'T') { + const int rtn_subst = read_subst(&d); + + if (rtn_subst == -1) + goto clean; + else if (rtn_subst == 1) + break; + + continue; + } + + if (*d.p == 'N') { + const int rtn_subst_iter = read_subst_iter(&d); + + if (rtn_subst_iter == -1) + goto clean; + else if(rtn_subst_iter == 1) + break; + + continue; + } + + arg_begin = d.vec.size; + + if (read_type(&d) == false) + goto clean; + + if (d.ptr == true) { + if (vector_str_push(&d.vec, "*", 1) == false) + goto clean; + + d.ptr = false; + } + + if (d.ref == true) { + if (vector_str_push(&d.vec, "&", 1) == false) + goto clean; + + d.ref = false; + } + + if (d.cnst == true) { + if (vector_str_push(&d.vec, " const", 6) == false) + goto clean; + + d.cnst = false; + } + + if (d.array == true) { + if (vector_str_push(&d.vec, d.array_str.buf, + d.array_str.size) == false) + goto clean; + + dest_cstring(&d.array_str); + d.array = false; + } + + if (*d.p == '\0') + break; + + if ((arg = vector_str_substr(&d.vec, arg_begin, d.vec.size - 1, + &arg_len)) == NULL) + goto clean; + + if (vector_str_push(&d.arg, arg, arg_len) == false) + goto clean; + + free(arg); + + if (vector_str_push(&d.vec, ", ", 2) == false) + goto clean; + + if (++try > CPP_DEMANGLE_GNU2_TRY) + goto clean; + } + + /* end argument types */ + if (vector_str_push(&d.vec, ")", 1) == false) + goto clean; +flat: + if (d.cnst_fn == true && vector_str_push(&d.vec, " const", 6) == false) + goto clean; + + rtn = vector_str_get_flat(&d.vec, NULL); +clean: + dest_demangle_data(&d); + + return (rtn); +} + +/** + * @brief Test input string is encoded by the GNU 2 style. + * + * @return True if input string is encoded by the GNU 2 style. + */ +bool +is_cpp_mangled_gnu2(const char *org) +{ + char *str; + bool rtn = false; + + if (org == NULL) + return (false); + + /* search valid text to end */ + str = strstr(org, "__"); + while (str != NULL) { + if (*(str + 2) != '\0') { + if (*(str + 2) == 'C' || + *(str + 2) == 'F' || + *(str + 2) == 'Q' || + ELFTC_ISDIGIT(*(str + 2))) { + rtn |= true; + + break; + } + + if (*(str + 3) != '\0') { + switch (SIMPLE_HASH(*(str + 2), *(str + 3))) { + case SIMPLE_HASH('m', 'l') : + case SIMPLE_HASH('d', 'v') : + case SIMPLE_HASH('m', 'd') : + case SIMPLE_HASH('p', 'l') : + case SIMPLE_HASH('m', 'i') : + case SIMPLE_HASH('l', 's') : + case SIMPLE_HASH('r', 's') : + case SIMPLE_HASH('e', 'q') : + case SIMPLE_HASH('n', 'e') : + case SIMPLE_HASH('l', 't') : + case SIMPLE_HASH('g', 't') : + case SIMPLE_HASH('l', 'e') : + case SIMPLE_HASH('g', 'e') : + case SIMPLE_HASH('a', 'd') : + case SIMPLE_HASH('o', 'r') : + case SIMPLE_HASH('e', 'r') : + case SIMPLE_HASH('a', 'a') : + case SIMPLE_HASH('o', 'o') : + case SIMPLE_HASH('n', 't') : + case SIMPLE_HASH('c', 'o') : + case SIMPLE_HASH('p', 'p') : + case SIMPLE_HASH('m', 'm') : + case SIMPLE_HASH('a', 's') : + case SIMPLE_HASH('r', 'f') : + case SIMPLE_HASH('a', 'p') : + case SIMPLE_HASH('a', 'm') : + case SIMPLE_HASH('a', 'l') : + case SIMPLE_HASH('a', 'r') : + case SIMPLE_HASH('a', 'o') : + case SIMPLE_HASH('a', 'e') : + case SIMPLE_HASH('c', 'm') : + case SIMPLE_HASH('r', 'm') : + case SIMPLE_HASH('c', 'l') : + case SIMPLE_HASH('v', 'c') : + case SIMPLE_HASH('n', 'w') : + case SIMPLE_HASH('d', 'l') : + case SIMPLE_HASH('o', 'p') : + case SIMPLE_HASH('t', 'f') : + case SIMPLE_HASH('t', 'i') : + rtn |= true; + + break; + }; + } + } + + str = strstr(str + 2, "__"); + } + + rtn |= strstr(org, "_$_") != NULL; + rtn |= strstr(org, "_vt$") != NULL; + + return (rtn); +} + +static void +dest_cstring(struct cstring *s) +{ + + if (s == NULL) + return; + + free(s->buf); + s->buf = NULL; + s->size = 0; +} + +static void +dest_demangle_data(struct demangle_data *d) +{ + + if (d != NULL) { + vector_str_dest(&d->arg); + vector_str_dest(&d->vec); + + dest_cstring(&d->array_str); + } +} + +static bool +init_cstring(struct cstring *s, size_t len) +{ + + if (s == NULL || len <= 1) + return (false); + + if ((s->buf = malloc(sizeof(char) * len)) == NULL) + return (false); + + s->size = len - 1; + + return (true); +} + +static bool +init_demangle_data(struct demangle_data *d) +{ + + if (d == NULL) + return (false); + + d->ptr = false; + d->ref = false; + d->cnst = false; + d->array = false; + d->cnst_fn = false; + d->class_name = false; + + d->array_str.buf = NULL; + d->array_str.size = 0; + + d->type = ENCODE_FUNC; + + if (vector_str_init(&d->vec) == false) + return (false); + + if (vector_str_init(&d->arg) == false) { + vector_str_dest(&d->vec); + + return (false); + } + + return (true); +} + +static bool +push_CTDT(const char *s, size_t l, struct vector_str *v) +{ + + if (s == NULL || l == 0 || v == NULL) + return (false); + + if (vector_str_push(v, s, l) == false) + return (false); + + assert(v->size > 1); + + return (vector_str_push(v, v->container[v->size - 2], + strlen(v->container[v->size - 2]))); +} + +static bool +read_array(struct demangle_data *d) +{ + size_t len; + const char *end; + + if (d == NULL || d->p == NULL) + return (false); + + end = d->p; + assert(end != NULL); + + for (;;) { + if (*end == '\0') + return (false); + + if (ELFTC_ISDIGIT(*end) == 0) + break; + + ++end; + } + + if (*end != '_') + return (false); + + len = end - d->p; + assert(len > 0); + + dest_cstring(&d->array_str); + if (init_cstring(&d->array_str, len + 3) == false) + return (false); + + strncpy(d->array_str.buf + 1, d->p, len); + *d->array_str.buf = '['; + *(d->array_str.buf + len + 1) = ']'; + + d->array = true; + d->p = end + 1; + + return (true); +} + +static bool +read_class(struct demangle_data *d) +{ + size_t len; + char *str; + + if (d == NULL) + return (false); + + len = strtol(d->p, &str, 10); + if (len == 0 && (errno == EINVAL || errno == ERANGE)) + return (false); + + assert(len > 0); + assert(str != NULL); + + if (vector_str_push(&d->vec, str, len) == false) + return (false); + + d->p = str + len; + + d->class_name = true; + + return (true); +} + +static bool +read_func(struct demangle_data *d) +{ + size_t len; + const char *name; + char *delim; + + if (d == NULL) + return (false); + + assert(d->p != NULL && "d->p (org str) is NULL"); + if ((delim = strstr(d->p, "__")) == NULL) + return (false); + + len = delim - d->p; + assert(len != 0); + + name = d->p; + + d->p = delim + 2; + + if (*d->p == 'C') { + ++d->p; + + d->cnst_fn = true; + } + + if (*d->p == 'Q' && ELFTC_ISDIGIT(*(d->p + 1))) { + ++d->p; + + if (read_qual_name(d) == false) + return (false); + } else if (ELFTC_ISDIGIT(*d->p)) { + if (read_class(d) == false) + return (false); + + if (vector_str_push(&d->vec, "::", 2) == false) + return (false); + } + + return (vector_str_push(&d->vec, name, len)); +} + +static bool +read_func_name(struct demangle_data *d) +{ + size_t len; + bool rtn; + char *op_name; + + if (d == NULL) + return (false); + + rtn = false; + op_name = NULL; + + assert(d->p != NULL && "d->p (org str) is NULL"); + + if (*d->p == '_' && *(d->p + 1) == '_') { + d->p += 2; + + /* CTOR */ + if (*d->p == 'Q' && ELFTC_ISDIGIT(*(d->p + 1))) { + ++d->p; + d->type = ENCODE_OP_CT; + + if (read_qual_name(d) == false) + return (false); + + return (vector_str_pop(&d->vec)); + } else if (ELFTC_ISDIGIT(*d->p)) { + d->type = ENCODE_OP_CT; + + return (read_class(d)); + } + + d->type = ENCODE_OP; + if (read_op(d) == false) { + /* not good condition, start function name with '__' */ + d->type = ENCODE_FUNC; + + if (vector_str_push(&d->vec, "__", 2) == false) + return (false); + + return (read_func(d)); + } + + if (d->type == ENCODE_OP_USER || + d->type == ENCODE_OP_TF || + d->type == ENCODE_OP_TI) + return (true); + + /* skip "__" */ + d->p += 2; + + if (*d->p == 'C') { + ++d->p; + + d->cnst_fn = true; + } + + /* assume delimiter is removed */ + if (*d->p == 'Q' && ELFTC_ISDIGIT(*(d->p + 1))) { + ++d->p; + + assert(d->vec.size > 0); + + len = strlen(d->vec.container[d->vec.size - 1]); + if ((op_name = malloc(sizeof(char) * (len + 1))) + == NULL) + return (false); + + snprintf(op_name, len + 1, "%s", + d->vec.container[d->vec.size - 1]); + vector_str_pop(&d->vec); + + if (read_qual_name(d) == false) + goto clean; + + if (vector_str_push(&d->vec, "::", 2) == false) + goto clean; + + if (vector_str_push(&d->vec, op_name, len) == false) + goto clean; + + rtn = true; + } else if (ELFTC_ISDIGIT(*d->p)) { + assert(d->vec.size > 0); + + len = strlen(d->vec.container[d->vec.size - 1]); + if ((op_name = malloc(sizeof(char) * (len + 1))) + == NULL) + return (false); + + snprintf(op_name, len + 1, "%s", + d->vec.container[d->vec.size - 1]); + vector_str_pop(&d->vec); + + if (read_class(d) == false) + goto clean; + + if (vector_str_push(&d->vec, "::", 2) == false) + goto clean; + + if (vector_str_push(&d->vec, op_name, len) == false) + goto clean; + + rtn = true; + } + } else if (memcmp(d->p, "_$_", 3) == 0) { + /* DTOR */ + d->p += 3; + d->type = ENCODE_OP_DT; + + if (*d->p == 'Q' && ELFTC_ISDIGIT(*(d->p + 1))) { + ++d->p; + + if (read_qual_name(d) == false) + return (false); + + return (vector_str_pop(&d->vec)); + } else if (ELFTC_ISDIGIT(*d->p)) + return (read_class(d)); + + return (false); + } else if (memcmp(d->p, "_vt$", 4) == 0) { + /* vtable */ + d->p += 4; + d->type = ENCODE_OP_VT; + + if (*d->p == 'Q' && ELFTC_ISDIGIT(*(d->p + 1))) { + ++d->p; + + if (read_qual_name(d) == false) + return (false); + + if (vector_str_pop(&d->vec) == false) + return (false); + } else if (ELFTC_ISDIGIT(*d->p)) { + if (read_class(d) == false) + return (false); + } + + return (vector_str_push(&d->vec, " virtual table", 14)); + } else + return (read_func(d)); +clean: + free(op_name); + + return (rtn); +} + +/* Read function ptr type */ +static bool +read_func_ptr(struct demangle_data *d) +{ + struct demangle_data fptr; + size_t arg_len, rtn_len; + char *arg_type, *rtn_type; + int lim; + + if (d == NULL) + return (false); + + if (init_demangle_data(&fptr) == false) + return (false); + + fptr.p = d->p + 1; + lim = 0; + arg_type = NULL; + rtn_type = NULL; + + for (;;) { + if (read_type(&fptr) == false) { + dest_demangle_data(&fptr); + + return (false); + } + + if (fptr.ptr == true) { + if (vector_str_push(&fptr.vec, "*", 1) == false) { + dest_demangle_data(&fptr); + + return (false); + } + + fptr.ptr = false; + } + + if (fptr.ref == true) { + if (vector_str_push(&fptr.vec, "&", 1) == false) { + dest_demangle_data(&fptr); + + return (false); + } + + fptr.ref = false; + } + + if (fptr.cnst == true) { + if (vector_str_push(&fptr.vec, " const", 6) == false) { + dest_demangle_data(&fptr); + + return (false); + } + + fptr.cnst = false; + } + + if (*fptr.p == '_') + break; + + if (vector_str_push(&fptr.vec, ", ", 2) == false) { + dest_demangle_data(&fptr); + + return (false); + } + + if (++lim > CPP_DEMANGLE_GNU2_TRY) { + + dest_demangle_data(&fptr); + + return (false); + } + } + + arg_type = vector_str_get_flat(&fptr.vec, &arg_len); + /* skip '_' */ + d->p = fptr.p + 1; + + dest_demangle_data(&fptr); + + if (init_demangle_data(&fptr) == false) { + free(arg_type); + + return (false); + } + + fptr.p = d->p; + lim = 0; + + if (read_type(&fptr) == false) { + free(arg_type); + dest_demangle_data(&fptr); + + return (false); + } + + rtn_type = vector_str_get_flat(&fptr.vec, &rtn_len); + d->p = fptr.p; + + + dest_demangle_data(&fptr); + + if (vector_str_push(&d->vec, rtn_type, rtn_len) == false) { + free(rtn_type); + free(arg_type); + + return (false); + } + + free(rtn_type); + + if (vector_str_push(&d->vec, " (*)(", 5) == false) { + free(arg_type); + + return (false); + } + + if (vector_str_push(&d->vec, arg_type, arg_len) == false) { + free(arg_type); + + return (false); + } + + free(arg_type); + + return (vector_str_push(&d->vec, ")", 1)); +} + +static bool +read_memptr(struct demangle_data *d) +{ + struct demangle_data mptr; + size_t len; + bool rtn; + char *mptr_str; + + if (d == NULL || d->p == NULL) + return (false); + + if (init_demangle_data(&mptr) == false) + return (false); + + rtn = false; + mptr_str = NULL; + + mptr.p = d->p; + if (*mptr.p == 'Q') { + ++mptr.p; + + if (read_qual_name(&mptr) == false) + goto clean; + } else if (read_class(&mptr) == false) + goto clean; + + d->p = mptr.p; + + if ((mptr_str = vector_str_get_flat(&mptr.vec, &len)) == NULL) + goto clean; + + if (vector_str_push(&d->vec, mptr_str, len) == false) + goto clean; + + if (vector_str_push(&d->vec, "::*", 3) == false) + goto clean; + + rtn = true; +clean: + free(mptr_str); + dest_demangle_data(&mptr); + + return (rtn); +} + +static bool +read_op(struct demangle_data *d) +{ + + if (d == NULL) + return (false); + + assert(d->p != NULL && "d->p (org str) is NULL"); + + switch (SIMPLE_HASH(*(d->p), *(d->p+1))) { + case SIMPLE_HASH('m', 'l') : + d->p += 2; + return (vector_str_push(&d->vec, "operator*", 9)); + case SIMPLE_HASH('d', 'v') : + d->p += 2; + return (vector_str_push(&d->vec, "operator/", 9)); + case SIMPLE_HASH('m', 'd') : + d->p += 2; + return (vector_str_push(&d->vec, "operator%", 9)); + case SIMPLE_HASH('p', 'l') : + d->p += 2; + return (vector_str_push(&d->vec, "operator+", 9)); + case SIMPLE_HASH('m', 'i') : + d->p += 2; + return (vector_str_push(&d->vec, "operator-", 9)); + case SIMPLE_HASH('l', 's') : + d->p += 2; + return (vector_str_push(&d->vec, "operator<<", 10)); + case SIMPLE_HASH('r', 's') : + d->p += 2; + return (vector_str_push(&d->vec, "operator>>", 10)); + case SIMPLE_HASH('e', 'q') : + d->p += 2; + return (vector_str_push(&d->vec, "operator==", 10)); + case SIMPLE_HASH('n', 'e') : + d->p += 2; + return (vector_str_push(&d->vec, "operator!=", 10)); + case SIMPLE_HASH('l', 't') : + d->p += 2; + return (vector_str_push(&d->vec, "operator<", 9)); + case SIMPLE_HASH('g', 't') : + d->p += 2; + return (vector_str_push(&d->vec, "operator>", 9)); + case SIMPLE_HASH('l', 'e') : + d->p += 2; + return (vector_str_push(&d->vec, "operator<=", 10)); + case SIMPLE_HASH('g', 'e') : + d->p += 2; + return (vector_str_push(&d->vec, "operator>=", 10)); + case SIMPLE_HASH('a', 'd') : + d->p += 2; + if (*d->p == 'v') { + ++d->p; + return (vector_str_push(&d->vec, "operator/=", + 10)); + } else + return (vector_str_push(&d->vec, "operator&", 9)); + case SIMPLE_HASH('o', 'r') : + d->p += 2; + return (vector_str_push(&d->vec, "operator|", 9)); + case SIMPLE_HASH('e', 'r') : + d->p += 2; + return (vector_str_push(&d->vec, "operator^", 9)); + case SIMPLE_HASH('a', 'a') : + d->p += 2; + if (*d->p == 'd') { + ++d->p; + return (vector_str_push(&d->vec, "operator&=", + 10)); + } else + return (vector_str_push(&d->vec, "operator&&", + 10)); + case SIMPLE_HASH('o', 'o') : + d->p += 2; + return (vector_str_push(&d->vec, "operator||", 10)); + case SIMPLE_HASH('n', 't') : + d->p += 2; + return (vector_str_push(&d->vec, "operator!", 9)); + case SIMPLE_HASH('c', 'o') : + d->p += 2; + return (vector_str_push(&d->vec, "operator~", 9)); + case SIMPLE_HASH('p', 'p') : + d->p += 2; + return (vector_str_push(&d->vec, "operator++", 10)); + case SIMPLE_HASH('m', 'm') : + d->p += 2; + return (vector_str_push(&d->vec, "operator--", 10)); + case SIMPLE_HASH('a', 's') : + d->p += 2; + return (vector_str_push(&d->vec, "operator=", 9)); + case SIMPLE_HASH('r', 'f') : + d->p += 2; + return (vector_str_push(&d->vec, "operator->", 10)); + case SIMPLE_HASH('a', 'p') : + /* apl */ + if (*(d->p + 2) != 'l') + return (false); + + d->p += 3; + return (vector_str_push(&d->vec, "operator+=", 10)); + case SIMPLE_HASH('a', 'm') : + d->p += 2; + if (*d->p == 'i') { + ++d->p; + return (vector_str_push(&d->vec, "operator-=", + 10)); + } else if (*d->p == 'u') { + ++d->p; + return (vector_str_push(&d->vec, "operator*=", + 10)); + } else if (*d->p == 'd') { + ++d->p; + return (vector_str_push(&d->vec, "operator%=", + 10)); + } + + return (false); + case SIMPLE_HASH('a', 'l') : + /* als */ + if (*(d->p + 2) != 's') + return (false); + + d->p += 3; + return (vector_str_push(&d->vec, "operator<<=", 11)); + case SIMPLE_HASH('a', 'r') : + /* ars */ + if (*(d->p + 2) != 's') + return (false); + + d->p += 3; + return (vector_str_push(&d->vec, "operator>>=", 11)); + case SIMPLE_HASH('a', 'o') : + /* aor */ + if (*(d->p + 2) != 'r') + return (false); + + d->p += 3; + return (vector_str_push(&d->vec, "operator|=", 10)); + case SIMPLE_HASH('a', 'e') : + /* aer */ + if (*(d->p + 2) != 'r') + return (false); + + d->p += 3; + return (vector_str_push(&d->vec, "operator^=", 10)); + case SIMPLE_HASH('c', 'm') : + d->p += 2; + return (vector_str_push(&d->vec, "operator,", 9)); + case SIMPLE_HASH('r', 'm') : + d->p += 2; + return (vector_str_push(&d->vec, "operator->*", 11)); + case SIMPLE_HASH('c', 'l') : + d->p += 2; + return (vector_str_push(&d->vec, "()", 2)); + case SIMPLE_HASH('v', 'c') : + d->p += 2; + return (vector_str_push(&d->vec, "[]", 2)); + case SIMPLE_HASH('n', 'w') : + d->p += 2; + return (vector_str_push(&d->vec, "operator new()", 14)); + case SIMPLE_HASH('d', 'l') : + d->p += 2; + return (vector_str_push(&d->vec, "operator delete()", + 17)); + case SIMPLE_HASH('o', 'p') : + /* __op<TO_TYPE>__<FROM_TYPE> */ + d->p += 2; + + d->type = ENCODE_OP_USER; + + return (read_op_user(d)); + case SIMPLE_HASH('t', 'f') : + d->p += 2; + d->type = ENCODE_OP_TF; + + if (read_type(d) == false) + return (false); + + return (vector_str_push(&d->vec, " type_info function", 19)); + case SIMPLE_HASH('t', 'i') : + d->p += 2; + d->type = ENCODE_OP_TI; + + if (read_type(d) == false) + return (false); + + return (vector_str_push(&d->vec, " type_info node", 15)); + default : + return (false); + }; +} + +static bool +read_op_user(struct demangle_data *d) +{ + struct demangle_data from, to; + size_t from_len, to_len; + bool rtn; + char *from_str, *to_str; + + if (d == NULL) + return (false); + + if (init_demangle_data(&from) == false) + return (false); + + rtn = false; + from_str = NULL; + to_str = NULL; + if (init_demangle_data(&to) == false) + goto clean; + + to.p = d->p; + if (*to.p == 'Q') { + ++to.p; + + if (read_qual_name(&to) == false) + goto clean; + + /* pop last '::' */ + if (vector_str_pop(&to.vec) == false) + goto clean; + } else { + if (read_class(&to) == false) + goto clean; + + /* skip '__' */ + to.p += 2; + } + + if ((to_str = vector_str_get_flat(&to.vec, &to_len)) == NULL) + goto clean; + + from.p = to.p; + if (*from.p == 'Q') { + ++from.p; + + if (read_qual_name(&from) == false) + goto clean; + + /* pop last '::' */ + if (vector_str_pop(&from.vec) == false) + goto clean; + } else if (read_class(&from) == false) + goto clean; + + if ((from_str = vector_str_get_flat(&from.vec, &from_len)) == NULL) + goto clean; + + if (vector_str_push(&d->vec, from_str, from_len) == false) + goto clean; + + if (vector_str_push(&d->vec, "::operator ", 11) == false) + goto clean; + + if (vector_str_push(&d->vec, to_str, to_len) == false) + goto clean; + + rtn = vector_str_push(&d->vec, "()", 2); +clean: + free(to_str); + free(from_str); + dest_demangle_data(&to); + dest_demangle_data(&from); + + return (rtn); +} + +/* single digit + class names */ +static bool +read_qual_name(struct demangle_data *d) +{ + int i; + char num; + + if (d == NULL) + return (false); + + assert(d->p != NULL && "d->p (org str) is NULL"); + assert(*d->p > 48 && *d->p < 58 && "*d->p not in ASCII numeric range"); + + num = *d->p - 48; + + assert(num > 0); + + ++d->p; + for (i = 0; i < num ; ++i) { + if (read_class(d) == false) + return (false); + + if (vector_str_push(&d->vec, "::", 2) == false) + return (false); + } + + if (*d->p != '\0') + d->p = d->p + 2; + + return (true); +} + +/* Return -1 at fail, 0 at success, and 1 at end */ +static int +read_subst(struct demangle_data *d) +{ + size_t idx; + char *str; + + if (d == NULL) + return (-1); + + idx = strtol(d->p + 1, &str, 10); + if (idx == 0 && (errno == EINVAL || errno == ERANGE)) + return (-1); + + assert(idx > 0); + assert(str != NULL); + + d->p = str; + + if (vector_str_push(&d->vec, d->arg.container[idx - 1], + strlen(d->arg.container[idx - 1])) == false) + return (-1); + + if (vector_str_push(&d->arg, d->arg.container[idx - 1], + strlen(d->arg.container[idx - 1])) == false) + return (-1); + + if (*d->p == '\0') + return (1); + + return (0); +} + +static int +read_subst_iter(struct demangle_data *d) +{ + int i; + size_t idx; + char repeat; + char *str; + + if (d == NULL) + return (-1); + + ++d->p; + assert(*d->p > 48 && *d->p < 58 && "*d->p not in ASCII numeric range"); + + repeat = *d->p - 48; + + assert(repeat > 1); + + ++d->p; + + idx = strtol(d->p, &str, 10); + if (idx == 0 && (errno == EINVAL || errno == ERANGE)) + return (-1); + + assert(idx > 0); + assert(str != NULL); + + d->p = str; + + for (i = 0; i < repeat ; ++i) { + if (vector_str_push(&d->vec, d->arg.container[idx - 1], + strlen(d->arg.container[idx - 1])) == false) + return (-1); + + if (vector_str_push(&d->arg, d->arg.container[idx - 1], + strlen(d->arg.container[idx - 1])) == false) + return (-1); + + if (i != repeat - 1 && + vector_str_push(&d->vec, ", ", 2) == false) + return (-1); + } + + if (*d->p == '\0') + return (1); + + return (0); +} + +static bool +read_type(struct demangle_data *d) +{ + + if (d == NULL) + return (false); + + assert(d->p != NULL && "d->p (org str) is NULL"); + + while (*d->p == 'U' || *d->p == 'C' || *d->p == 'V' || *d->p == 'S' || + *d->p == 'P' || *d->p == 'R' || *d->p == 'A' || *d->p == 'F' || + *d->p == 'M') { + switch (*d->p) { + case 'U' : + ++d->p; + + if (vector_str_push(&d->vec, "unsigned ", 9) == false) + return (false); + + break; + case 'C' : + ++d->p; + + if (*d->p == 'P') + d->cnst = true; + else { + if (vector_str_push(&d->vec, "const ", 6) == + false) + return (false); + } + + break; + case 'V' : + ++d->p; + + if (vector_str_push(&d->vec, "volatile ", 9) == false) + return (false); + + break; + case 'S' : + ++d->p; + + if (vector_str_push(&d->vec, "signed ", 7) == false) + return (false); + + break; + case 'P' : + ++d->p; + + if (*d->p == 'F') + return (read_func_ptr(d)); + else + d->ptr = true; + + break; + case 'R' : + ++d->p; + + d->ref = true; + + break; + case 'F' : + break; + case 'A' : + ++d->p; + + if (read_array(d) == false) + return (false); + + break; + case 'M' : + ++d->p; + + if (read_memptr(d) == false) + return (false); + + break; + default : + break; + }; + }; + + if (ELFTC_ISDIGIT(*d->p)) + return (read_class(d)); + + switch (*d->p) { + case 'Q' : + ++d->p; + + return (read_qual_name(d)); + case 'v' : + ++d->p; + + return (vector_str_push(&d->vec, "void", 4)); + case 'b': + ++d->p; + + return(vector_str_push(&d->vec, "bool", 4)); + case 'c' : + ++d->p; + + return (vector_str_push(&d->vec, "char", 4)); + case 's' : + ++d->p; + + return (vector_str_push(&d->vec, "short", 5)); + case 'i' : + ++d->p; + + return (vector_str_push(&d->vec, "int", 3)); + case 'l' : + ++d->p; + + return (vector_str_push(&d->vec, "long", 4)); + case 'f' : + ++d->p; + + return (vector_str_push(&d->vec, "float", 5)); + case 'd': + ++d->p; + + return (vector_str_push(&d->vec, "double", 6)); + case 'r': + ++d->p; + + return (vector_str_push(&d->vec, "long double", 11)); + case 'e': + ++d->p; + + return (vector_str_push(&d->vec, "...", 3)); + case 'w': + ++d->p; + + return (vector_str_push(&d->vec, "wchar_t", 7)); + case 'x': + ++d->p; + + return (vector_str_push(&d->vec, "long long", 9)); + default: + return (false); + }; + + /* NOTREACHED */ + return (false); +} diff --git a/contrib/elftoolchain/libelftc/libelftc_dem_gnu3.c b/contrib/elftoolchain/libelftc/libelftc_dem_gnu3.c new file mode 100644 index 000000000000..1cdb0e7d4d29 --- /dev/null +++ b/contrib/elftoolchain/libelftc/libelftc_dem_gnu3.c @@ -0,0 +1,3238 @@ +/*- + * Copyright (c) 2007 Hyogeol Lee <hyogeollee@gmail.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer + * in this position and unchanged. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#include <sys/types.h> +#include <assert.h> +#include <ctype.h> +#include <errno.h> +#include <libelftc.h> +#include <limits.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "_libelftc.h" + +ELFTC_VCSID("$Id: libelftc_dem_gnu3.c 3194 2015-05-05 17:55:16Z emaste $"); + +/** + * @file cpp_demangle.c + * @brief Decode IA-64 C++ ABI style implementation. + * + * IA-64 standard ABI(Itanium C++ ABI) references. + * + * http://www.codesourcery.com/cxx-abi/abi.html#mangling \n + * http://www.codesourcery.com/cxx-abi/abi-mangling.html + */ + +enum type_qualifier { + TYPE_PTR, TYPE_REF, TYPE_CMX, TYPE_IMG, TYPE_EXT, TYPE_RST, TYPE_VAT, + TYPE_CST +}; + +struct vector_type_qualifier { + size_t size, capacity; + enum type_qualifier *q_container; + struct vector_str ext_name; +}; + +enum read_cmd { + READ_FAIL, READ_NEST, READ_TMPL, READ_EXPR, READ_EXPL, READ_LOCAL, + READ_TYPE, READ_FUNC, READ_PTRMEM +}; + +struct vector_read_cmd { + size_t size, capacity; + enum read_cmd *r_container; +}; + +struct cpp_demangle_data { + struct vector_str output; /* output string vector */ + struct vector_str output_tmp; + struct vector_str subst; /* substitution string vector */ + struct vector_str tmpl; + struct vector_str class_type; + struct vector_read_cmd cmd; + bool paren; /* parenthesis opened */ + bool pfirst; /* first element of parameter */ + bool mem_rst; /* restrict member function */ + bool mem_vat; /* volatile member function */ + bool mem_cst; /* const member function */ + int func_type; + const char *cur; /* current mangled name ptr */ + const char *last_sname; /* last source name */ +}; + +#define CPP_DEMANGLE_TRY_LIMIT 128 +#define FLOAT_SPRINTF_TRY_LIMIT 5 +#define FLOAT_QUADRUPLE_BYTES 16 +#define FLOAT_EXTENED_BYTES 10 + +#define SIMPLE_HASH(x,y) (64 * x + y) + +static void cpp_demangle_data_dest(struct cpp_demangle_data *); +static int cpp_demangle_data_init(struct cpp_demangle_data *, + const char *); +static int cpp_demangle_get_subst(struct cpp_demangle_data *, size_t); +static int cpp_demangle_get_tmpl_param(struct cpp_demangle_data *, size_t); +static int cpp_demangle_push_fp(struct cpp_demangle_data *, + char *(*)(const char *, size_t)); +static int cpp_demangle_push_str(struct cpp_demangle_data *, const char *, + size_t); +static int cpp_demangle_push_subst(struct cpp_demangle_data *, + const char *, size_t); +static int cpp_demangle_push_subst_v(struct cpp_demangle_data *, + struct vector_str *); +static int cpp_demangle_push_type_qualifier(struct cpp_demangle_data *, + struct vector_type_qualifier *, const char *); +static int cpp_demangle_read_array(struct cpp_demangle_data *); +static int cpp_demangle_read_encoding(struct cpp_demangle_data *); +static int cpp_demangle_read_expr_primary(struct cpp_demangle_data *); +static int cpp_demangle_read_expression(struct cpp_demangle_data *); +static int cpp_demangle_read_expression_binary(struct cpp_demangle_data *, + const char *, size_t); +static int cpp_demangle_read_expression_unary(struct cpp_demangle_data *, + const char *, size_t); +static int cpp_demangle_read_expression_trinary(struct cpp_demangle_data *, + const char *, size_t, const char *, size_t); +static int cpp_demangle_read_function(struct cpp_demangle_data *, int *, + struct vector_type_qualifier *); +static int cpp_demangle_local_source_name(struct cpp_demangle_data *ddata); +static int cpp_demangle_read_local_name(struct cpp_demangle_data *); +static int cpp_demangle_read_name(struct cpp_demangle_data *); +static int cpp_demangle_read_nested_name(struct cpp_demangle_data *); +static int cpp_demangle_read_number(struct cpp_demangle_data *, long *); +static int cpp_demangle_read_nv_offset(struct cpp_demangle_data *); +static int cpp_demangle_read_offset(struct cpp_demangle_data *); +static int cpp_demangle_read_offset_number(struct cpp_demangle_data *); +static int cpp_demangle_read_pointer_to_member(struct cpp_demangle_data *); +static int cpp_demangle_read_sname(struct cpp_demangle_data *); +static int cpp_demangle_read_subst(struct cpp_demangle_data *); +static int cpp_demangle_read_subst_std(struct cpp_demangle_data *); +static int cpp_demangle_read_subst_stdtmpl(struct cpp_demangle_data *, + const char *, size_t); +static int cpp_demangle_read_tmpl_arg(struct cpp_demangle_data *); +static int cpp_demangle_read_tmpl_args(struct cpp_demangle_data *); +static int cpp_demangle_read_tmpl_param(struct cpp_demangle_data *); +static int cpp_demangle_read_type(struct cpp_demangle_data *, int); +static int cpp_demangle_read_uqname(struct cpp_demangle_data *); +static int cpp_demangle_read_v_offset(struct cpp_demangle_data *); +static char *decode_fp_to_double(const char *, size_t); +static char *decode_fp_to_float(const char *, size_t); +static char *decode_fp_to_float128(const char *, size_t); +static char *decode_fp_to_float80(const char *, size_t); +static char *decode_fp_to_long_double(const char *, size_t); +static int hex_to_dec(char); +static void vector_read_cmd_dest(struct vector_read_cmd *); +static int vector_read_cmd_find(struct vector_read_cmd *, enum read_cmd); +static int vector_read_cmd_init(struct vector_read_cmd *); +static int vector_read_cmd_pop(struct vector_read_cmd *); +static int vector_read_cmd_push(struct vector_read_cmd *, enum read_cmd); +static void vector_type_qualifier_dest(struct vector_type_qualifier *); +static int vector_type_qualifier_init(struct vector_type_qualifier *); +static int vector_type_qualifier_push(struct vector_type_qualifier *, + enum type_qualifier); + +static int cpp_demangle_gnu3_push_head; + +/** + * @brief Decode the input string by IA-64 C++ ABI style. + * + * GNU GCC v3 use IA-64 standard ABI. + * @return New allocated demangled string or NULL if failed. + * @todo 1. Testing and more test case. 2. Code cleaning. + */ +char * +cpp_demangle_gnu3(const char *org) +{ + struct cpp_demangle_data ddata; + ssize_t org_len; + unsigned int limit; + char *rtn; + + if (org == NULL || (org_len = strlen(org)) < 2) + return (NULL); + + if (org_len > 11 && !strncmp(org, "_GLOBAL__I_", 11)) { + if ((rtn = malloc(org_len + 19)) == NULL) + return (NULL); + snprintf(rtn, org_len + 19, + "global constructors keyed to %s", org + 11); + return (rtn); + } + + if (org[0] != '_' || org[1] != 'Z') + return (NULL); + + if (!cpp_demangle_data_init(&ddata, org + 2)) + return (NULL); + + cpp_demangle_gnu3_push_head = 0; + rtn = NULL; + + if (!cpp_demangle_read_encoding(&ddata)) + goto clean; + + limit = 0; + while (*ddata.cur != '\0') { + /* + * Breaking at some gcc info at tail. e.g) @@GLIBCXX_3.4 + */ + if (*ddata.cur == '@' && *(ddata.cur + 1) == '@') + break; + if (!cpp_demangle_read_type(&ddata, 1)) + goto clean; + if (limit++ > CPP_DEMANGLE_TRY_LIMIT) + goto clean; + } + + if (ddata.output.size == 0) + goto clean; + if (ddata.paren && !vector_str_push(&ddata.output, ")", 1)) + goto clean; + if (ddata.mem_vat && !vector_str_push(&ddata.output, " volatile", 9)) + goto clean; + if (ddata.mem_cst && !vector_str_push(&ddata.output, " const", 6)) + goto clean; + if (ddata.mem_rst && !vector_str_push(&ddata.output, " restrict", 9)) + goto clean; + + rtn = vector_str_get_flat(&ddata.output, (size_t *) NULL); + +clean: + cpp_demangle_data_dest(&ddata); + + return (rtn); +} + +static void +cpp_demangle_data_dest(struct cpp_demangle_data *d) +{ + + if (d == NULL) + return; + + vector_read_cmd_dest(&d->cmd); + vector_str_dest(&d->class_type); + vector_str_dest(&d->tmpl); + vector_str_dest(&d->subst); + vector_str_dest(&d->output_tmp); + vector_str_dest(&d->output); +} + +static int +cpp_demangle_data_init(struct cpp_demangle_data *d, const char *cur) +{ + + if (d == NULL || cur == NULL) + return (0); + + if (!vector_str_init(&d->output)) + return (0); + if (!vector_str_init(&d->output_tmp)) + goto clean1; + if (!vector_str_init(&d->subst)) + goto clean2; + if (!vector_str_init(&d->tmpl)) + goto clean3; + if (!vector_str_init(&d->class_type)) + goto clean4; + if (!vector_read_cmd_init(&d->cmd)) + goto clean5; + + assert(d->output.container != NULL); + assert(d->output_tmp.container != NULL); + assert(d->subst.container != NULL); + assert(d->tmpl.container != NULL); + assert(d->class_type.container != NULL); + + d->paren = false; + d->pfirst = false; + d->mem_rst = false; + d->mem_vat = false; + d->mem_cst = false; + d->func_type = 0; + d->cur = cur; + d->last_sname = NULL; + + return (1); + +clean5: + vector_str_dest(&d->class_type); +clean4: + vector_str_dest(&d->tmpl); +clean3: + vector_str_dest(&d->subst); +clean2: + vector_str_dest(&d->output_tmp); +clean1: + vector_str_dest(&d->output); + + return (0); +} + +static int +cpp_demangle_push_fp(struct cpp_demangle_data *ddata, + char *(*decoder)(const char *, size_t)) +{ + size_t len; + int rtn; + const char *fp; + char *f; + + if (ddata == NULL || decoder == NULL) + return (0); + + fp = ddata->cur; + while (*ddata->cur != 'E') + ++ddata->cur; + ++ddata->cur; + + if ((f = decoder(fp, ddata->cur - fp)) == NULL) + return (0); + + rtn = 0; + if ((len = strlen(f)) > 0) + rtn = cpp_demangle_push_str(ddata, f, len); + + free(f); + + return (rtn); +} + +static int +cpp_demangle_push_str(struct cpp_demangle_data *ddata, const char *str, + size_t len) +{ + + if (ddata == NULL || str == NULL || len == 0) + return (0); + + if (cpp_demangle_gnu3_push_head > 0) + return (vector_str_push(&ddata->output_tmp, str, len)); + + return (vector_str_push(&ddata->output, str, len)); +} + +static int +cpp_demangle_push_subst(struct cpp_demangle_data *ddata, const char *str, + size_t len) +{ + + if (ddata == NULL || str == NULL || len == 0) + return (0); + + if (!vector_str_find(&ddata->subst, str, len)) + return (vector_str_push(&ddata->subst, str, len)); + + return (1); +} + +static int +cpp_demangle_push_subst_v(struct cpp_demangle_data *ddata, struct vector_str *v) +{ + size_t str_len; + int rtn; + char *str; + + if (ddata == NULL || v == NULL) + return (0); + + if ((str = vector_str_get_flat(v, &str_len)) == NULL) + return (0); + + rtn = cpp_demangle_push_subst(ddata, str, str_len); + + free(str); + + return (rtn); +} + +static int +cpp_demangle_push_type_qualifier(struct cpp_demangle_data *ddata, + struct vector_type_qualifier *v, const char *type_str) +{ + struct vector_str subst_v; + size_t idx, e_idx, e_len; + int rtn; + char *buf; + + if (ddata == NULL || v == NULL) + return (0); + + if ((idx = v->size) == 0) + return (1); + + rtn = 0; + if (type_str != NULL) { + if (!vector_str_init(&subst_v)) + return (0); + if (!vector_str_push(&subst_v, type_str, strlen(type_str))) + goto clean; + } + + e_idx = 0; + while (idx > 0) { + switch (v->q_container[idx - 1]) { + case TYPE_PTR: + if (!cpp_demangle_push_str(ddata, "*", 1)) + goto clean; + if (type_str != NULL) { + if (!vector_str_push(&subst_v, "*", 1)) + goto clean; + if (!cpp_demangle_push_subst_v(ddata, &subst_v)) + goto clean; + } + break; + + case TYPE_REF: + if (!cpp_demangle_push_str(ddata, "&", 1)) + goto clean; + if (type_str != NULL) { + if (!vector_str_push(&subst_v, "&", 1)) + goto clean; + if (!cpp_demangle_push_subst_v(ddata, &subst_v)) + goto clean; + } + break; + + case TYPE_CMX: + if (!cpp_demangle_push_str(ddata, " complex", 8)) + goto clean; + if (type_str != NULL) { + if (!vector_str_push(&subst_v, " complex", 8)) + goto clean; + if (!cpp_demangle_push_subst_v(ddata, &subst_v)) + goto clean; + } + break; + + case TYPE_IMG: + if (!cpp_demangle_push_str(ddata, " imaginary", 10)) + goto clean; + if (type_str != NULL) { + if (!vector_str_push(&subst_v, " imaginary", 10)) + goto clean; + if (!cpp_demangle_push_subst_v(ddata, &subst_v)) + goto clean; + } + break; + + case TYPE_EXT: + if (e_idx > v->ext_name.size - 1) + goto clean; + if ((e_len = strlen(v->ext_name.container[e_idx])) == 0) + goto clean; + if ((buf = malloc(sizeof(char) * (e_len + 1))) == NULL) + goto clean; + + memcpy(buf, " ", 1); + memcpy(buf + 1, v->ext_name.container[e_idx], e_len); + + if (!cpp_demangle_push_str(ddata, buf, e_len + 1)) { + free(buf); + goto clean; + } + + if (type_str != NULL) { + if (!vector_str_push(&subst_v, buf, + e_len + 1)) { + free(buf); + goto clean; + } + if (!cpp_demangle_push_subst_v(ddata, &subst_v)) { + free(buf); + goto clean; + } + } + free(buf); + ++e_idx; + break; + + case TYPE_RST: + if (!cpp_demangle_push_str(ddata, " restrict", 9)) + goto clean; + if (type_str != NULL) { + if (!vector_str_push(&subst_v, " restrict", 9)) + goto clean; + if (!cpp_demangle_push_subst_v(ddata, &subst_v)) + goto clean; + } + break; + + case TYPE_VAT: + if (!cpp_demangle_push_str(ddata, " volatile", 9)) + goto clean; + if (type_str != NULL) { + if (!vector_str_push(&subst_v, " volatile", 9)) + goto clean; + if (!cpp_demangle_push_subst_v(ddata, &subst_v)) + goto clean; + } + break; + + case TYPE_CST: + if (!cpp_demangle_push_str(ddata, " const", 6)) + goto clean; + if (type_str != NULL) { + if (!vector_str_push(&subst_v, " const", 6)) + goto clean; + if (!cpp_demangle_push_subst_v(ddata, &subst_v)) + goto clean; + } + break; + + }; + --idx; + } + + rtn = 1; +clean: + if (type_str != NULL) + vector_str_dest(&subst_v); + + return (rtn); +} + +static int +cpp_demangle_get_subst(struct cpp_demangle_data *ddata, size_t idx) +{ + size_t len; + + if (ddata == NULL || ddata->subst.size <= idx) + return (0); + if ((len = strlen(ddata->subst.container[idx])) == 0) + return (0); + if (!cpp_demangle_push_str(ddata, ddata->subst.container[idx], len)) + return (0); + + /* skip '_' */ + ++ddata->cur; + + return (1); +} + +static int +cpp_demangle_get_tmpl_param(struct cpp_demangle_data *ddata, size_t idx) +{ + size_t len; + + if (ddata == NULL || ddata->tmpl.size <= idx) + return (0); + if ((len = strlen(ddata->tmpl.container[idx])) == 0) + return (0); + if (!cpp_demangle_push_str(ddata, ddata->tmpl.container[idx], len)) + return (0); + + ++ddata->cur; + + return (1); +} + +static int +cpp_demangle_read_array(struct cpp_demangle_data *ddata) +{ + size_t i, num_len, exp_len, p_idx, idx; + const char *num; + char *exp; + + if (ddata == NULL || *(++ddata->cur) == '\0') + return (0); + + if (*ddata->cur == '_') { + if (*(++ddata->cur) == '\0') + return (0); + + if (!cpp_demangle_read_type(ddata, 0)) + return (0); + + if (!cpp_demangle_push_str(ddata, "[]", 2)) + return (0); + } else { + if (ELFTC_ISDIGIT(*ddata->cur) != 0) { + num = ddata->cur; + while (ELFTC_ISDIGIT(*ddata->cur) != 0) + ++ddata->cur; + if (*ddata->cur != '_') + return (0); + num_len = ddata->cur - num; + assert(num_len > 0); + if (*(++ddata->cur) == '\0') + return (0); + if (!cpp_demangle_read_type(ddata, 0)) + return (0); + if (!cpp_demangle_push_str(ddata, "[", 1)) + return (0); + if (!cpp_demangle_push_str(ddata, num, num_len)) + return (0); + if (!cpp_demangle_push_str(ddata, "]", 1)) + return (0); + } else { + p_idx = ddata->output.size; + if (!cpp_demangle_read_expression(ddata)) + return (0); + if ((exp = vector_str_substr(&ddata->output, p_idx, + ddata->output.size - 1, &exp_len)) == NULL) + return (0); + idx = ddata->output.size; + for (i = p_idx; i < idx; ++i) + if (!vector_str_pop(&ddata->output)) { + free(exp); + return (0); + } + if (*ddata->cur != '_') { + free(exp); + return (0); + } + ++ddata->cur; + if (*ddata->cur == '\0') { + free(exp); + return (0); + } + if (!cpp_demangle_read_type(ddata, 0)) { + free(exp); + return (0); + } + if (!cpp_demangle_push_str(ddata, "[", 1)) { + free(exp); + return (0); + } + if (!cpp_demangle_push_str(ddata, exp, exp_len)) { + free(exp); + return (0); + } + if (!cpp_demangle_push_str(ddata, "]", 1)) { + free(exp); + return (0); + } + free(exp); + } + } + + return (1); +} + +static int +cpp_demangle_read_expr_primary(struct cpp_demangle_data *ddata) +{ + const char *num; + + if (ddata == NULL || *(++ddata->cur) == '\0') + return (0); + + if (*ddata->cur == '_' && *(ddata->cur + 1) == 'Z') { + ddata->cur += 2; + if (*ddata->cur == '\0') + return (0); + if (!cpp_demangle_read_encoding(ddata)) + return (0); + ++ddata->cur; + return (1); + } + + switch (*ddata->cur) { + case 'b': + switch (*(++ddata->cur)) { + case '0': + return (cpp_demangle_push_str(ddata, "false", 5)); + case '1': + return (cpp_demangle_push_str(ddata, "true", 4)); + default: + return (0); + }; + + case 'd': + ++ddata->cur; + return (cpp_demangle_push_fp(ddata, decode_fp_to_double)); + + case 'e': + ++ddata->cur; + if (sizeof(long double) == 10) + return (cpp_demangle_push_fp(ddata, + decode_fp_to_double)); + return (cpp_demangle_push_fp(ddata, decode_fp_to_float80)); + + case 'f': + ++ddata->cur; + return (cpp_demangle_push_fp(ddata, decode_fp_to_float)); + + case 'g': + ++ddata->cur; + if (sizeof(long double) == 16) + return (cpp_demangle_push_fp(ddata, + decode_fp_to_double)); + return (cpp_demangle_push_fp(ddata, decode_fp_to_float128)); + + case 'i': + case 'j': + case 'l': + case 'm': + case 'n': + case 's': + case 't': + case 'x': + case 'y': + if (*(++ddata->cur) == 'n') { + if (!cpp_demangle_push_str(ddata, "-", 1)) + return (0); + ++ddata->cur; + } + num = ddata->cur; + while (*ddata->cur != 'E') { + if (!ELFTC_ISDIGIT(*ddata->cur)) + return (0); + ++ddata->cur; + } + ++ddata->cur; + return (cpp_demangle_push_str(ddata, num, ddata->cur - num)); + + default: + return (0); + }; +} + +static int +cpp_demangle_read_expression(struct cpp_demangle_data *ddata) +{ + + if (ddata == NULL || *ddata->cur == '\0') + return (0); + + switch (SIMPLE_HASH(*ddata->cur, *(ddata->cur + 1))) { + case SIMPLE_HASH('s', 't'): + ddata->cur += 2; + return (cpp_demangle_read_type(ddata, 0)); + + case SIMPLE_HASH('s', 'r'): + ddata->cur += 2; + if (!cpp_demangle_read_type(ddata, 0)) + return (0); + if (!cpp_demangle_read_uqname(ddata)) + return (0); + if (*ddata->cur == 'I') + return (cpp_demangle_read_tmpl_args(ddata)); + return (1); + + case SIMPLE_HASH('a', 'a'): + /* operator && */ + ddata->cur += 2; + return (cpp_demangle_read_expression_binary(ddata, "&&", 2)); + + case SIMPLE_HASH('a', 'd'): + /* operator & (unary) */ + ddata->cur += 2; + return (cpp_demangle_read_expression_unary(ddata, "&", 1)); + + case SIMPLE_HASH('a', 'n'): + /* operator & */ + ddata->cur += 2; + return (cpp_demangle_read_expression_binary(ddata, "&", 1)); + + case SIMPLE_HASH('a', 'N'): + /* operator &= */ + ddata->cur += 2; + return (cpp_demangle_read_expression_binary(ddata, "&=", 2)); + + case SIMPLE_HASH('a', 'S'): + /* operator = */ + ddata->cur += 2; + return (cpp_demangle_read_expression_binary(ddata, "=", 1)); + + case SIMPLE_HASH('c', 'l'): + /* operator () */ + ddata->cur += 2; + return (cpp_demangle_read_expression_binary(ddata, "()", 2)); + + case SIMPLE_HASH('c', 'm'): + /* operator , */ + ddata->cur += 2; + return (cpp_demangle_read_expression_binary(ddata, ",", 1)); + + case SIMPLE_HASH('c', 'o'): + /* operator ~ */ + ddata->cur += 2; + return (cpp_demangle_read_expression_binary(ddata, "~", 1)); + + case SIMPLE_HASH('c', 'v'): + /* operator (cast) */ + ddata->cur += 2; + return (cpp_demangle_read_expression_binary(ddata, "(cast)", 6)); + + case SIMPLE_HASH('d', 'a'): + /* operator delete [] */ + ddata->cur += 2; + return (cpp_demangle_read_expression_unary(ddata, "delete []", 9)); + + case SIMPLE_HASH('d', 'e'): + /* operator * (unary) */ + ddata->cur += 2; + return (cpp_demangle_read_expression_unary(ddata, "*", 1)); + + case SIMPLE_HASH('d', 'l'): + /* operator delete */ + ddata->cur += 2; + return (cpp_demangle_read_expression_unary(ddata, "delete", 6)); + + case SIMPLE_HASH('d', 'v'): + /* operator / */ + ddata->cur += 2; + return (cpp_demangle_read_expression_binary(ddata, "/", 1)); + + case SIMPLE_HASH('d', 'V'): + /* operator /= */ + ddata->cur += 2; + return (cpp_demangle_read_expression_binary(ddata, "/=", 2)); + + case SIMPLE_HASH('e', 'o'): + /* operator ^ */ + ddata->cur += 2; + return (cpp_demangle_read_expression_binary(ddata, "^", 1)); + + case SIMPLE_HASH('e', 'O'): + /* operator ^= */ + ddata->cur += 2; + return (cpp_demangle_read_expression_binary(ddata, "^=", 2)); + + case SIMPLE_HASH('e', 'q'): + /* operator == */ + ddata->cur += 2; + return (cpp_demangle_read_expression_binary(ddata, "==", 2)); + + case SIMPLE_HASH('g', 'e'): + /* operator >= */ + ddata->cur += 2; + return (cpp_demangle_read_expression_binary(ddata, ">=", 2)); + + case SIMPLE_HASH('g', 't'): + /* operator > */ + ddata->cur += 2; + return (cpp_demangle_read_expression_binary(ddata, ">", 1)); + + case SIMPLE_HASH('i', 'x'): + /* operator [] */ + ddata->cur += 2; + return (cpp_demangle_read_expression_binary(ddata, "[]", 2)); + + case SIMPLE_HASH('l', 'e'): + /* operator <= */ + ddata->cur += 2; + return (cpp_demangle_read_expression_binary(ddata, "<=", 2)); + + case SIMPLE_HASH('l', 's'): + /* operator << */ + ddata->cur += 2; + return (cpp_demangle_read_expression_binary(ddata, "<<", 2)); + + case SIMPLE_HASH('l', 'S'): + /* operator <<= */ + ddata->cur += 2; + return (cpp_demangle_read_expression_binary(ddata, "<<=", 3)); + + case SIMPLE_HASH('l', 't'): + /* operator < */ + ddata->cur += 2; + return (cpp_demangle_read_expression_binary(ddata, "<", 1)); + + case SIMPLE_HASH('m', 'i'): + /* operator - */ + ddata->cur += 2; + return (cpp_demangle_read_expression_binary(ddata, "-", 1)); + + case SIMPLE_HASH('m', 'I'): + /* operator -= */ + ddata->cur += 2; + return (cpp_demangle_read_expression_binary(ddata, "-=", 2)); + + case SIMPLE_HASH('m', 'l'): + /* operator * */ + ddata->cur += 2; + return (cpp_demangle_read_expression_binary(ddata, "*", 1)); + + case SIMPLE_HASH('m', 'L'): + /* operator *= */ + ddata->cur += 2; + return (cpp_demangle_read_expression_binary(ddata, "*=", 2)); + + case SIMPLE_HASH('m', 'm'): + /* operator -- */ + ddata->cur += 2; + return (cpp_demangle_read_expression_binary(ddata, "--", 2)); + + case SIMPLE_HASH('n', 'a'): + /* operator new[] */ + ddata->cur += 2; + return (cpp_demangle_read_expression_unary(ddata, "new []", 6)); + + case SIMPLE_HASH('n', 'e'): + /* operator != */ + ddata->cur += 2; + return (cpp_demangle_read_expression_binary(ddata, "!=", 2)); + + case SIMPLE_HASH('n', 'g'): + /* operator - (unary) */ + ddata->cur += 2; + return (cpp_demangle_read_expression_unary(ddata, "-", 1)); + + case SIMPLE_HASH('n', 't'): + /* operator ! */ + ddata->cur += 2; + return (cpp_demangle_read_expression_binary(ddata, "!", 1)); + + case SIMPLE_HASH('n', 'w'): + /* operator new */ + ddata->cur += 2; + return (cpp_demangle_read_expression_unary(ddata, "new", 3)); + + case SIMPLE_HASH('o', 'o'): + /* operator || */ + ddata->cur += 2; + return (cpp_demangle_read_expression_binary(ddata, "||", 2)); + + case SIMPLE_HASH('o', 'r'): + /* operator | */ + ddata->cur += 2; + return (cpp_demangle_read_expression_binary(ddata, "|", 1)); + + case SIMPLE_HASH('o', 'R'): + /* operator |= */ + ddata->cur += 2; + return (cpp_demangle_read_expression_binary(ddata, "|=", 2)); + + case SIMPLE_HASH('p', 'l'): + /* operator + */ + ddata->cur += 2; + return (cpp_demangle_read_expression_binary(ddata, "+", 1)); + + case SIMPLE_HASH('p', 'L'): + /* operator += */ + ddata->cur += 2; + return (cpp_demangle_read_expression_binary(ddata, "+=", 2)); + + case SIMPLE_HASH('p', 'm'): + /* operator ->* */ + ddata->cur += 2; + return (cpp_demangle_read_expression_binary(ddata, "->*", 3)); + + case SIMPLE_HASH('p', 'p'): + /* operator ++ */ + ddata->cur += 2; + return (cpp_demangle_read_expression_binary(ddata, "++", 2)); + + case SIMPLE_HASH('p', 's'): + /* operator + (unary) */ + ddata->cur += 2; + return (cpp_demangle_read_expression_unary(ddata, "+", 1)); + + case SIMPLE_HASH('p', 't'): + /* operator -> */ + ddata->cur += 2; + return (cpp_demangle_read_expression_binary(ddata, "->", 2)); + + case SIMPLE_HASH('q', 'u'): + /* operator ? */ + ddata->cur += 2; + return (cpp_demangle_read_expression_trinary(ddata, "?", 1, + ":", 1)); + + case SIMPLE_HASH('r', 'm'): + /* operator % */ + ddata->cur += 2; + return (cpp_demangle_read_expression_binary(ddata, "%", 1)); + + case SIMPLE_HASH('r', 'M'): + /* operator %= */ + ddata->cur += 2; + return (cpp_demangle_read_expression_binary(ddata, "%=", 2)); + + case SIMPLE_HASH('r', 's'): + /* operator >> */ + ddata->cur += 2; + return (cpp_demangle_read_expression_binary(ddata, ">>", 2)); + + case SIMPLE_HASH('r', 'S'): + /* operator >>= */ + ddata->cur += 2; + return (cpp_demangle_read_expression_binary(ddata, ">>=", 3)); + + case SIMPLE_HASH('r', 'z'): + /* operator sizeof */ + ddata->cur += 2; + return (cpp_demangle_read_expression_unary(ddata, "sizeof", 6)); + + case SIMPLE_HASH('s', 'v'): + /* operator sizeof */ + ddata->cur += 2; + return (cpp_demangle_read_expression_unary(ddata, "sizeof", 6)); + }; + + switch (*ddata->cur) { + case 'L': + return (cpp_demangle_read_expr_primary(ddata)); + case 'T': + return (cpp_demangle_read_tmpl_param(ddata)); + }; + + return (0); +} + +static int +cpp_demangle_read_expression_binary(struct cpp_demangle_data *ddata, + const char *name, size_t len) +{ + + if (ddata == NULL || name == NULL || len == 0) + return (0); + if (!cpp_demangle_read_expression(ddata)) + return (0); + if (!cpp_demangle_push_str(ddata, name, len)) + return (0); + + return (cpp_demangle_read_expression(ddata)); +} + +static int +cpp_demangle_read_expression_unary(struct cpp_demangle_data *ddata, + const char *name, size_t len) +{ + + if (ddata == NULL || name == NULL || len == 0) + return (0); + if (!cpp_demangle_read_expression(ddata)) + return (0); + + return (cpp_demangle_push_str(ddata, name, len)); +} + +static int +cpp_demangle_read_expression_trinary(struct cpp_demangle_data *ddata, + const char *name1, size_t len1, const char *name2, size_t len2) +{ + + if (ddata == NULL || name1 == NULL || len1 == 0 || name2 == NULL || + len2 == 0) + return (0); + + if (!cpp_demangle_read_expression(ddata)) + return (0); + if (!cpp_demangle_push_str(ddata, name1, len1)) + return (0); + if (!cpp_demangle_read_expression(ddata)) + return (0); + if (!cpp_demangle_push_str(ddata, name2, len2)) + return (0); + + return (cpp_demangle_read_expression(ddata)); +} + +static int +cpp_demangle_read_function(struct cpp_demangle_data *ddata, int *ext_c, + struct vector_type_qualifier *v) +{ + size_t class_type_size, class_type_len, limit; + const char *class_type; + + if (ddata == NULL || *ddata->cur != 'F' || v == NULL) + return (0); + + ++ddata->cur; + if (*ddata->cur == 'Y') { + if (ext_c != NULL) + *ext_c = 1; + ++ddata->cur; + } + if (!cpp_demangle_read_type(ddata, 0)) + return (0); + if (*ddata->cur != 'E') { + if (!cpp_demangle_push_str(ddata, "(", 1)) + return (0); + if (vector_read_cmd_find(&ddata->cmd, READ_PTRMEM)) { + if ((class_type_size = ddata->class_type.size) == 0) + return (0); + class_type = + ddata->class_type.container[class_type_size - 1]; + if (class_type == NULL) + return (0); + if ((class_type_len = strlen(class_type)) == 0) + return (0); + if (!cpp_demangle_push_str(ddata, class_type, + class_type_len)) + return (0); + if (!cpp_demangle_push_str(ddata, "::*", 3)) + return (0); + ++ddata->func_type; + } else { + if (!cpp_demangle_push_type_qualifier(ddata, v, + (const char *) NULL)) + return (0); + vector_type_qualifier_dest(v); + if (!vector_type_qualifier_init(v)) + return (0); + } + + if (!cpp_demangle_push_str(ddata, ")(", 2)) + return (0); + + limit = 0; + for (;;) { + if (!cpp_demangle_read_type(ddata, 0)) + return (0); + if (*ddata->cur == 'E') + break; + if (limit++ > CPP_DEMANGLE_TRY_LIMIT) + return (0); + } + + if (vector_read_cmd_find(&ddata->cmd, READ_PTRMEM) == 1) { + if (!cpp_demangle_push_type_qualifier(ddata, v, + (const char *) NULL)) + return (0); + vector_type_qualifier_dest(v); + if (!vector_type_qualifier_init(v)) + return (0); + } + + if (!cpp_demangle_push_str(ddata, ")", 1)) + return (0); + } + + ++ddata->cur; + + return (1); +} + +/* read encoding, encoding are function name, data name, special-name */ +static int +cpp_demangle_read_encoding(struct cpp_demangle_data *ddata) +{ + + if (ddata == NULL || *ddata->cur == '\0') + return (0); + + /* special name */ + switch (SIMPLE_HASH(*ddata->cur, *(ddata->cur + 1))) { + case SIMPLE_HASH('G', 'V'): + /* sentry object for 1 time init */ + if (!cpp_demangle_push_str(ddata, "guard variable for ", 20)) + return (0); + ddata->cur += 2; + break; + + case SIMPLE_HASH('T', 'c'): + /* virtual function covariant override thunk */ + if (!cpp_demangle_push_str(ddata, + "virtual function covariant override ", 36)) + return (0); + ddata->cur += 2; + if (*ddata->cur == '\0') + return (0); + if (!cpp_demangle_read_offset(ddata)) + return (0); + if (!cpp_demangle_read_offset(ddata)) + return (0); + return (cpp_demangle_read_encoding(ddata)); + + case SIMPLE_HASH('T', 'D'): + /* typeinfo common proxy */ + break; + + case SIMPLE_HASH('T', 'h'): + /* virtual function non-virtual override thunk */ + if (cpp_demangle_push_str(ddata, + "virtual function non-virtual override ", 38) == 0) + return (0); + ddata->cur += 2; + if (*ddata->cur == '\0') + return (0); + if (!cpp_demangle_read_nv_offset(ddata)) + return (0); + return (cpp_demangle_read_encoding(ddata)); + + case SIMPLE_HASH('T', 'I'): + /* typeinfo structure */ + /* FALLTHROUGH */ + case SIMPLE_HASH('T', 'S'): + /* RTTI name (NTBS) */ + if (!cpp_demangle_push_str(ddata, "typeinfo for ", 14)) + return (0); + ddata->cur += 2; + if (*ddata->cur == '\0') + return (0); + return (cpp_demangle_read_type(ddata, 1)); + + case SIMPLE_HASH('T', 'T'): + /* VTT table */ + if (!cpp_demangle_push_str(ddata, "VTT for ", 8)) + return (0); + ddata->cur += 2; + return (cpp_demangle_read_type(ddata, 1)); + + case SIMPLE_HASH('T', 'v'): + /* virtual function virtual override thunk */ + if (!cpp_demangle_push_str(ddata, + "virtual function virtual override ", 34)) + return (0); + ddata->cur += 2; + if (*ddata->cur == '\0') + return (0); + if (!cpp_demangle_read_v_offset(ddata)) + return (0); + return (cpp_demangle_read_encoding(ddata)); + + case SIMPLE_HASH('T', 'V'): + /* virtual table */ + if (!cpp_demangle_push_str(ddata, "vtable for ", 12)) + return (0); + ddata->cur += 2; + if (*ddata->cur == '\0') + return (0); + return (cpp_demangle_read_type(ddata, 1)); + }; + + return (cpp_demangle_read_name(ddata)); +} + +static int +cpp_demangle_read_local_name(struct cpp_demangle_data *ddata) +{ + size_t limit; + + if (ddata == NULL) + return (0); + if (*(++ddata->cur) == '\0') + return (0); + if (!cpp_demangle_read_encoding(ddata)) + return (0); + + limit = 0; + for (;;) { + if (!cpp_demangle_read_type(ddata, 1)) + return (0); + if (*ddata->cur == 'E') + break; + if (limit++ > CPP_DEMANGLE_TRY_LIMIT) + return (0); + } + if (*(++ddata->cur) == '\0') + return (0); + if (ddata->paren == true) { + if (!cpp_demangle_push_str(ddata, ")", 1)) + return (0); + ddata->paren = false; + } + if (*ddata->cur == 's') + ++ddata->cur; + else { + if (!cpp_demangle_push_str(ddata, "::", 2)) + return (0); + if (!cpp_demangle_read_name(ddata)) + return (0); + } + if (*ddata->cur == '_') { + ++ddata->cur; + while (ELFTC_ISDIGIT(*ddata->cur) != 0) + ++ddata->cur; + } + + return (1); +} + +static int +cpp_demangle_read_name(struct cpp_demangle_data *ddata) +{ + struct vector_str *output, v; + size_t p_idx, subst_str_len; + int rtn; + char *subst_str; + + if (ddata == NULL || *ddata->cur == '\0') + return (0); + + output = cpp_demangle_gnu3_push_head > 0 ? + &ddata->output_tmp : &ddata->output; + + subst_str = NULL; + + switch (*ddata->cur) { + case 'S': + return (cpp_demangle_read_subst(ddata)); + case 'N': + return (cpp_demangle_read_nested_name(ddata)); + case 'Z': + return (cpp_demangle_read_local_name(ddata)); + }; + + if (!vector_str_init(&v)) + return (0); + + p_idx = output->size; + rtn = 0; + if (!cpp_demangle_read_uqname(ddata)) + goto clean; + if ((subst_str = vector_str_substr(output, p_idx, output->size - 1, + &subst_str_len)) == NULL) + goto clean; + if (subst_str_len > 8 && strstr(subst_str, "operator") != NULL) { + rtn = 1; + goto clean; + } + if (!vector_str_push(&v, subst_str, subst_str_len)) + goto clean; + if (!cpp_demangle_push_subst_v(ddata, &v)) + goto clean; + + if (*ddata->cur == 'I') { + p_idx = output->size; + if (!cpp_demangle_read_tmpl_args(ddata)) + goto clean; + free(subst_str); + if ((subst_str = vector_str_substr(output, p_idx, + output->size - 1, &subst_str_len)) == NULL) + goto clean; + if (!vector_str_push(&v, subst_str, subst_str_len)) + goto clean; + if (!cpp_demangle_push_subst_v(ddata, &v)) + goto clean; + } + + rtn = 1; + +clean: + free(subst_str); + vector_str_dest(&v); + + return (rtn); +} + +static int +cpp_demangle_read_nested_name(struct cpp_demangle_data *ddata) +{ + struct vector_str *output, v; + size_t limit, p_idx, subst_str_len; + int rtn; + char *subst_str; + + if (ddata == NULL || *ddata->cur != 'N') + return (0); + if (*(++ddata->cur) == '\0') + return (0); + + while (*ddata->cur == 'r' || *ddata->cur == 'V' || + *ddata->cur == 'K') { + switch (*ddata->cur) { + case 'r': + ddata->mem_rst = true; + break; + case 'V': + ddata->mem_vat = true; + break; + case 'K': + ddata->mem_cst = true; + break; + }; + ++ddata->cur; + } + + output = cpp_demangle_gnu3_push_head > 0 ? + &ddata->output_tmp : &ddata->output; + if (!vector_str_init(&v)) + return (0); + + rtn = 0; + limit = 0; + for (;;) { + p_idx = output->size; + switch (*ddata->cur) { + case 'I': + if (!cpp_demangle_read_tmpl_args(ddata)) + goto clean; + break; + case 'S': + if (!cpp_demangle_read_subst(ddata)) + goto clean; + break; + case 'T': + if (!cpp_demangle_read_tmpl_param(ddata)) + goto clean; + break; + default: + if (!cpp_demangle_read_uqname(ddata)) + goto clean; + }; + + if ((subst_str = vector_str_substr(output, p_idx, + output->size - 1, &subst_str_len)) == NULL) + goto clean; + if (!vector_str_push(&v, subst_str, subst_str_len)) { + free(subst_str); + goto clean; + } + free(subst_str); + + if (!cpp_demangle_push_subst_v(ddata, &v)) + goto clean; + if (*ddata->cur == 'E') + break; + else if (*ddata->cur != 'I' && + *ddata->cur != 'C' && *ddata->cur != 'D') { + if (!cpp_demangle_push_str(ddata, "::", 2)) + goto clean; + if (!vector_str_push(&v, "::", 2)) + goto clean; + } + if (limit++ > CPP_DEMANGLE_TRY_LIMIT) + goto clean; + } + + ++ddata->cur; + rtn = 1; + +clean: + vector_str_dest(&v); + + return (rtn); +} + +/* + * read number + * number ::= [n] <decimal> + */ +static int +cpp_demangle_read_number(struct cpp_demangle_data *ddata, long *rtn) +{ + long len, negative_factor; + + if (ddata == NULL || rtn == NULL) + return (0); + + negative_factor = 1; + if (*ddata->cur == 'n') { + negative_factor = -1; + + ++ddata->cur; + } + if (ELFTC_ISDIGIT(*ddata->cur) == 0) + return (0); + + errno = 0; + if ((len = strtol(ddata->cur, (char **) NULL, 10)) == 0 && + errno != 0) + return (0); + + while (ELFTC_ISDIGIT(*ddata->cur) != 0) + ++ddata->cur; + + assert(len >= 0); + assert(negative_factor == 1 || negative_factor == -1); + + *rtn = len * negative_factor; + + return (1); +} + +static int +cpp_demangle_read_nv_offset(struct cpp_demangle_data *ddata) +{ + + if (ddata == NULL) + return (0); + + if (!cpp_demangle_push_str(ddata, "offset : ", 9)) + return (0); + + return (cpp_demangle_read_offset_number(ddata)); +} + +/* read offset, offset are nv-offset, v-offset */ +static int +cpp_demangle_read_offset(struct cpp_demangle_data *ddata) +{ + + if (ddata == NULL) + return (0); + + if (*ddata->cur == 'h') { + ++ddata->cur; + return (cpp_demangle_read_nv_offset(ddata)); + } else if (*ddata->cur == 'v') { + ++ddata->cur; + return (cpp_demangle_read_v_offset(ddata)); + } + + return (0); +} + +static int +cpp_demangle_read_offset_number(struct cpp_demangle_data *ddata) +{ + bool negative; + const char *start; + + if (ddata == NULL || *ddata->cur == '\0') + return (0); + + /* offset could be negative */ + if (*ddata->cur == 'n') { + negative = true; + start = ddata->cur + 1; + } else { + negative = false; + start = ddata->cur; + } + + while (*ddata->cur != '_') + ++ddata->cur; + + if (negative && !cpp_demangle_push_str(ddata, "-", 1)) + return (0); + + assert(start != NULL); + + if (!cpp_demangle_push_str(ddata, start, ddata->cur - start)) + return (0); + if (!cpp_demangle_push_str(ddata, " ", 1)) + return (0); + + ++ddata->cur; + + return (1); +} + +static int +cpp_demangle_read_pointer_to_member(struct cpp_demangle_data *ddata) +{ + size_t class_type_len, i, idx, p_idx; + int p_func_type, rtn; + char *class_type; + + if (ddata == NULL || *ddata->cur != 'M' || *(++ddata->cur) == '\0') + return (0); + + p_idx = ddata->output.size; + if (!cpp_demangle_read_type(ddata, 0)) + return (0); + + if ((class_type = vector_str_substr(&ddata->output, p_idx, + ddata->output.size - 1, &class_type_len)) == NULL) + return (0); + + rtn = 0; + idx = ddata->output.size; + for (i = p_idx; i < idx; ++i) + if (!vector_str_pop(&ddata->output)) + goto clean1; + + if (!vector_read_cmd_push(&ddata->cmd, READ_PTRMEM)) + goto clean1; + + if (!vector_str_push(&ddata->class_type, class_type, class_type_len)) + goto clean2; + + p_func_type = ddata->func_type; + if (!cpp_demangle_read_type(ddata, 0)) + goto clean3; + + if (p_func_type == ddata->func_type) { + if (!cpp_demangle_push_str(ddata, " ", 1)) + goto clean3; + if (!cpp_demangle_push_str(ddata, class_type, class_type_len)) + goto clean3; + if (!cpp_demangle_push_str(ddata, "::*", 3)) + goto clean3; + } + + rtn = 1; +clean3: + if (!vector_str_pop(&ddata->class_type)) + rtn = 0; +clean2: + if (!vector_read_cmd_pop(&ddata->cmd)) + rtn = 0; +clean1: + free(class_type); + + return (rtn); +} + +/* read source-name, source-name is <len> <ID> */ +static int +cpp_demangle_read_sname(struct cpp_demangle_data *ddata) +{ + long len; + + if (ddata == NULL || cpp_demangle_read_number(ddata, &len) == 0 || + len <= 0 || cpp_demangle_push_str(ddata, ddata->cur, len) == 0) + return (0); + + assert(ddata->output.size > 0); + if (vector_read_cmd_find(&ddata->cmd, READ_TMPL) == 0) + ddata->last_sname = + ddata->output.container[ddata->output.size - 1]; + + ddata->cur += len; + + return (1); +} + +static int +cpp_demangle_read_subst(struct cpp_demangle_data *ddata) +{ + long nth; + + if (ddata == NULL || *ddata->cur == '\0') + return (0); + + /* abbreviations of the form Sx */ + switch (SIMPLE_HASH(*ddata->cur, *(ddata->cur + 1))) { + case SIMPLE_HASH('S', 'a'): + /* std::allocator */ + if (cpp_demangle_push_str(ddata, "std::allocator", 14) == 0) + return (0); + ddata->cur += 2; + if (*ddata->cur == 'I') + return (cpp_demangle_read_subst_stdtmpl(ddata, + "std::allocator", 14)); + return (1); + + case SIMPLE_HASH('S', 'b'): + /* std::basic_string */ + if (!cpp_demangle_push_str(ddata, "std::basic_string", 17)) + return (0); + ddata->cur += 2; + if (*ddata->cur == 'I') + return (cpp_demangle_read_subst_stdtmpl(ddata, + "std::basic_string", 17)); + return (1); + + case SIMPLE_HASH('S', 'd'): + /* std::basic_iostream<char, std::char_traits<char> > */ + if (!cpp_demangle_push_str(ddata, "std::iostream", 19)) + return (0); + ddata->last_sname = "iostream"; + ddata->cur += 2; + if (*ddata->cur == 'I') + return (cpp_demangle_read_subst_stdtmpl(ddata, + "std::iostream", 19)); + return (1); + + case SIMPLE_HASH('S', 'i'): + /* std::basic_istream<char, std::char_traits<char> > */ + if (!cpp_demangle_push_str(ddata, "std::istream", 18)) + return (0); + ddata->last_sname = "istream"; + ddata->cur += 2; + if (*ddata->cur == 'I') + return (cpp_demangle_read_subst_stdtmpl(ddata, + "std::istream", 18)); + return (1); + + case SIMPLE_HASH('S', 'o'): + /* std::basic_ostream<char, std::char_traits<char> > */ + if (!cpp_demangle_push_str(ddata, "std::ostream", 18)) + return (0); + ddata->last_sname = "istream"; + ddata->cur += 2; + if (*ddata->cur == 'I') + return (cpp_demangle_read_subst_stdtmpl(ddata, + "std::ostream", 18)); + return (1); + + case SIMPLE_HASH('S', 's'): + /* + * std::basic_string<char, std::char_traits<char>, + * std::allocator<char> > + * + * a.k.a std::string + */ + if (!cpp_demangle_push_str(ddata, "std::string", 11)) + return (0); + ddata->last_sname = "string"; + ddata->cur += 2; + if (*ddata->cur == 'I') + return (cpp_demangle_read_subst_stdtmpl(ddata, + "std::string", 11)); + return (1); + + case SIMPLE_HASH('S', 't'): + /* std:: */ + return (cpp_demangle_read_subst_std(ddata)); + }; + + if (*(++ddata->cur) == '\0') + return (0); + + /* substitution */ + if (*ddata->cur == '_') + return (cpp_demangle_get_subst(ddata, 0)); + else { + errno = 0; + /* substitution number is base 36 */ + if ((nth = strtol(ddata->cur, (char **) NULL, 36)) == 0 && + errno != 0) + return (0); + + /* first was '_', so increase one */ + ++nth; + + while (*ddata->cur != '_') + ++ddata->cur; + + assert(nth > 0); + + return (cpp_demangle_get_subst(ddata, nth)); + } + + /* NOTREACHED */ + return (0); +} + +static int +cpp_demangle_read_subst_std(struct cpp_demangle_data *ddata) +{ + struct vector_str *output, v; + size_t p_idx, subst_str_len; + int rtn; + char *subst_str; + + if (ddata == NULL) + return (0); + + if (!vector_str_init(&v)) + return (0); + + subst_str = NULL; + rtn = 0; + if (!cpp_demangle_push_str(ddata, "std::", 5)) + goto clean; + + if (!vector_str_push(&v, "std::", 5)) + goto clean; + + ddata->cur += 2; + + output = cpp_demangle_gnu3_push_head > 0 ? + &ddata->output_tmp : &ddata->output; + + p_idx = output->size; + if (!cpp_demangle_read_uqname(ddata)) + goto clean; + + if ((subst_str = vector_str_substr(output, p_idx, output->size - 1, + &subst_str_len)) == NULL) + goto clean; + + if (!vector_str_push(&v, subst_str, subst_str_len)) + goto clean; + + if (!cpp_demangle_push_subst_v(ddata, &v)) + goto clean; + + if (*ddata->cur == 'I') { + p_idx = output->size; + if (!cpp_demangle_read_tmpl_args(ddata)) + goto clean; + free(subst_str); + if ((subst_str = vector_str_substr(output, p_idx, + output->size - 1, &subst_str_len)) == NULL) + goto clean; + if (!vector_str_push(&v, subst_str, subst_str_len)) + goto clean; + if (!cpp_demangle_push_subst_v(ddata, &v)) + goto clean; + } + + rtn = 1; +clean: + free(subst_str); + vector_str_dest(&v); + + return (rtn); +} + +static int +cpp_demangle_read_subst_stdtmpl(struct cpp_demangle_data *ddata, + const char *str, size_t len) +{ + struct vector_str *output; + size_t p_idx, substr_len; + int rtn; + char *subst_str, *substr; + + if (ddata == NULL || str == NULL || len == 0) + return (0); + + output = cpp_demangle_gnu3_push_head > 0 ? &ddata->output_tmp : + &ddata->output; + + p_idx = output->size; + substr = NULL; + subst_str = NULL; + + if (!cpp_demangle_read_tmpl_args(ddata)) + return (0); + if ((substr = vector_str_substr(output, p_idx, output->size - 1, + &substr_len)) == NULL) + return (0); + + rtn = 0; + if ((subst_str = malloc(sizeof(char) * (substr_len + len + 1))) == + NULL) + goto clean; + + memcpy(subst_str, str, len); + memcpy(subst_str + len, substr, substr_len); + subst_str[substr_len + len] = '\0'; + + if (!cpp_demangle_push_subst(ddata, subst_str, substr_len + len)) + goto clean; + + rtn = 1; +clean: + free(subst_str); + free(substr); + + return (rtn); +} + +static int +cpp_demangle_read_tmpl_arg(struct cpp_demangle_data *ddata) +{ + + if (ddata == NULL || *ddata->cur == '\0') + return (0); + + switch (*ddata->cur) { + case 'L': + return (cpp_demangle_read_expr_primary(ddata)); + case 'X': + return (cpp_demangle_read_expression(ddata)); + }; + + return (cpp_demangle_read_type(ddata, 0)); +} + +static int +cpp_demangle_read_tmpl_args(struct cpp_demangle_data *ddata) +{ + struct vector_str *v; + size_t arg_len, idx, limit, size; + char *arg; + + if (ddata == NULL || *ddata->cur == '\0') + return (0); + + ++ddata->cur; + + if (!vector_read_cmd_push(&ddata->cmd, READ_TMPL)) + return (0); + + if (!cpp_demangle_push_str(ddata, "<", 1)) + return (0); + + limit = 0; + v = cpp_demangle_gnu3_push_head > 0 ? + &ddata->output_tmp : &ddata->output; + for (;;) { + idx = v->size; + if (!cpp_demangle_read_tmpl_arg(ddata)) + return (0); + if ((arg = vector_str_substr(v, idx, v->size - 1, &arg_len)) == + NULL) + return (0); + if (!vector_str_find(&ddata->tmpl, arg, arg_len) && + !vector_str_push(&ddata->tmpl, arg, arg_len)) { + free(arg); + return (0); + } + + free(arg); + + if (*ddata->cur == 'E') { + ++ddata->cur; + size = v->size; + assert(size > 0); + if (!strncmp(v->container[size - 1], ">", 1)) { + if (!cpp_demangle_push_str(ddata, " >", 2)) + return (0); + } else if (!cpp_demangle_push_str(ddata, ">", 1)) + return (0); + break; + } else if (*ddata->cur != 'I' && + !cpp_demangle_push_str(ddata, ", ", 2)) + return (0); + + if (limit++ > CPP_DEMANGLE_TRY_LIMIT) + return (0); + } + + return (vector_read_cmd_pop(&ddata->cmd)); +} + +/* + * Read template parameter that forms in 'T[number]_'. + * This function much like to read_subst but only for types. + */ +static int +cpp_demangle_read_tmpl_param(struct cpp_demangle_data *ddata) +{ + long nth; + + if (ddata == NULL || *ddata->cur != 'T') + return (0); + + ++ddata->cur; + + if (*ddata->cur == '_') + return (cpp_demangle_get_tmpl_param(ddata, 0)); + else { + + errno = 0; + if ((nth = strtol(ddata->cur, (char **) NULL, 36)) == 0 && + errno != 0) + return (0); + + /* T_ is first */ + ++nth; + + while (*ddata->cur != '_') + ++ddata->cur; + + assert(nth > 0); + + return (cpp_demangle_get_tmpl_param(ddata, nth)); + } + + /* NOTREACHED */ + return (0); +} + +static int +cpp_demangle_read_type(struct cpp_demangle_data *ddata, int delimit) +{ + struct vector_type_qualifier v; + struct vector_str *output; + size_t p_idx, type_str_len; + int extern_c, is_builtin; + long len; + char *type_str; + + if (ddata == NULL) + return (0); + + output = &ddata->output; + if (!strncmp(ddata->output.container[ddata->output.size - 1], ">", 1)) { + cpp_demangle_gnu3_push_head++; + output = &ddata->output_tmp; + } else if (delimit == 1) { + if (ddata->paren == false) { + if (!cpp_demangle_push_str(ddata, "(", 1)) + return (0); + if (ddata->output.size < 2) + return (0); + ddata->paren = true; + ddata->pfirst = true; + /* Need pop function name */ + if (ddata->subst.size == 1 && + !vector_str_pop(&ddata->subst)) + return (0); + } + + if (ddata->pfirst) + ddata->pfirst = false; + else if (*ddata->cur != 'I' && + !cpp_demangle_push_str(ddata, ", ", 2)) + return (0); + } + + assert(output != NULL); + /* + * [r, V, K] [P, R, C, G, U] builtin, function, class-enum, array + * pointer-to-member, template-param, template-template-param, subst + */ + + if (!vector_type_qualifier_init(&v)) + return (0); + + extern_c = 0; + is_builtin = 1; + p_idx = output->size; + type_str = NULL; +again: + /* builtin type */ + switch (*ddata->cur) { + case 'a': + /* signed char */ + if (!cpp_demangle_push_str(ddata, "signed char", 11)) + goto clean; + ++ddata->cur; + goto rtn; + + case 'A': + /* array type */ + if (!cpp_demangle_read_array(ddata)) + goto clean; + is_builtin = 0; + goto rtn; + + case 'b': + /* bool */ + if (!cpp_demangle_push_str(ddata, "bool", 4)) + goto clean; + ++ddata->cur; + goto rtn; + + case 'C': + /* complex pair */ + if (!vector_type_qualifier_push(&v, TYPE_CMX)) + goto clean; + ++ddata->cur; + goto again; + + case 'c': + /* char */ + if (!cpp_demangle_push_str(ddata, "char", 4)) + goto clean; + ++ddata->cur; + goto rtn; + + case 'd': + /* double */ + if (!cpp_demangle_push_str(ddata, "double", 6)) + goto clean; + ++ddata->cur; + goto rtn; + + case 'e': + /* long double */ + if (!cpp_demangle_push_str(ddata, "long double", 11)) + goto clean; + ++ddata->cur; + goto rtn; + + case 'f': + /* float */ + if (!cpp_demangle_push_str(ddata, "float", 5)) + goto clean; + ++ddata->cur; + goto rtn; + + case 'F': + /* function */ + if (!cpp_demangle_read_function(ddata, &extern_c, &v)) + goto clean; + is_builtin = 0; + goto rtn; + + case 'g': + /* __float128 */ + if (!cpp_demangle_push_str(ddata, "__float128", 10)) + goto clean; + ++ddata->cur; + goto rtn; + + case 'G': + /* imaginary */ + if (!vector_type_qualifier_push(&v, TYPE_IMG)) + goto clean; + ++ddata->cur; + goto again; + + case 'h': + /* unsigned char */ + if (!cpp_demangle_push_str(ddata, "unsigned char", 13)) + goto clean; + ++ddata->cur; + goto rtn; + + case 'i': + /* int */ + if (!cpp_demangle_push_str(ddata, "int", 3)) + goto clean; + ++ddata->cur; + goto rtn; + + case 'j': + /* unsigned int */ + if (!cpp_demangle_push_str(ddata, "unsigned int", 12)) + goto clean; + ++ddata->cur; + goto rtn; + + case 'K': + /* const */ + if (!vector_type_qualifier_push(&v, TYPE_CST)) + goto clean; + ++ddata->cur; + goto again; + + case 'l': + /* long */ + if (!cpp_demangle_push_str(ddata, "long", 4)) + goto clean; + ++ddata->cur; + goto rtn; + + case 'm': + /* unsigned long */ + if (!cpp_demangle_push_str(ddata, "unsigned long", 13)) + goto clean; + + ++ddata->cur; + + goto rtn; + case 'M': + /* pointer to member */ + if (!cpp_demangle_read_pointer_to_member(ddata)) + goto clean; + is_builtin = 0; + goto rtn; + + case 'n': + /* __int128 */ + if (!cpp_demangle_push_str(ddata, "__int128", 8)) + goto clean; + ++ddata->cur; + goto rtn; + + case 'o': + /* unsigned __int128 */ + if (!cpp_demangle_push_str(ddata, "unsigned _;int128", 17)) + goto clean; + ++ddata->cur; + goto rtn; + + case 'P': + /* pointer */ + if (!vector_type_qualifier_push(&v, TYPE_PTR)) + goto clean; + ++ddata->cur; + goto again; + + case 'r': + /* restrict */ + if (!vector_type_qualifier_push(&v, TYPE_RST)) + goto clean; + ++ddata->cur; + goto again; + + case 'R': + /* reference */ + if (!vector_type_qualifier_push(&v, TYPE_REF)) + goto clean; + ++ddata->cur; + goto again; + + case 's': + /* short, local string */ + if (!cpp_demangle_push_str(ddata, "short", 5)) + goto clean; + ++ddata->cur; + goto rtn; + + case 'S': + /* substitution */ + if (!cpp_demangle_read_subst(ddata)) + goto clean; + is_builtin = 0; + goto rtn; + + case 't': + /* unsigned short */ + if (!cpp_demangle_push_str(ddata, "unsigned short", 14)) + goto clean; + ++ddata->cur; + goto rtn; + + case 'T': + /* template parameter */ + if (!cpp_demangle_read_tmpl_param(ddata)) + goto clean; + is_builtin = 0; + goto rtn; + + case 'u': + /* vendor extended builtin */ + ++ddata->cur; + if (!cpp_demangle_read_sname(ddata)) + goto clean; + is_builtin = 0; + goto rtn; + + case 'U': + /* vendor extended type qualifier */ + if (!cpp_demangle_read_number(ddata, &len)) + goto clean; + if (len <= 0) + goto clean; + if (!vector_str_push(&v.ext_name, ddata->cur, len)) + return (0); + ddata->cur += len; + goto again; + + case 'v': + /* void */ + if (!cpp_demangle_push_str(ddata, "void", 4)) + goto clean; + ++ddata->cur; + goto rtn; + + case 'V': + /* volatile */ + if (!vector_type_qualifier_push(&v, TYPE_VAT)) + goto clean; + ++ddata->cur; + goto again; + + case 'w': + /* wchar_t */ + if (!cpp_demangle_push_str(ddata, "wchar_t", 6)) + goto clean; + ++ddata->cur; + goto rtn; + + case 'x': + /* long long */ + if (!cpp_demangle_push_str(ddata, "long long", 9)) + goto clean; + ++ddata->cur; + goto rtn; + + case 'y': + /* unsigned long long */ + if (!cpp_demangle_push_str(ddata, "unsigned long long", 18)) + goto clean; + ++ddata->cur; + goto rtn; + + case 'z': + /* ellipsis */ + if (!cpp_demangle_push_str(ddata, "ellipsis", 8)) + goto clean; + ++ddata->cur; + goto rtn; + }; + + if (!cpp_demangle_read_name(ddata)) + goto clean; + + is_builtin = 0; +rtn: + if ((type_str = vector_str_substr(output, p_idx, output->size - 1, + &type_str_len)) == NULL) + goto clean; + + if (is_builtin == 0) { + if (!vector_str_find(&ddata->subst, type_str, type_str_len) && + !vector_str_push(&ddata->subst, type_str, type_str_len)) + goto clean; + } + + if (!cpp_demangle_push_type_qualifier(ddata, &v, type_str)) + goto clean; + + free(type_str); + vector_type_qualifier_dest(&v); + + if (cpp_demangle_gnu3_push_head > 0) { + if (*ddata->cur == 'I' && cpp_demangle_read_tmpl_args(ddata) + == 0) + return (0); + + if (--cpp_demangle_gnu3_push_head > 0) + return (1); + + if (!vector_str_push(&ddata->output_tmp, " ", 1)) + return (0); + + if (!vector_str_push_vector_head(&ddata->output, + &ddata->output_tmp)) + return (0); + + vector_str_dest(&ddata->output_tmp); + if (!vector_str_init(&ddata->output_tmp)) + return (0); + + if (!cpp_demangle_push_str(ddata, "(", 1)) + return (0); + + ddata->paren = true; + ddata->pfirst = true; + } + + return (1); +clean: + free(type_str); + vector_type_qualifier_dest(&v); + + return (0); +} + +/* + * read unqualified-name, unqualified name are operator-name, ctor-dtor-name, + * source-name + */ +static int +cpp_demangle_read_uqname(struct cpp_demangle_data *ddata) +{ + size_t len; + + if (ddata == NULL || *ddata->cur == '\0') + return (0); + + /* operator name */ + switch (SIMPLE_HASH(*ddata->cur, *(ddata->cur + 1))) { + case SIMPLE_HASH('a', 'a'): + /* operator && */ + if (!cpp_demangle_push_str(ddata, "operator&&", 10)) + return (0); + ddata->cur += 2; + return (1); + + case SIMPLE_HASH('a', 'd'): + /* operator & (unary) */ + if (!cpp_demangle_push_str(ddata, "operator&", 9)) + return (0); + ddata->cur += 2; + return (1); + + case SIMPLE_HASH('a', 'n'): + /* operator & */ + if (!cpp_demangle_push_str(ddata, "operator&", 9)) + return (0); + ddata->cur += 2; + return (1); + + case SIMPLE_HASH('a', 'N'): + /* operator &= */ + if (!cpp_demangle_push_str(ddata, "operator&=", 10)) + return (0); + ddata->cur += 2; + return (1); + + case SIMPLE_HASH('a', 'S'): + /* operator = */ + if (!cpp_demangle_push_str(ddata, "operator=", 9)) + return (0); + ddata->cur += 2; + return (1); + + case SIMPLE_HASH('c', 'l'): + /* operator () */ + if (!cpp_demangle_push_str(ddata, "operator()", 10)) + return (0); + ddata->cur += 2; + return (1); + + case SIMPLE_HASH('c', 'm'): + /* operator , */ + if (!cpp_demangle_push_str(ddata, "operator,", 9)) + return (0); + ddata->cur += 2; + return (1); + + case SIMPLE_HASH('c', 'o'): + /* operator ~ */ + if (!cpp_demangle_push_str(ddata, "operator~", 9)) + return (0); + ddata->cur += 2; + return (1); + + case SIMPLE_HASH('c', 'v'): + /* operator (cast) */ + if (!cpp_demangle_push_str(ddata, "operator(cast)", 14)) + return (0); + ddata->cur += 2; + return (cpp_demangle_read_type(ddata, 1)); + + case SIMPLE_HASH('d', 'a'): + /* operator delete [] */ + if (!cpp_demangle_push_str(ddata, "operator delete []", 18)) + return (0); + ddata->cur += 2; + return (1); + + case SIMPLE_HASH('d', 'e'): + /* operator * (unary) */ + if (!cpp_demangle_push_str(ddata, "operator*", 9)) + return (0); + ddata->cur += 2; + return (1); + + case SIMPLE_HASH('d', 'l'): + /* operator delete */ + if (!cpp_demangle_push_str(ddata, "operator delete", 15)) + return (0); + ddata->cur += 2; + return (1); + + case SIMPLE_HASH('d', 'v'): + /* operator / */ + if (!cpp_demangle_push_str(ddata, "operator/", 9)) + return (0); + ddata->cur += 2; + return (1); + + case SIMPLE_HASH('d', 'V'): + /* operator /= */ + if (!cpp_demangle_push_str(ddata, "operator/=", 10)) + return (0); + ddata->cur += 2; + return (1); + + case SIMPLE_HASH('e', 'o'): + /* operator ^ */ + if (!cpp_demangle_push_str(ddata, "operator^", 9)) + return (0); + ddata->cur += 2; + return (1); + + case SIMPLE_HASH('e', 'O'): + /* operator ^= */ + if (!cpp_demangle_push_str(ddata, "operator^=", 10)) + return (0); + ddata->cur += 2; + return (1); + + case SIMPLE_HASH('e', 'q'): + /* operator == */ + if (!cpp_demangle_push_str(ddata, "operator==", 10)) + return (0); + ddata->cur += 2; + return (1); + + case SIMPLE_HASH('g', 'e'): + /* operator >= */ + if (!cpp_demangle_push_str(ddata, "operator>=", 10)) + return (0); + ddata->cur += 2; + return (1); + + case SIMPLE_HASH('g', 't'): + /* operator > */ + if (!cpp_demangle_push_str(ddata, "operator>", 9)) + return (0); + ddata->cur += 2; + return (1); + + case SIMPLE_HASH('i', 'x'): + /* operator [] */ + if (!cpp_demangle_push_str(ddata, "operator[]", 10)) + return (0); + ddata->cur += 2; + return (1); + + case SIMPLE_HASH('l', 'e'): + /* operator <= */ + if (!cpp_demangle_push_str(ddata, "operator<=", 10)) + return (0); + ddata->cur += 2; + return (1); + + case SIMPLE_HASH('l', 's'): + /* operator << */ + if (!cpp_demangle_push_str(ddata, "operator<<", 10)) + return (0); + ddata->cur += 2; + return (1); + + case SIMPLE_HASH('l', 'S'): + /* operator <<= */ + if (!cpp_demangle_push_str(ddata, "operator<<=", 11)) + return (0); + ddata->cur += 2; + return (1); + + case SIMPLE_HASH('l', 't'): + /* operator < */ + if (!cpp_demangle_push_str(ddata, "operator<", 9)) + return (0); + ddata->cur += 2; + return (1); + + case SIMPLE_HASH('m', 'i'): + /* operator - */ + if (!cpp_demangle_push_str(ddata, "operator-", 9)) + return (0); + ddata->cur += 2; + return (1); + + case SIMPLE_HASH('m', 'I'): + /* operator -= */ + if (!cpp_demangle_push_str(ddata, "operator-=", 10)) + return (0); + ddata->cur += 2; + return (1); + + case SIMPLE_HASH('m', 'l'): + /* operator * */ + if (!cpp_demangle_push_str(ddata, "operator*", 9)) + return (0); + ddata->cur += 2; + return (1); + + case SIMPLE_HASH('m', 'L'): + /* operator *= */ + if (!cpp_demangle_push_str(ddata, "operator*=", 10)) + return (0); + ddata->cur += 2; + return (1); + + case SIMPLE_HASH('m', 'm'): + /* operator -- */ + if (!cpp_demangle_push_str(ddata, "operator--", 10)) + return (0); + ddata->cur += 2; + return (1); + + case SIMPLE_HASH('n', 'a'): + /* operator new[] */ + if (!cpp_demangle_push_str(ddata, "operator new []", 15)) + return (0); + ddata->cur += 2; + return (1); + + case SIMPLE_HASH('n', 'e'): + /* operator != */ + if (!cpp_demangle_push_str(ddata, "operator!=", 10)) + return (0); + ddata->cur += 2; + return (1); + + case SIMPLE_HASH('n', 'g'): + /* operator - (unary) */ + if (!cpp_demangle_push_str(ddata, "operator-", 9)) + return (0); + ddata->cur += 2; + return (1); + + case SIMPLE_HASH('n', 't'): + /* operator ! */ + if (!cpp_demangle_push_str(ddata, "operator!", 9)) + return (0); + ddata->cur += 2; + return (1); + + case SIMPLE_HASH('n', 'w'): + /* operator new */ + if (!cpp_demangle_push_str(ddata, "operator new", 12)) + return (0); + ddata->cur += 2; + return (1); + + case SIMPLE_HASH('o', 'o'): + /* operator || */ + if (!cpp_demangle_push_str(ddata, "operator||", 10)) + return (0); + ddata->cur += 2; + return (1); + + case SIMPLE_HASH('o', 'r'): + /* operator | */ + if (!cpp_demangle_push_str(ddata, "operator|", 9)) + return (0); + ddata->cur += 2; + return (1); + + case SIMPLE_HASH('o', 'R'): + /* operator |= */ + if (!cpp_demangle_push_str(ddata, "operator|=", 10)) + return (0); + ddata->cur += 2; + return (1); + + case SIMPLE_HASH('p', 'l'): + /* operator + */ + if (!cpp_demangle_push_str(ddata, "operator+", 9)) + return (0); + ddata->cur += 2; + return (1); + + case SIMPLE_HASH('p', 'L'): + /* operator += */ + if (!cpp_demangle_push_str(ddata, "operator+=", 10)) + return (0); + ddata->cur += 2; + return (1); + + case SIMPLE_HASH('p', 'm'): + /* operator ->* */ + if (!cpp_demangle_push_str(ddata, "operator->*", 11)) + return (0); + ddata->cur += 2; + return (1); + + case SIMPLE_HASH('p', 'p'): + /* operator ++ */ + if (!cpp_demangle_push_str(ddata, "operator++", 10)) + return (0); + ddata->cur += 2; + return (1); + + case SIMPLE_HASH('p', 's'): + /* operator + (unary) */ + if (!cpp_demangle_push_str(ddata, "operator+", 9)) + return (0); + ddata->cur += 2; + return (1); + + case SIMPLE_HASH('p', 't'): + /* operator -> */ + if (!cpp_demangle_push_str(ddata, "operator->", 10)) + return (0); + ddata->cur += 2; + return (1); + + case SIMPLE_HASH('q', 'u'): + /* operator ? */ + if (!cpp_demangle_push_str(ddata, "operator?", 9)) + return (0); + ddata->cur += 2; + return (1); + + case SIMPLE_HASH('r', 'm'): + /* operator % */ + if (!cpp_demangle_push_str(ddata, "operator%", 9)) + return (0); + ddata->cur += 2; + return (1); + + case SIMPLE_HASH('r', 'M'): + /* operator %= */ + if (!cpp_demangle_push_str(ddata, "operator%=", 10)) + return (0); + ddata->cur += 2; + return (1); + + case SIMPLE_HASH('r', 's'): + /* operator >> */ + if (!cpp_demangle_push_str(ddata, "operator>>", 10)) + return (0); + ddata->cur += 2; + return (1); + + case SIMPLE_HASH('r', 'S'): + /* operator >>= */ + if (!cpp_demangle_push_str(ddata, "operator>>=", 11)) + return (0); + ddata->cur += 2; + return (1); + + case SIMPLE_HASH('r', 'z'): + /* operator sizeof */ + if (!cpp_demangle_push_str(ddata, "operator sizeof ", 16)) + return (0); + ddata->cur += 2; + return (1); + + case SIMPLE_HASH('s', 'r'): + /* scope resolution operator */ + if (!cpp_demangle_push_str(ddata, "scope resolution operator ", + 26)) + return (0); + ddata->cur += 2; + return (1); + + case SIMPLE_HASH('s', 'v'): + /* operator sizeof */ + if (!cpp_demangle_push_str(ddata, "operator sizeof ", 16)) + return (0); + ddata->cur += 2; + return (1); + }; + + /* vendor extened operator */ + if (*ddata->cur == 'v' && ELFTC_ISDIGIT(*(ddata->cur + 1))) { + if (!cpp_demangle_push_str(ddata, "vendor extened operator ", + 24)) + return (0); + if (!cpp_demangle_push_str(ddata, ddata->cur + 1, 1)) + return (0); + ddata->cur += 2; + return (cpp_demangle_read_sname(ddata)); + } + + /* ctor-dtor-name */ + switch (SIMPLE_HASH(*ddata->cur, *(ddata->cur + 1))) { + case SIMPLE_HASH('C', '1'): + /* FALLTHROUGH */ + case SIMPLE_HASH('C', '2'): + /* FALLTHROUGH */ + case SIMPLE_HASH('C', '3'): + if (ddata->last_sname == NULL) + return (0); + if ((len = strlen(ddata->last_sname)) == 0) + return (0); + if (!cpp_demangle_push_str(ddata, "::", 2)) + return (0); + if (!cpp_demangle_push_str(ddata, ddata->last_sname, len)) + return (0); + ddata->cur +=2; + return (1); + + case SIMPLE_HASH('D', '0'): + /* FALLTHROUGH */ + case SIMPLE_HASH('D', '1'): + /* FALLTHROUGH */ + case SIMPLE_HASH('D', '2'): + if (ddata->last_sname == NULL) + return (0); + if ((len = strlen(ddata->last_sname)) == 0) + return (0); + if (!cpp_demangle_push_str(ddata, "::~", 3)) + return (0); + if (!cpp_demangle_push_str(ddata, ddata->last_sname, len)) + return (0); + ddata->cur +=2; + return (1); + }; + + /* source name */ + if (ELFTC_ISDIGIT(*ddata->cur) != 0) + return (cpp_demangle_read_sname(ddata)); + + /* local source name */ + if (*ddata->cur == 'L') + return (cpp_demangle_local_source_name(ddata)); + + return (1); +} + +/* + * Read local source name. + * + * References: + * http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31775 + * http://gcc.gnu.org/viewcvs?view=rev&revision=124467 + */ +static int +cpp_demangle_local_source_name(struct cpp_demangle_data *ddata) +{ + /* L */ + if (ddata == NULL || *ddata->cur != 'L') + return (0); + ++ddata->cur; + + /* source name */ + if (!cpp_demangle_read_sname(ddata)) + return (0); + + /* discriminator */ + if (*ddata->cur == '_') { + ++ddata->cur; + while (ELFTC_ISDIGIT(*ddata->cur) != 0) + ++ddata->cur; + } + + return (1); +} + +static int +cpp_demangle_read_v_offset(struct cpp_demangle_data *ddata) +{ + + if (ddata == NULL) + return (0); + + if (!cpp_demangle_push_str(ddata, "offset : ", 9)) + return (0); + + if (!cpp_demangle_read_offset_number(ddata)) + return (0); + + if (!cpp_demangle_push_str(ddata, "virtual offset : ", 17)) + return (0); + + return (!cpp_demangle_read_offset_number(ddata)); +} + +/* + * Decode floating point representation to string + * Return new allocated string or NULL + * + * Todo + * Replace these functions to macro. + */ +static char * +decode_fp_to_double(const char *p, size_t len) +{ + double f; + size_t rtn_len, limit, i; + int byte; + char *rtn; + + if (p == NULL || len == 0 || len % 2 != 0 || len / 2 > sizeof(double)) + return (NULL); + + memset(&f, 0, sizeof(double)); + + for (i = 0; i < len / 2; ++i) { + byte = hex_to_dec(p[len - i * 2 - 1]) + + hex_to_dec(p[len - i * 2 - 2]) * 16; + + if (byte < 0 || byte > 255) + return (NULL); + +#if ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN + ((unsigned char *)&f)[i] = (unsigned char)(byte); +#else /* ELFTC_BYTE_ORDER != ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ + ((unsigned char *)&f)[sizeof(double) - i - 1] = + (unsigned char)(byte); +#endif /* ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ + } + + rtn_len = 64; + limit = 0; +again: + if ((rtn = malloc(sizeof(char) * rtn_len)) == NULL) + return (NULL); + + if (snprintf(rtn, rtn_len, "%fld", f) >= (int)rtn_len) { + free(rtn); + if (limit++ > FLOAT_SPRINTF_TRY_LIMIT) + return (NULL); + rtn_len *= BUFFER_GROWFACTOR; + goto again; + } + + return rtn; +} + +static char * +decode_fp_to_float(const char *p, size_t len) +{ + size_t i, rtn_len, limit; + float f; + int byte; + char *rtn; + + if (p == NULL || len == 0 || len % 2 != 0 || len / 2 > sizeof(float)) + return (NULL); + + memset(&f, 0, sizeof(float)); + + for (i = 0; i < len / 2; ++i) { + byte = hex_to_dec(p[len - i * 2 - 1]) + + hex_to_dec(p[len - i * 2 - 2]) * 16; + if (byte < 0 || byte > 255) + return (NULL); +#if ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN + ((unsigned char *)&f)[i] = (unsigned char)(byte); +#else /* ELFTC_BYTE_ORDER != ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ + ((unsigned char *)&f)[sizeof(float) - i - 1] = + (unsigned char)(byte); +#endif /* ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ + } + + rtn_len = 64; + limit = 0; +again: + if ((rtn = malloc(sizeof(char) * rtn_len)) == NULL) + return (NULL); + + if (snprintf(rtn, rtn_len, "%ff", f) >= (int)rtn_len) { + free(rtn); + if (limit++ > FLOAT_SPRINTF_TRY_LIMIT) + return (NULL); + rtn_len *= BUFFER_GROWFACTOR; + goto again; + } + + return rtn; +} + +static char * +decode_fp_to_float128(const char *p, size_t len) +{ + long double f; + size_t rtn_len, limit, i; + int byte; + unsigned char buf[FLOAT_QUADRUPLE_BYTES]; + char *rtn; + + switch(sizeof(long double)) { + case FLOAT_QUADRUPLE_BYTES: + return (decode_fp_to_long_double(p, len)); + case FLOAT_EXTENED_BYTES: + if (p == NULL || len == 0 || len % 2 != 0 || + len / 2 > FLOAT_QUADRUPLE_BYTES) + return (NULL); + + memset(buf, 0, FLOAT_QUADRUPLE_BYTES); + + for (i = 0; i < len / 2; ++i) { + byte = hex_to_dec(p[len - i * 2 - 1]) + + hex_to_dec(p[len - i * 2 - 2]) * 16; + if (byte < 0 || byte > 255) + return (NULL); +#if ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN + buf[i] = (unsigned char)(byte); +#else /* ELFTC_BYTE_ORDER != ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ + buf[FLOAT_QUADRUPLE_BYTES - i -1] = + (unsigned char)(byte); +#endif /* ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ + } + memset(&f, 0, FLOAT_EXTENED_BYTES); + +#if ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN + memcpy(&f, buf, FLOAT_EXTENED_BYTES); +#else /* ELFTC_BYTE_ORDER != ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ + memcpy(&f, buf + 6, FLOAT_EXTENED_BYTES); +#endif /* ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ + + rtn_len = 256; + limit = 0; +again: + if ((rtn = malloc(sizeof(char) * rtn_len)) == NULL) + return (NULL); + + if (snprintf(rtn, rtn_len, "%Lfd", f) >= (int)rtn_len) { + free(rtn); + if (limit++ > FLOAT_SPRINTF_TRY_LIMIT) + return (NULL); + rtn_len *= BUFFER_GROWFACTOR; + goto again; + } + + return (rtn); + default: + return (NULL); + } +} + +static char * +decode_fp_to_float80(const char *p, size_t len) +{ + long double f; + size_t rtn_len, limit, i; + int byte; + unsigned char buf[FLOAT_EXTENED_BYTES]; + char *rtn; + + switch(sizeof(long double)) { + case FLOAT_QUADRUPLE_BYTES: + if (p == NULL || len == 0 || len % 2 != 0 || + len / 2 > FLOAT_EXTENED_BYTES) + return (NULL); + + memset(buf, 0, FLOAT_EXTENED_BYTES); + + for (i = 0; i < len / 2; ++i) { + byte = hex_to_dec(p[len - i * 2 - 1]) + + hex_to_dec(p[len - i * 2 - 2]) * 16; + + if (byte < 0 || byte > 255) + return (NULL); + +#if ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN + buf[i] = (unsigned char)(byte); +#else /* ELFTC_BYTE_ORDER != ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ + buf[FLOAT_EXTENED_BYTES - i -1] = + (unsigned char)(byte); +#endif /* ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ + } + + memset(&f, 0, FLOAT_QUADRUPLE_BYTES); + +#if ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN + memcpy(&f, buf, FLOAT_EXTENED_BYTES); +#else /* ELFTC_BYTE_ORDER != ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ + memcpy((unsigned char *)(&f) + 6, buf, FLOAT_EXTENED_BYTES); +#endif /* ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ + + rtn_len = 256; + limit = 0; +again: + if ((rtn = malloc(sizeof(char) * rtn_len)) == NULL) + return (NULL); + + if (snprintf(rtn, rtn_len, "%Lfd", f) >= (int)rtn_len) { + free(rtn); + if (limit++ > FLOAT_SPRINTF_TRY_LIMIT) + return (NULL); + rtn_len *= BUFFER_GROWFACTOR; + goto again; + } + + return (rtn); + case FLOAT_EXTENED_BYTES: + return (decode_fp_to_long_double(p, len)); + default: + return (NULL); + } +} + +static char * +decode_fp_to_long_double(const char *p, size_t len) +{ + long double f; + size_t rtn_len, limit, i; + int byte; + char *rtn; + + if (p == NULL || len == 0 || len % 2 != 0 || + len / 2 > sizeof(long double)) + return (NULL); + + memset(&f, 0, sizeof(long double)); + + for (i = 0; i < len / 2; ++i) { + byte = hex_to_dec(p[len - i * 2 - 1]) + + hex_to_dec(p[len - i * 2 - 2]) * 16; + + if (byte < 0 || byte > 255) + return (NULL); + +#if ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN + ((unsigned char *)&f)[i] = (unsigned char)(byte); +#else /* ELFTC_BYTE_ORDER != ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ + ((unsigned char *)&f)[sizeof(long double) - i - 1] = + (unsigned char)(byte); +#endif /* ELFTC_BYTE_ORDER == ELFTC_BYTE_ORDER_LITTLE_ENDIAN */ + } + + rtn_len = 256; + limit = 0; +again: + if ((rtn = malloc(sizeof(char) * rtn_len)) == NULL) + return (NULL); + + if (snprintf(rtn, rtn_len, "%Lfd", f) >= (int)rtn_len) { + free(rtn); + if (limit++ > FLOAT_SPRINTF_TRY_LIMIT) + return (NULL); + rtn_len *= BUFFER_GROWFACTOR; + goto again; + } + + return (rtn); +} + +/* Simple hex to integer function used by decode_to_* function. */ +static int +hex_to_dec(char c) +{ + + switch (c) { + case '0': + return (0); + case '1': + return (1); + case '2': + return (2); + case '3': + return (3); + case '4': + return (4); + case '5': + return (5); + case '6': + return (6); + case '7': + return (7); + case '8': + return (8); + case '9': + return (9); + case 'a': + return (10); + case 'b': + return (11); + case 'c': + return (12); + case 'd': + return (13); + case 'e': + return (14); + case 'f': + return (15); + default: + return (-1); + }; +} + +/** + * @brief Test input string is mangled by IA-64 C++ ABI style. + * + * Test string heads with "_Z" or "_GLOBAL__I_". + * @return Return 0 at false. + */ +bool +is_cpp_mangled_gnu3(const char *org) +{ + size_t len; + + len = strlen(org); + return ((len > 2 && *org == '_' && *(org + 1) == 'Z') || + (len > 11 && !strncmp(org, "_GLOBAL__I_", 11))); +} + +static void +vector_read_cmd_dest(struct vector_read_cmd *v) +{ + + if (v == NULL) + return; + + free(v->r_container); +} + +/* return -1 at failed, 0 at not found, 1 at found. */ +static int +vector_read_cmd_find(struct vector_read_cmd *v, enum read_cmd dst) +{ + size_t i; + + if (v == NULL || dst == READ_FAIL) + return (-1); + + for (i = 0; i < v->size; ++i) + if (v->r_container[i] == dst) + return (1); + + return (0); +} + +static int +vector_read_cmd_init(struct vector_read_cmd *v) +{ + + if (v == NULL) + return (0); + + v->size = 0; + v->capacity = VECTOR_DEF_CAPACITY; + + if ((v->r_container = malloc(sizeof(enum read_cmd) * v->capacity)) + == NULL) + return (0); + + return (1); +} + +static int +vector_read_cmd_pop(struct vector_read_cmd *v) +{ + + if (v == NULL || v->size == 0) + return (0); + + --v->size; + v->r_container[v->size] = READ_FAIL; + + return (1); +} + +static int +vector_read_cmd_push(struct vector_read_cmd *v, enum read_cmd cmd) +{ + enum read_cmd *tmp_r_ctn; + size_t tmp_cap; + size_t i; + + if (v == NULL) + return (0); + + if (v->size == v->capacity) { + tmp_cap = v->capacity * BUFFER_GROWFACTOR; + if ((tmp_r_ctn = malloc(sizeof(enum read_cmd) * tmp_cap)) + == NULL) + return (0); + for (i = 0; i < v->size; ++i) + tmp_r_ctn[i] = v->r_container[i]; + free(v->r_container); + v->r_container = tmp_r_ctn; + v->capacity = tmp_cap; + } + + v->r_container[v->size] = cmd; + ++v->size; + + return (1); +} + +static void +vector_type_qualifier_dest(struct vector_type_qualifier *v) +{ + + if (v == NULL) + return; + + free(v->q_container); + vector_str_dest(&v->ext_name); +} + +/* size, capacity, ext_name */ +static int +vector_type_qualifier_init(struct vector_type_qualifier *v) +{ + + if (v == NULL) + return (0); + + v->size = 0; + v->capacity = VECTOR_DEF_CAPACITY; + + if ((v->q_container = malloc(sizeof(enum type_qualifier) * v->capacity)) + == NULL) + return (0); + + assert(v->q_container != NULL); + + if (vector_str_init(&v->ext_name) == false) { + free(v->q_container); + return (0); + } + + return (1); +} + +static int +vector_type_qualifier_push(struct vector_type_qualifier *v, + enum type_qualifier t) +{ + enum type_qualifier *tmp_ctn; + size_t tmp_cap; + size_t i; + + if (v == NULL) + return (0); + + if (v->size == v->capacity) { + tmp_cap = v->capacity * BUFFER_GROWFACTOR; + if ((tmp_ctn = malloc(sizeof(enum type_qualifier) * tmp_cap)) + == NULL) + return (0); + for (i = 0; i < v->size; ++i) + tmp_ctn[i] = v->q_container[i]; + free(v->q_container); + v->q_container = tmp_ctn; + v->capacity = tmp_cap; + } + + v->q_container[v->size] = t; + ++v->size; + + return (1); +} diff --git a/contrib/elftoolchain/libelftc/libelftc_hash.c b/contrib/elftoolchain/libelftc/libelftc_hash.c new file mode 100644 index 000000000000..6cc54b964e46 --- /dev/null +++ b/contrib/elftoolchain/libelftc/libelftc_hash.c @@ -0,0 +1,70 @@ +/*- + * Copyright (c) 2013, Joseph Koshy + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer + * in this position and unchanged. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * An implementation of the Fowler-Noll-Vo hash function. + * + * References: + * - http://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function + * - http://www.isthe.com/chongo/tech/comp/fnv/ + */ + +#include <sys/types.h> + +#include <limits.h> + +#include "_libelftc.h" + +ELFTC_VCSID("$Id: libelftc_hash.c 2870 2013-01-07 10:38:43Z jkoshy $"); + +/* + * Use the size of an 'int' to determine the magic numbers used by the + * hash function. + */ + +#if INT_MAX == 2147483647UL +#define FNV_PRIME 16777619UL +#define FNV_OFFSET 2166136261UL +#elif INT_MAX == 18446744073709551615ULL +#define FNV_PRIME 1099511628211ULL +#define FNV_OFFSET 14695981039346656037ULL +#else +#error sizeof(int) is unknown. +#endif + +unsigned int +libelftc_hash_string(const char *s) +{ + char c; + unsigned int hash; + + for (hash = FNV_OFFSET; (c = *s) != '\0'; s++) { + hash ^= c; + hash *= FNV_PRIME; + } + + return (hash); +} diff --git a/contrib/elftoolchain/libelftc/libelftc_vstr.c b/contrib/elftoolchain/libelftc/libelftc_vstr.c new file mode 100644 index 000000000000..4b0977efa0f5 --- /dev/null +++ b/contrib/elftoolchain/libelftc/libelftc_vstr.c @@ -0,0 +1,318 @@ +/*- + * Copyright (c) 2008 Hyogeol Lee <hyogeollee@gmail.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer + * in this position and unchanged. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/types.h> +#include <assert.h> +#include <libelftc.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "_libelftc.h" + +ELFTC_VCSID("$Id: libelftc_vstr.c 2065 2011-10-26 15:24:47Z jkoshy $"); + +/** + * @file vector_str.c + * @brief Dynamic vector data for string implementation. + * + * Resemble to std::vector<std::string> in C++. + */ + +static size_t get_strlen_sum(const struct vector_str *v); +static bool vector_str_grow(struct vector_str *v); + +static size_t +get_strlen_sum(const struct vector_str *v) +{ + size_t i, len = 0; + + if (v == NULL) + return (0); + + assert(v->size > 0); + + for (i = 0; i < v->size; ++i) + len += strlen(v->container[i]); + + return (len); +} + +/** + * @brief Deallocate resource in vector_str. + */ +void +vector_str_dest(struct vector_str *v) +{ + size_t i; + + if (v == NULL) + return; + + for (i = 0; i < v->size; ++i) + free(v->container[i]); + + free(v->container); +} + +/** + * @brief Find string in vector_str. + * @param v Destination vector. + * @param o String to find. + * @param l Length of the string. + * @return -1 at failed, 0 at not found, 1 at found. + */ +int +vector_str_find(const struct vector_str *v, const char *o, size_t l) +{ + size_t i; + + if (v == NULL || o == NULL) + return (-1); + + for (i = 0; i < v->size; ++i) + if (strncmp(v->container[i], o, l) == 0) + return (1); + + return (0); +} + +/** + * @brief Get new allocated flat string from vector. + * + * If l is not NULL, return length of the string. + * @param v Destination vector. + * @param l Length of the string. + * @return NULL at failed or NUL terminated new allocated string. + */ +char * +vector_str_get_flat(const struct vector_str *v, size_t *l) +{ + ssize_t elem_pos, elem_size, rtn_size; + size_t i; + char *rtn; + + if (v == NULL || v->size == 0) + return (NULL); + + if ((rtn_size = get_strlen_sum(v)) == 0) + return (NULL); + + if ((rtn = malloc(sizeof(char) * (rtn_size + 1))) == NULL) + return (NULL); + + elem_pos = 0; + for (i = 0; i < v->size; ++i) { + elem_size = strlen(v->container[i]); + + memcpy(rtn + elem_pos, v->container[i], elem_size); + + elem_pos += elem_size; + } + + rtn[rtn_size] = '\0'; + + if (l != NULL) + *l = rtn_size; + + return (rtn); +} + +static bool +vector_str_grow(struct vector_str *v) +{ + size_t i, tmp_cap; + char **tmp_ctn; + + if (v == NULL) + return (false); + + assert(v->capacity > 0); + + tmp_cap = v->capacity * BUFFER_GROWFACTOR; + + assert(tmp_cap > v->capacity); + + if ((tmp_ctn = malloc(sizeof(char *) * tmp_cap)) == NULL) + return (false); + + for (i = 0; i < v->size; ++i) + tmp_ctn[i] = v->container[i]; + + free(v->container); + + v->container = tmp_ctn; + v->capacity = tmp_cap; + + return (true); +} + +/** + * @brief Initialize vector_str. + * @return false at failed, true at success. + */ +bool +vector_str_init(struct vector_str *v) +{ + + if (v == NULL) + return (false); + + v->size = 0; + v->capacity = VECTOR_DEF_CAPACITY; + + assert(v->capacity > 0); + + if ((v->container = malloc(sizeof(char *) * v->capacity)) == NULL) + return (false); + + assert(v->container != NULL); + + return (true); +} + +/** + * @brief Remove last element in vector_str. + * @return false at failed, true at success. + */ +bool +vector_str_pop(struct vector_str *v) +{ + + if (v == NULL) + return (false); + + if (v->size == 0) + return (true); + + --v->size; + + free(v->container[v->size]); + v->container[v->size] = NULL; + + return (true); +} + +/** + * @brief Push back string to vector. + * @return false at failed, true at success. + */ +bool +vector_str_push(struct vector_str *v, const char *str, size_t len) +{ + + if (v == NULL || str == NULL) + return (false); + + if (v->size == v->capacity && vector_str_grow(v) == false) + return (false); + + if ((v->container[v->size] = malloc(sizeof(char) * (len + 1))) == NULL) + return (false); + + snprintf(v->container[v->size], len + 1, "%s", str); + + ++v->size; + + return (true); +} + +/** + * @brief Push front org vector to det vector. + * @return false at failed, true at success. + */ +bool +vector_str_push_vector_head(struct vector_str *dst, struct vector_str *org) +{ + size_t i, j, tmp_cap; + char **tmp_ctn; + + if (dst == NULL || org == NULL) + return (false); + + tmp_cap = (dst->size + org->size) * BUFFER_GROWFACTOR; + + if ((tmp_ctn = malloc(sizeof(char *) * tmp_cap)) == NULL) + return (false); + + for (i = 0; i < org->size; ++i) + if ((tmp_ctn[i] = strdup(org->container[i])) == NULL) { + for (j = 0; j < i; ++j) + free(tmp_ctn[j]); + + free(tmp_ctn); + + return (false); + } + + for (i = 0; i < dst->size; ++i) + tmp_ctn[i + org->size] = dst->container[i]; + + free(dst->container); + + dst->container = tmp_ctn; + dst->capacity = tmp_cap; + dst->size += org->size; + + return (true); +} + +/** + * @brief Get new allocated flat string from vector between begin and end. + * + * If r_len is not NULL, string length will be returned. + * @return NULL at failed or NUL terminated new allocated string. + */ +char * +vector_str_substr(const struct vector_str *v, size_t begin, size_t end, + size_t *r_len) +{ + size_t cur, i, len; + char *rtn; + + if (v == NULL || begin > end) + return (NULL); + + len = 0; + for (i = begin; i < end + 1; ++i) + len += strlen(v->container[i]); + + if ((rtn = malloc(sizeof(char) * (len + 1))) == NULL) + return (NULL); + + if (r_len != NULL) + *r_len = len; + + cur = 0; + for (i = begin; i < end + 1; ++i) { + len = strlen(v->container[i]); + memcpy(rtn + cur, v->container[i], len); + cur += len; + } + rtn[cur] = '\0'; + + return (rtn); +} diff --git a/contrib/elftoolchain/libelftc/make-toolchain-version b/contrib/elftoolchain/libelftc/make-toolchain-version new file mode 100755 index 000000000000..ac6155a49a00 --- /dev/null +++ b/contrib/elftoolchain/libelftc/make-toolchain-version @@ -0,0 +1,104 @@ +#!/bin/sh +# +# This script generates a project-wide version identifier for use by +# the `elftc_version()' API. +# +# $Id: make-toolchain-version 2583 2012-09-14 09:49:25Z jkoshy $ + +# +# Defaults. +# +buildhost=`uname -s` +elftcname="elftoolchain" +options="e:h:o:r:t:" +top="" +version="HEAD" +versionfile="elftc_version.c" +progname=`basename ${0}` + +usage() +{ + exec >&2 + + # Print a message, if supplied. + if [ -n "${*}" ]; then echo "##${@}"; fi + + echo "Usage: ${progname} [options]" + echo " Generate a toolchain-wide version number" + echo " -e PROJECTNAME Set the project name [default: ${elftcname}]." + echo " -h HOSTOS Set the build OS [default: ${buildhost}]." + echo " -o OUTPUT Set the output file [default: ${versionfile}]." + echo " -r VERSION Set the version string [default: ${version}]." + echo " -t TOPDIR Set the top-of-tree directory [required]." + exit 1 +} + +# +# Parse options. +# + +while getopts ${options} option +do + case ${option} in + 'e') elftcname="${OPTARG}" ;; + 'h') buildhost="${OPTARG}" ;; + 'o') versionfile="${OPTARG}" ;; + 'r') version="${OPTARG}" ;; + 't') top="${OPTARG}" ;; + '?') usage ;; + esac +done + +[ -n "${top}" ] || usage + +# Try to determine the in-tree revision number. +# +# This script attempts to handle the case where our sources have been +# incorporated into an operating system's base sources. +# +# - If SVN is detected, we use the `svninfo' tool to determine the +# in-tree revision number. +# - If CVS is detected, we use the string `unknown'. +# - Otherwise, we use `git --describe'. + +curdir=`pwd` +cd ${top} || usage "ERROR: Cannot change directory to \"${top}\"." + +if [ -d .svn ]; then # FreeBSD and SF.Net sources. + versionstring=" svn:"$(svnversion) +elif [ -d CVS ]; then # NetBSD. + versionstring=" cvs:unknown" +else # DragonFlyBSD. + versionstring=" git:"$(git describe --all --dirty --long 2> /dev/null) + + # Cannot determine an in-tree version number. + if [ $? -ne 0 ]; then + versionstring="" + fi +fi + +cd ${curdir} || usage "Cannot change back to ${curdir}." + +# +# Only replace the source file if its content has changed. +# +tmpfile=`mktemp ${TMPDIR:-/tmp}/MV.XXXXXXX` +trap "rm -f ${tmpfile};" 0 1 2 3 15 + +cat > ${tmpfile} <<EOF +/* WARNING: Generated by "${progname}". */ + +#include <sys/types.h> +#include <libelftc.h> + +const char * +elftc_version(void) +{ + return "${elftcname} ${version} ${buildhost}${versionstring}"; +} +EOF + +if ! cmp -s ${tmpfile} ${versionfile}; then + echo "@ ${progname}: building \"${versionfile}\"." + cp ${tmpfile} ${versionfile} || exit ${?} +fi diff --git a/contrib/elftoolchain/libelftc/os.FreeBSD.mk b/contrib/elftoolchain/libelftc/os.FreeBSD.mk new file mode 100644 index 000000000000..8079a5b4f2c6 --- /dev/null +++ b/contrib/elftoolchain/libelftc/os.FreeBSD.mk @@ -0,0 +1,7 @@ +# +# Building for a FreeBSD target. +# +# $Id: os.freebsd.mk 189 2008-07-20 10:38:08Z jkoshy $ + +# Symbol versioning support [FreeBSD 7.X and later] +VERSION_MAP= ${.CURDIR}/Version.map diff --git a/contrib/elftoolchain/libelftc/os.Linux.mk b/contrib/elftoolchain/libelftc/os.Linux.mk new file mode 100644 index 000000000000..9730bf6a2788 --- /dev/null +++ b/contrib/elftoolchain/libelftc/os.Linux.mk @@ -0,0 +1,3 @@ +# $Id: os.Linux.mk 994 2010-06-13 10:39:19Z jkoshy $ + +CFLAGS+= -Wall diff --git a/contrib/elftoolchain/nm/Makefile b/contrib/elftoolchain/nm/Makefile new file mode 100644 index 000000000000..6177c61221eb --- /dev/null +++ b/contrib/elftoolchain/nm/Makefile @@ -0,0 +1,13 @@ + +# $Id: Makefile 2076 2011-10-27 03:50:33Z jkoshy $ + +TOP= .. + +PROG= nm +SRCS= nm.c + +WARNS?= 6 + +LDADD= -ldwarf -lelftc -lelf + +.include "${TOP}/mk/elftoolchain.prog.mk" diff --git a/contrib/elftoolchain/nm/nm.1 b/contrib/elftoolchain/nm/nm.1 new file mode 100644 index 000000000000..4cdd73745b60 --- /dev/null +++ b/contrib/elftoolchain/nm/nm.1 @@ -0,0 +1,338 @@ +.\" Copyright (c) 2007 Hyogeol Lee <hyogeollee@gmail.com> +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer +.\" in this position and unchanged. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR +.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +.\" (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: nm.1 3195 2015-05-12 17:22:19Z emaste $ +.\" +.Dd February 15, 2015 +.Os +.Dt NM 1 +.Sh NAME +.Nm nm +.Nd display symbolic information in object files +.Sh SYNOPSIS +.Nm +.Op Fl -debug-syms +.Op Fl -defined-only +.Op Fl -demangle Ns Op = Ns style +.Op Fl -dynamic +.Op Fl -extern-only +.Op Fl -help +.Op Fl -line-numbers +.Op Fl -no-demangle +.Op Fl -no-sort +.Op Fl -numeric-sort +.Op Fl -print-armap +.Op Fl -print-file-name +.Op Fl -print-size +.Op Fl -radix= Ns Ar format +.Op Fl -reverse-sort +.Op Fl -size-sort +.Op Fl -undefined-only +.Op Fl -version +.Op Fl A +.Op Fl B +.Op Fl C Op Ar style +.Op Fl D +.Op Fl P +.Op Fl V +.Op Fl a +.Op Fl e +.Op Fl g +.Op Fl h +.Op Fl l +.Op Fl n +.Op Fl o +.Op Fl p +.Op Fl r +.Op Fl S +.Op Fl s +.Op Fl t Ar format +.Op Fl u +.Op Fl x +.Ar +.Sh DESCRIPTION +The +.Nm +utility displays symbolic information in the object files, +executables, and object library files named by its arguments. +Lack of symbolic information in an otherwise valid input +file, is not considered to be an error. +If no files are specified on the command line, +.Nm +will attempt to read +.Pa a.out . +.Pp +The +.Nm +utility recognizes the following options: +.Bl -tag -width ".Fl d Ar argument" +.It Fl -debug-syms +Display all symbols, including debugger-only symbols. +.It Fl -defined-only +Display only defined symbols. +.It Fl -demangle Ns Op = Ns Ar style +Decode (demangle) low-level symbol names into human-readable names. +Supported values for argument +.Ar style +are +.Sq auto , +.Sq gnu-v2 , +.Sq gnu-v3 +and +.Sq arm. +If argument +.Ar style +is not specified, it is taken to be +.Sq auto . +.It Fl -dynamic +Only display dynamic symbols. +This option is only meaningful for shared libraries. +.It Fl -extern-only +Only display information about global (external) symbols. +.It Fl -help +Display a help message and exit. +.It Fl -format Ns = Ns Ar format +Display output in the format specified by argument +.Ar format . +Supported values for the format argument are +.Sq bsd , +.Sq sysv , +and +.Sq posix . +The default output format is +.Sq bsd . +.It Fl -line-numbers +Display the filename and line number associated a symbol using +any debugging information present in the input file. +For defined symbols, look up the line number associated with +the address of the symbol. +For undefined symbols, look up the line number associated with +a relocation entry that refers to the symbol. +If line number information can be determined, it is displayed after +other symbol information. +.It Fl -no-demangle +Do not demangle symbol names (default). +.It Fl -no-sort +Do not sort symbols. +.It Fl -numeric-sort +Sort symbols numerically by address instead of alphabetically by name. +.It Fl -print-armap +For +.Xr ar 1 +archives, include the index of the archive's members. +.It Fl -print-file-name +Write the full pathname or library name of an object on each line, +before the rest of the information for a symbol. +If this option is not specified, +.Nm +will only identify an input file once, before its symbols are +listed. +.It Fl -print-size +Print the size of each symbol instead of its value. +.It Fl -radix Ns = Ns Ar radix +Print numeric values using the specified radix. +Supported values for argument +.Ar radix +are +.Sq d +for decimal, +.Sq o +for octal, and +.Sq x +for hexadecimal. +.It Fl -reverse-sort +Reverse the order of the sort. +.It Fl -size-sort +Sort symbols by size instead of alphabetically by name. +.It Fl -undefined-only +Display only undefined symbols. +.It Fl -version +Display the version identifier for +.Nm +and exit. +.It Fl A +Equivalent to specifying option +.Fl -print-file-name . +.It Fl B +Equivalent to specifying option +.Fl -format= Ns Ar bsd . +.It Fl C Op Ar style +Equivalent to specifying option +.Fl -demangle Ns Op = Ns Ar style . +.It Fl D +Equivalent to specifying option +.Fl -dynamic . +.It Fl F Ar format +Equivalent to specifying option +.Fl -format Ns = Ns Ar format . +.It Fl P +Equivalent to specifying option +.Fl -format Ns = Ns Ar posix . +.It Fl S +Equivalent to specifying option +.Fl -print-size . +.It Fl V +Equivalent to specifying option +.Fl -version . +.It Fl a +Equivalent to specifying option +.Fl -debug-syms . +.It Fl e +Only display information for global and static symbols. +.It Fl f +Produce full output (default). +.It Fl g +Equivalent to specifying option +.Fl -extern-only . +.It Fl h +Equivalent to specifying option +.Fl -help . +.It Fl l +Equivalent to specifying option +.Fl -line-numbers . +.It Fl n +Equivalent to specifying option +.Fl -numeric-sort . +.It Fl o +If POSIX output was specified using the +.Fl F Ar posix +or +.Fl P +options, this option is equivalent to specifying +.Fl -radix Ns = Ns Sq Ar o . +If POSIX output was not specified, this option +acts as a synonym for the +.Fl -print-file-name +option. +.It Fl p +Equivalent to specifying option +.Fl -no-sort . +.It Fl v +Equivalent to option +.Fl n . +.It Fl r +Equivalent to specifying option +.Fl -reverse-sort +.It Fl s +Equivalent to specifying option +.Fl -print-armap . +.It Fl t Ar radix +Equivalent to specifying option +.Fl -radix= Ns Ar radix . +.It Fl u +Equivalent to specifying option +.Fl -undefined-only . +.It Fl x +Write numeric values in hexadecimal (equivalent to -t x). +.El +.Sh OUTPUT FORMAT +.Pp +The +.Nm +utility can present its information in a number of formats, numeric +radices and sort orders. +By default +.Nm +uses BSD style output, a hexadecimal radix, without output sorted +alphabetically by name and without demangling of names. +.Pp +For each symbol listed, +.Nm +presents the following information: +.Bl -bullet -compact +.It +The library or object name, if options +.Fl A +or +.Fl -print-file-name +were specified. +.It +The symbol name. +.It +The type of the symbol denoted by a single character as below: +.Bl -tag -compact -width indent +.It A +A global, absolute symbol. +.It B +A global +.Dq bss +(uninitialized data) symbol. +.It C +A +.Dq common +symbol, representing uninitialized data. +.It D +A global symbol naming initialized data. +.It N +A debugger symbol. +.It R +A read-only data symbol. +.It T +A global text symbol. +.It U +An undefined symbol. +.It V +A weak object. +.It W +A weak reference. +.It a +A local absolute symbol. +.It b +A local +.Dq bss +(uninitialized data) symbol. +.It d +A local data symbol. +.It t +A local text symbol. +.It v +A weak object that is undefined. +.It w +A weak symbol that is undefined. +.It ? +None of the above. +.El +.It +The value of the symbol. +.It +The size of the symbol if applicable. +.It +Line number information, if available and if options +.Fl l +or +.Fl -line-numbers +were specified. +.El +.Sh EXIT STATUS +.Ex -std +.Sh SEE ALSO +.Xr ar 1 , +.Xr objdump 1 , +.Xr ranlib 1 , +.Xr elf 3 +.Sh AUTHORS +The +.Nm +utility and this manual page were written by +.An Hyogeol Lee Aq Mt hyogeollee@gmail.com . diff --git a/contrib/elftoolchain/nm/nm.c b/contrib/elftoolchain/nm/nm.c new file mode 100644 index 000000000000..87a456ee97cb --- /dev/null +++ b/contrib/elftoolchain/nm/nm.c @@ -0,0 +1,2119 @@ +/*- + * Copyright (c) 2007 Hyogeol Lee <hyogeollee@gmail.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer + * in this position and unchanged. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/queue.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <ar.h> +#include <assert.h> +#include <ctype.h> +#include <dwarf.h> +#include <err.h> +#include <errno.h> +#include <fcntl.h> +#include <gelf.h> +#include <getopt.h> +#include <inttypes.h> +#include <libdwarf.h> +#include <libelftc.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <strings.h> +#include <unistd.h> + +#include "_elftc.h" + +ELFTC_VCSID("$Id: nm.c 3179 2015-03-31 19:38:56Z emaste $"); + +/* symbol information list */ +STAILQ_HEAD(sym_head, sym_entry); + +struct sym_entry { + char *name; + GElf_Sym *sym; + STAILQ_ENTRY(sym_entry) sym_entries; +}; + +typedef int (*fn_sort)(const void *, const void *); +typedef void (*fn_elem_print)(char, const char *, const GElf_Sym *, const char *); +typedef void (*fn_sym_print)(const GElf_Sym *); +typedef int (*fn_filter)(char, const GElf_Sym *, const char *); + +/* output filter list */ +static SLIST_HEAD(filter_head, filter_entry) nm_out_filter = + SLIST_HEAD_INITIALIZER(nm_out_filter); + +struct filter_entry { + fn_filter fn; + SLIST_ENTRY(filter_entry) filter_entries; +}; + +struct sym_print_data { + struct sym_head *headp; + size_t sh_num, list_num; + const char *t_table, **s_table, *filename, *objname; +}; + +struct nm_prog_info { + const char *name; + const char *def_filename; +}; + +/* List for line number information. */ +struct line_info_entry { + uint64_t addr; /* address */ + uint64_t line; /* line number */ + char *file; /* file name with path */ + SLIST_ENTRY(line_info_entry) entries; +}; +SLIST_HEAD(line_info_head, line_info_entry); + +/* List for function line number information. */ +struct func_info_entry { + char *name; /* function name */ + char *file; /* file name with path */ + uint64_t lowpc; /* low address */ + uint64_t highpc; /* high address */ + uint64_t line; /* line number */ + SLIST_ENTRY(func_info_entry) entries; +}; +SLIST_HEAD(func_info_head, func_info_entry); + +/* List for variable line number information. */ +struct var_info_entry { + char *name; /* variable name */ + char *file; /* file name with path */ + uint64_t addr; /* address */ + uint64_t line; /* line number */ + SLIST_ENTRY(var_info_entry) entries; +}; +SLIST_HEAD(var_info_head, var_info_entry); + +/* output numric type */ +enum radix { + RADIX_OCT, + RADIX_HEX, + RADIX_DEC +}; + +/* output symbol type, PRINT_SYM_DYN for dynamic symbol only */ +enum print_symbol { + PRINT_SYM_SYM, + PRINT_SYM_DYN +}; + +/* output name type */ +enum print_name { + PRINT_NAME_NONE, + PRINT_NAME_FULL, + PRINT_NAME_MULTI +}; + +struct nm_prog_options { + enum print_symbol print_symbol; + enum print_name print_name; + enum radix t; + int demangle_type; + bool print_debug; + bool print_armap; + int print_size; + bool debug_line; + int def_only; + bool undef_only; + int sort_size; + bool sort_reverse; + int no_demangle; + + /* + * function pointer to sort symbol list. + * possible function - cmp_name, cmp_none, cmp_size, cmp_value + */ + fn_sort sort_fn; + + /* + * function pointer to print symbol elem. + * possible function - sym_elem_print_all + * sym_elem_print_all_portable + * sym_elem_print_all_sysv + */ + fn_elem_print elem_print_fn; + + fn_sym_print value_print_fn; + fn_sym_print size_print_fn; +}; + +#define CHECK_SYM_PRINT_DATA(p) (p->headp == NULL || p->sh_num == 0 || \ +p->t_table == NULL || p->s_table == NULL || p->filename == NULL) +#define IS_SYM_TYPE(t) ((t) == '?' || isalpha((t)) != 0) +#define IS_UNDEF_SYM_TYPE(t) ((t) == 'U' || (t) == 'v' || (t) == 'w') +#define UNUSED(p) ((void)p) + +static int cmp_name(const void *, const void *); +static int cmp_none(const void *, const void *); +static int cmp_size(const void *, const void *); +static int cmp_value(const void *, const void *); +static void filter_dest(void); +static int filter_insert(fn_filter); +static void get_opt(int, char **); +static int get_sym(Elf *, struct sym_head *, int, size_t, size_t, + const char *, const char **, int); +static const char * get_sym_name(Elf *, const GElf_Sym *, size_t, + const char **, int); +static char get_sym_type(const GElf_Sym *, const char *); +static void global_dest(void); +static void global_init(void); +static bool is_sec_data(GElf_Shdr *); +static bool is_sec_debug(const char *); +static bool is_sec_nobits(GElf_Shdr *); +static bool is_sec_readonly(GElf_Shdr *); +static bool is_sec_text(GElf_Shdr *); +static void print_ar_index(int, Elf *); +static void print_header(const char *, const char *); +static void print_version(void); +static int read_elf(Elf *, const char *, Elf_Kind); +static int read_object(const char *); +static int read_files(int, char **); +static void set_opt_value_print_fn(enum radix); +static int sym_elem_def(char, const GElf_Sym *, const char *); +static int sym_elem_global(char, const GElf_Sym *, const char *); +static int sym_elem_global_static(char, const GElf_Sym *, + const char *); +static int sym_elem_nondebug(char, const GElf_Sym *, const char *); +static int sym_elem_nonzero_size(char, const GElf_Sym *, + const char *); +static void sym_elem_print_all(char, const char *, + const GElf_Sym *, const char *); +static void sym_elem_print_all_portable(char, const char *, + const GElf_Sym *, const char *); +static void sym_elem_print_all_sysv(char, const char *, + const GElf_Sym *, const char *); +static int sym_elem_undef(char, const GElf_Sym *, const char *); +static void sym_list_dest(struct sym_head *); +static int sym_list_insert(struct sym_head *, const char *, + const GElf_Sym *); +static void sym_list_print(struct sym_print_data *, + struct func_info_head *, struct var_info_head *, + struct line_info_head *); +static void sym_list_print_each(struct sym_entry *, + struct sym_print_data *, struct func_info_head *, + struct var_info_head *, struct line_info_head *); +static struct sym_entry *sym_list_sort(struct sym_print_data *); +static void sym_size_oct_print(const GElf_Sym *); +static void sym_size_hex_print(const GElf_Sym *); +static void sym_size_dec_print(const GElf_Sym *); +static void sym_value_oct_print(const GElf_Sym *); +static void sym_value_hex_print(const GElf_Sym *); +static void sym_value_dec_print(const GElf_Sym *); +static void usage(int); + +static struct nm_prog_info nm_info; +static struct nm_prog_options nm_opts; +static int nm_elfclass; + +/* + * Point to current sym_print_data to use portable qsort function. + * (e.g. There is no qsort_r function in NetBSD.) + * + * Using in sym_list_sort. + */ +static struct sym_print_data *nm_print_data; + +static const struct option nm_longopts[] = { + { "debug-syms", no_argument, NULL, 'a' }, + { "defined-only", no_argument, &nm_opts.def_only, 1}, + { "demangle", optional_argument, NULL, 'C' }, + { "dynamic", no_argument, NULL, 'D' }, + { "extern-only", no_argument, NULL, 'g' }, + { "format", required_argument, NULL, 'F' }, + { "help", no_argument, NULL, 'h' }, + { "line-numbers", no_argument, NULL, 'l' }, + { "no-demangle", no_argument, &nm_opts.no_demangle, + 1}, + { "no-sort", no_argument, NULL, 'p' }, + { "numeric-sort", no_argument, NULL, 'v' }, + { "print-armap", no_argument, NULL, 's' }, + { "print-file-name", no_argument, NULL, 'A' }, + { "print-size", no_argument, NULL, 'S' }, + { "radix", required_argument, NULL, 't' }, + { "reverse-sort", no_argument, NULL, 'r' }, + { "size-sort", no_argument, &nm_opts.sort_size, 1}, + { "undefined-only", no_argument, NULL, 'u' }, + { "version", no_argument, NULL, 'V' }, + { NULL, 0, NULL, 0 } +}; + +#if defined(ELFTC_NEED_BYTEORDER_EXTENSIONS) +static __inline uint32_t +be32dec(const void *pp) +{ + unsigned char const *p = (unsigned char const *)pp; + + return ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]); +} + +static __inline uint32_t +le32dec(const void *pp) +{ + unsigned char const *p = (unsigned char const *)pp; + + return ((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]); +} + +static __inline uint64_t +be64dec(const void *pp) +{ + unsigned char const *p = (unsigned char const *)pp; + + return (((uint64_t)be32dec(p) << 32) | be32dec(p + 4)); +} + +static __inline uint64_t +le64dec(const void *pp) +{ + unsigned char const *p = (unsigned char const *)pp; + + return (((uint64_t)le32dec(p + 4) << 32) | le32dec(p)); +} +#endif + +static int +cmp_name(const void *l, const void *r) +{ + + assert(l != NULL); + assert(r != NULL); + assert(((const struct sym_entry *)l)->name != NULL); + assert(((const struct sym_entry *)r)->name != NULL); + + return (strcmp(((const struct sym_entry *)l)->name, + ((const struct sym_entry *)r)->name)); +} + +static int +cmp_none(const void *l, const void *r) +{ + + UNUSED(l); + UNUSED(r); + + return (0); +} + +/* Size comparison. If l and r have same size, compare their name. */ +static int +cmp_size(const void *lp, const void *rp) +{ + const struct sym_entry *l, *r; + + l = lp; + r = rp; + + assert(l != NULL); + assert(l->name != NULL); + assert(l->sym != NULL); + assert(r != NULL); + assert(r->name != NULL); + assert(r->sym != NULL); + + if (l->sym->st_size == r->sym->st_size) + return (strcmp(l->name, r->name)); + + return (l->sym->st_size - r->sym->st_size); +} + +/* Value comparison. Undefined symbols come first. */ +static int +cmp_value(const void *lp, const void *rp) +{ + const struct sym_entry *l, *r; + const char *ttable; + int l_is_undef, r_is_undef; + + l = lp; + r = rp; + + assert(nm_print_data != NULL); + ttable = nm_print_data->t_table; + + assert(l != NULL); + assert(l->name != NULL); + assert(l->sym != NULL); + assert(r != NULL); + assert(r->name != NULL); + assert(r->sym != NULL); + assert(ttable != NULL); + + l_is_undef = IS_UNDEF_SYM_TYPE(get_sym_type(l->sym, ttable)) ? 1 : 0; + r_is_undef = IS_UNDEF_SYM_TYPE(get_sym_type(r->sym, ttable)) ? 1 : 0; + + assert(l_is_undef + r_is_undef >= 0); + assert(l_is_undef + r_is_undef <= 2); + + switch (l_is_undef + r_is_undef) { + case 0: + /* Both defined */ + if (l->sym->st_value == r->sym->st_value) + return (strcmp(l->name, r->name)); + return (l->sym->st_value > r->sym->st_value ? 1 : -1); + case 1: + /* One undefined */ + return (l_is_undef == 0 ? 1 : -1); + case 2: + /* Both undefined */ + return (strcmp(l->name, r->name)); + } + /* NOTREACHED */ + + return (l->sym->st_value - r->sym->st_value); +} + +static void +filter_dest(void) +{ + struct filter_entry *e; + + while (!SLIST_EMPTY(&nm_out_filter)) { + e = SLIST_FIRST(&nm_out_filter); + SLIST_REMOVE_HEAD(&nm_out_filter, filter_entries); + free(e); + } +} + +static int +filter_insert(fn_filter filter_fn) +{ + struct filter_entry *e; + + assert(filter_fn != NULL); + + if ((e = malloc(sizeof(struct filter_entry))) == NULL) { + warn("malloc"); + return (0); + } + e->fn = filter_fn; + SLIST_INSERT_HEAD(&nm_out_filter, e, filter_entries); + + return (1); +} + +static int +parse_demangle_option(const char *opt) +{ + + if (opt == NULL) + return (ELFTC_DEM_UNKNOWN); + else if (!strncasecmp(opt, "gnu-v2", 6)) + return (ELFTC_DEM_GNU2); + else if (!strncasecmp(opt, "gnu-v3", 6)) + return (ELFTC_DEM_GNU3); + else if (!strncasecmp(opt, "arm", 3)) + return (ELFTC_DEM_ARM); + else + errx(EXIT_FAILURE, "unknown demangling style '%s'", opt); + + /* NOTREACHED */ + return (0); +} + +static void +get_opt(int argc, char **argv) +{ + int ch; + bool is_posix, oflag; + + if (argc <= 0 || argv == NULL) + return; + + oflag = is_posix = false; + nm_opts.t = RADIX_HEX; + while ((ch = getopt_long(argc, argv, "ABCDF:PSVaefghlnoprst:uvx", + nm_longopts, NULL)) != -1) { + switch (ch) { + case 'A': + nm_opts.print_name = PRINT_NAME_FULL; + break; + case 'B': + nm_opts.elem_print_fn = &sym_elem_print_all; + break; + case 'C': + nm_opts.demangle_type = parse_demangle_option(optarg); + break; + case 'D': + nm_opts.print_symbol = PRINT_SYM_DYN; + break; + case 'F': + /* sysv, bsd, posix */ + switch (optarg[0]) { + case 'B': + case 'b': + nm_opts.elem_print_fn = &sym_elem_print_all; + break; + case 'P': + case 'p': + is_posix = true; + nm_opts.elem_print_fn = + &sym_elem_print_all_portable; + break; + case 'S': + case 's': + nm_opts.elem_print_fn = + &sym_elem_print_all_sysv; + break; + default: + warnx("%s: Invalid format", optarg); + usage(1); + } + + break; + case 'P': + is_posix = true; + nm_opts.elem_print_fn = &sym_elem_print_all_portable; + break; + case 'S': + nm_opts.print_size = 1; + break; + case 'V': + print_version(); + /* NOTREACHED */ + case 'a': + nm_opts.print_debug = true; + break; + case 'e': + filter_insert(sym_elem_global_static); + break; + case 'f': + break; + case 'g': + filter_insert(sym_elem_global); + break; + case 'h': + usage(0); + break; + case 'l': + nm_opts.debug_line = true; + break; + case 'n': + case 'v': + nm_opts.sort_fn = &cmp_value; + break; + case 'o': + oflag = true; + break; + case 'p': + nm_opts.sort_fn = &cmp_none; + break; + case 'r': + nm_opts.sort_reverse = true; + break; + case 's': + nm_opts.print_armap = true; + break; + case 't': + /* t require always argument to getopt_long */ + switch (optarg[0]) { + case 'd': + nm_opts.t = RADIX_DEC; + break; + case 'o': + nm_opts.t = RADIX_OCT; + break; + case 'x': + nm_opts.t = RADIX_HEX; + break; + default: + warnx("%s: Invalid radix", optarg); + usage(1); + } + break; + case 'u': + filter_insert(sym_elem_undef); + nm_opts.undef_only = true; + break; + /* case 'v': see case 'n' above. */ + case 'x': + nm_opts.t = RADIX_HEX; + break; + case 0: + if (nm_opts.sort_size != 0) { + nm_opts.sort_fn = &cmp_size; + filter_insert(sym_elem_def); + filter_insert(sym_elem_nonzero_size); + } + if (nm_opts.def_only != 0) + filter_insert(sym_elem_def); + if (nm_opts.no_demangle != 0) + nm_opts.demangle_type = -1; + break; + default : + usage(1); + } + } + + /* + * In POSIX mode, the '-o' option controls the output radix. + * In non-POSIX mode, the option is a synonym for the '-A' and + * '--print-file-name' options. + */ + if (oflag) { + if (is_posix) + nm_opts.t = RADIX_OCT; + else + nm_opts.print_name = PRINT_NAME_FULL; + } + + assert(nm_opts.sort_fn != NULL && "nm_opts.sort_fn is null"); + assert(nm_opts.elem_print_fn != NULL && + "nm_opts.elem_print_fn is null"); + assert(nm_opts.value_print_fn != NULL && + "nm_opts.value_print_fn is null"); + + set_opt_value_print_fn(nm_opts.t); + + if (nm_opts.undef_only == true) { + if (nm_opts.sort_fn == &cmp_size) + errx(EXIT_FAILURE, + "--size-sort with -u is meaningless"); + if (nm_opts.def_only != 0) + errx(EXIT_FAILURE, + "-u with --defined-only is meaningless"); + } + if (nm_opts.print_debug == false) + filter_insert(sym_elem_nondebug); + if (nm_opts.sort_reverse == true && nm_opts.sort_fn == cmp_none) + nm_opts.sort_reverse = false; +} + +/* + * Get symbol information from elf. + */ +static int +get_sym(Elf *elf, struct sym_head *headp, int shnum, size_t dynndx, + size_t strndx, const char *type_table, const char **sec_table, + int sec_table_size) +{ + Elf_Scn *scn; + Elf_Data *data; + GElf_Shdr shdr; + GElf_Sym sym; + struct filter_entry *fep; + size_t ndx; + int rtn; + const char *sym_name; + char type; + bool filter; + int i, j; + + assert(elf != NULL); + assert(headp != NULL); + + rtn = 0; + for (i = 1; i < shnum; i++) { + if ((scn = elf_getscn(elf, i)) == NULL) { + warnx("elf_getscn failed: %s", elf_errmsg(-1)); + continue; + } + if (gelf_getshdr(scn, &shdr) != &shdr) { + warnx("gelf_getshdr failed: %s", elf_errmsg(-1)); + continue; + } + if (shdr.sh_type == SHT_SYMTAB) { + if (nm_opts.print_symbol != PRINT_SYM_SYM) + continue; + } else if (shdr.sh_type == SHT_DYNSYM) { + if (nm_opts.print_symbol != PRINT_SYM_DYN) + continue; + } else + continue; + + ndx = shdr.sh_type == SHT_DYNSYM ? dynndx : strndx; + + data = NULL; + while ((data = elf_getdata(scn, data)) != NULL) { + j = 1; + while (gelf_getsym(data, j++, &sym) != NULL) { + sym_name = get_sym_name(elf, &sym, ndx, + sec_table, sec_table_size); + filter = false; + type = get_sym_type(&sym, type_table); + SLIST_FOREACH(fep, &nm_out_filter, + filter_entries) { + if (!fep->fn(type, &sym, sym_name)) { + filter = true; + break; + } + } + if (filter == false) { + if (sym_list_insert(headp, sym_name, + &sym) == 0) + return (0); + rtn++; + } + } + } + } + + return (rtn); +} + +static const char * +get_sym_name(Elf *elf, const GElf_Sym *sym, size_t ndx, const char **sec_table, + int sec_table_size) +{ + const char *sym_name; + + sym_name = NULL; + + /* Show section name as symbol name for STT_SECTION symbols. */ + if (GELF_ST_TYPE(sym->st_info) == STT_SECTION) { + if (sec_table != NULL && sym->st_shndx < sec_table_size) + sym_name = sec_table[sym->st_shndx]; + } else + sym_name = elf_strptr(elf, ndx, sym->st_name); + + if (sym_name == NULL) + sym_name = "(null)"; + + return (sym_name); +} + +static char +get_sym_type(const GElf_Sym *sym, const char *type_table) +{ + bool is_local; + + if (sym == NULL || type_table == NULL) + return ('?'); + + is_local = sym->st_info >> 4 == STB_LOCAL; + + if (sym->st_shndx == SHN_ABS) /* absolute */ + return (is_local ? 'a' : 'A'); + + if (sym->st_shndx == SHN_COMMON) /* common */ + return ('C'); + + if ((sym->st_info) >> 4 == STB_WEAK) { /* weak */ + if ((sym->st_info & 0xf) == STT_OBJECT) + return (sym->st_shndx == SHN_UNDEF ? 'v' : 'V'); + + return (sym->st_shndx == SHN_UNDEF ? 'w' : 'W'); + } + + if (sym->st_shndx == SHN_UNDEF) /* undefined */ + return ('U'); + + return (is_local == true && type_table[sym->st_shndx] != 'N' ? + tolower((unsigned char) type_table[sym->st_shndx]) : + type_table[sym->st_shndx]); +} + +static void +global_dest(void) +{ + + filter_dest(); +} + +static void +global_init(void) +{ + + if (elf_version(EV_CURRENT) == EV_NONE) + errx(EXIT_FAILURE, "elf_version error"); + + nm_info.name = ELFTC_GETPROGNAME(); + nm_info.def_filename = "a.out"; + nm_opts.print_symbol = PRINT_SYM_SYM; + nm_opts.print_name = PRINT_NAME_NONE; + nm_opts.demangle_type = -1; + nm_opts.print_debug = false; + nm_opts.print_armap = false; + nm_opts.print_size = 0; + nm_opts.debug_line = false; + nm_opts.def_only = 0; + nm_opts.undef_only = false; + nm_opts.sort_size = 0; + nm_opts.sort_reverse = false; + nm_opts.no_demangle = 0; + nm_opts.sort_fn = &cmp_name; + nm_opts.elem_print_fn = &sym_elem_print_all; + nm_opts.value_print_fn = &sym_value_dec_print; + nm_opts.size_print_fn = &sym_size_dec_print; + SLIST_INIT(&nm_out_filter); +} + +static bool +is_sec_data(GElf_Shdr *s) +{ + + assert(s != NULL && "shdr is NULL"); + + return (((s->sh_flags & SHF_ALLOC) != 0) && s->sh_type != SHT_NOBITS); +} + +static bool +is_sec_debug(const char *shname) +{ + const char *dbg_sec[] = { + ".debug", + ".gnu.linkonce.wi.", + ".line", + ".rel.debug", + ".rela.debug", + ".stab", + NULL + }; + const char **p; + + assert(shname != NULL && "shname is NULL"); + + for (p = dbg_sec; *p; p++) { + if (!strncmp(shname, *p, strlen(*p))) + return (true); + } + + return (false); +} + +static bool +is_sec_nobits(GElf_Shdr *s) +{ + + assert(s != NULL && "shdr is NULL"); + + return (s->sh_type == SHT_NOBITS); +} + +static bool +is_sec_readonly(GElf_Shdr *s) +{ + + assert(s != NULL && "shdr is NULL"); + + return ((s->sh_flags & SHF_WRITE) == 0); +} + +static bool +is_sec_text(GElf_Shdr *s) +{ + + assert(s != NULL && "shdr is NULL"); + + return ((s->sh_flags & SHF_EXECINSTR) != 0); +} + +static void +print_ar_index(int fd, Elf *arf) +{ + Elf *elf; + Elf_Arhdr *arhdr; + Elf_Arsym *arsym; + Elf_Cmd cmd; + off_t start; + size_t arsym_size; + + if (arf == NULL) + return; + + if ((arsym = elf_getarsym(arf, &arsym_size)) == NULL) + return; + + printf("\nArchive index:\n"); + + start = arsym->as_off; + cmd = ELF_C_READ; + while (arsym_size > 1) { + if (elf_rand(arf, arsym->as_off) == arsym->as_off && + (elf = elf_begin(fd, cmd, arf)) != NULL) { + if ((arhdr = elf_getarhdr(elf)) != NULL) + printf("%s in %s\n", arsym->as_name, + arhdr->ar_name != NULL ? + arhdr->ar_name : arhdr->ar_rawname); + elf_end(elf); + } + ++arsym; + --arsym_size; + } + + elf_rand(arf, start); +} + +#define DEMANGLED_BUFFER_SIZE (8 * 1024) +#define PRINT_DEMANGLED_NAME(FORMAT, NAME) do { \ + char _demangled[DEMANGLED_BUFFER_SIZE]; \ + if (nm_opts.demangle_type < 0 || \ + elftc_demangle((NAME), _demangled, sizeof(_demangled), \ + nm_opts.demangle_type) < 0) \ + printf((FORMAT), (NAME)); \ + else \ + printf((FORMAT), _demangled); \ + } while (0) + +static void +print_header(const char *file, const char *obj) +{ + + if (file == NULL) + return; + + if (nm_opts.elem_print_fn == &sym_elem_print_all_sysv) { + printf("\n\n%s from %s", + nm_opts.undef_only == false ? "Symbols" : + "Undefined symbols", file); + if (obj != NULL) + printf("[%s]", obj); + printf(":\n\n"); + + printf("\ +Name Value Class Type Size Line Section\n\n"); + } else { + /* archive file without -A option and POSIX */ + if (nm_opts.print_name != PRINT_NAME_FULL && obj != NULL) { + if (nm_opts.elem_print_fn == + sym_elem_print_all_portable) + printf("%s[%s]:\n", file, obj); + else if (nm_opts.elem_print_fn == sym_elem_print_all) + printf("\n%s:\n", obj); + /* multiple files(not archive) without -A option */ + } else if (nm_opts.print_name == PRINT_NAME_MULTI) { + if (nm_opts.elem_print_fn == sym_elem_print_all) + printf("\n"); + printf("%s:\n", file); + } + } +} + +static void +print_version(void) +{ + + (void) printf("%s (%s)\n", nm_info.name, elftc_version()); + exit(0); +} + +static uint64_t +get_block_value(Dwarf_Debug dbg, Dwarf_Block *block) +{ + Elf *elf; + GElf_Ehdr eh; + Dwarf_Error de; + + if (dwarf_get_elf(dbg, &elf, &de) != DW_DLV_OK) { + warnx("dwarf_get_elf failed: %s", dwarf_errmsg(de)); + return (0); + } + + if (gelf_getehdr(elf, &eh) != &eh) { + warnx("gelf_getehdr failed: %s", elf_errmsg(-1)); + return (0); + } + + if (block->bl_len == 5) { + if (eh.e_ident[EI_DATA] == ELFDATA2LSB) + return (le32dec((uint8_t *) block->bl_data + 1)); + else + return (be32dec((uint8_t *) block->bl_data + 1)); + } else if (block->bl_len == 9) { + if (eh.e_ident[EI_DATA] == ELFDATA2LSB) + return (le64dec((uint8_t *) block->bl_data + 1)); + else + return (be64dec((uint8_t *) block->bl_data + 1)); + } + + return (0); +} + +static char * +find_object_name(Dwarf_Debug dbg, Dwarf_Die die) +{ + Dwarf_Die ret_die; + Dwarf_Attribute at; + Dwarf_Off off; + Dwarf_Error de; + const char *str; + char *name; + + if (dwarf_attrval_string(die, DW_AT_name, &str, &de) == DW_DLV_OK) { + if ((name = strdup(str)) == NULL) { + warn("strdup"); + return (NULL); + } + return (name); + } + + if (dwarf_attr(die, DW_AT_specification, &at, &de) != DW_DLV_OK) + return (NULL); + + if (dwarf_global_formref(at, &off, &de) != DW_DLV_OK) + return (NULL); + + if (dwarf_offdie(dbg, off, &ret_die, &de) != DW_DLV_OK) + return (NULL); + + return (find_object_name(dbg, ret_die)); +} + +static void +search_line_attr(Dwarf_Debug dbg, struct func_info_head *func_info, + struct var_info_head *var_info, Dwarf_Die die, char **src_files, + Dwarf_Signed filecount) +{ + Dwarf_Attribute at; + Dwarf_Unsigned udata; + Dwarf_Half tag; + Dwarf_Block *block; + Dwarf_Bool flag; + Dwarf_Die ret_die; + Dwarf_Error de; + struct func_info_entry *func; + struct var_info_entry *var; + int ret; + + if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) { + warnx("dwarf_tag failed: %s", dwarf_errmsg(de)); + goto cont_search; + } + + /* We're interested in DIEs which define functions or variables. */ + if (tag != DW_TAG_subprogram && tag != DW_TAG_entry_point && + tag != DW_TAG_inlined_subroutine && tag != DW_TAG_variable) + goto cont_search; + + if (tag == DW_TAG_variable) { + + /* Ignore "artificial" variable. */ + if (dwarf_attrval_flag(die, DW_AT_artificial, &flag, &de) == + DW_DLV_OK && flag) + goto cont_search; + + /* Ignore pure declaration. */ + if (dwarf_attrval_flag(die, DW_AT_declaration, &flag, &de) == + DW_DLV_OK && flag) + goto cont_search; + + /* Ignore stack varaibles. */ + if (dwarf_attrval_flag(die, DW_AT_external, &flag, &de) != + DW_DLV_OK || !flag) + goto cont_search; + + if ((var = calloc(1, sizeof(*var))) == NULL) { + warn("calloc failed"); + goto cont_search; + } + + if (dwarf_attrval_unsigned(die, DW_AT_decl_file, &udata, + &de) == DW_DLV_OK && udata > 0 && + (Dwarf_Signed) (udata - 1) < filecount) { + var->file = strdup(src_files[udata - 1]); + if (var->file == NULL) { + warn("strdup"); + free(var); + goto cont_search; + } + } + + if (dwarf_attrval_unsigned(die, DW_AT_decl_line, &udata, &de) == + DW_DLV_OK) + var->line = udata; + + var->name = find_object_name(dbg, die); + if (var->name == NULL) { + if (var->file) + free(var->file); + free(var); + goto cont_search; + } + + if (dwarf_attr(die, DW_AT_location, &at, &de) == DW_DLV_OK && + dwarf_formblock(at, &block, &de) == DW_DLV_OK) { + /* + * Since we ignored stack variables, the rest are the + * external varaibles which should always use DW_OP_addr + * operator for DW_AT_location value. + */ + if (*((uint8_t *)block->bl_data) == DW_OP_addr) + var->addr = get_block_value(dbg, block); + } + + SLIST_INSERT_HEAD(var_info, var, entries); + + } else { + + if ((func = calloc(1, sizeof(*func))) == NULL) { + warn("calloc failed"); + goto cont_search; + } + + /* + * Note that dwarf_attrval_unsigned() handles DW_AT_abstract_origin + * internally, so it can retrieve DW_AT_decl_file/DW_AT_decl_line + * attributes for inlined functions as well. + */ + if (dwarf_attrval_unsigned(die, DW_AT_decl_file, &udata, + &de) == DW_DLV_OK && udata > 0 && + (Dwarf_Signed) (udata - 1) < filecount) { + func->file = strdup(src_files[udata - 1]); + if (func->file == NULL) { + warn("strdup"); + free(func); + goto cont_search; + } + } + + if (dwarf_attrval_unsigned(die, DW_AT_decl_line, &udata, &de) == + DW_DLV_OK) + func->line = udata; + + func->name = find_object_name(dbg, die); + if (func->name == NULL) { + if (func->file) + free(func->file); + free(func); + goto cont_search; + } + + if (dwarf_attrval_unsigned(die, DW_AT_low_pc, &udata, &de) == + DW_DLV_OK) + func->lowpc = udata; + if (dwarf_attrval_unsigned(die, DW_AT_high_pc, &udata, &de) == + DW_DLV_OK) + func->highpc = udata; + + SLIST_INSERT_HEAD(func_info, func, entries); + } + +cont_search: + + /* Search children. */ + ret = dwarf_child(die, &ret_die, &de); + if (ret == DW_DLV_ERROR) + warnx("dwarf_child: %s", dwarf_errmsg(de)); + else if (ret == DW_DLV_OK) + search_line_attr(dbg, func_info, var_info, ret_die, src_files, + filecount); + + /* Search sibling. */ + ret = dwarf_siblingof(dbg, die, &ret_die, &de); + if (ret == DW_DLV_ERROR) + warnx("dwarf_siblingof: %s", dwarf_errmsg(de)); + else if (ret == DW_DLV_OK) + search_line_attr(dbg, func_info, var_info, ret_die, src_files, + filecount); + + dwarf_dealloc(dbg, die, DW_DLA_DIE); +} + +/* + * Read elf file and collect symbol information, sort them, print. + * Return 1 at failed, 0 at success. + */ +static int +read_elf(Elf *elf, const char *filename, Elf_Kind kind) +{ + Dwarf_Debug dbg; + Dwarf_Die die; + Dwarf_Error de; + Dwarf_Half tag; + Elf_Arhdr *arhdr; + Elf_Scn *scn; + GElf_Shdr shdr; + GElf_Half i; + Dwarf_Line *lbuf; + Dwarf_Unsigned lineno; + Dwarf_Signed lcount, filecount; + Dwarf_Addr lineaddr; + struct sym_print_data p_data; + struct sym_head list_head; + struct line_info_head *line_info; + struct func_info_head *func_info; + struct var_info_head *var_info; + struct line_info_entry *lie; + struct func_info_entry *func; + struct var_info_entry *var; + const char *shname, *objname; + char *type_table, **sec_table, *sfile, **src_files; + size_t shstrndx, shnum, dynndx, strndx; + int ret, rtn, e_err; + +#define OBJNAME (objname == NULL ? filename : objname) + + assert(filename != NULL && "filename is null"); + + STAILQ_INIT(&list_head); + type_table = NULL; + sec_table = NULL; + line_info = NULL; + func_info = NULL; + var_info = NULL; + objname = NULL; + dynndx = SHN_UNDEF; + strndx = SHN_UNDEF; + rtn = 0; + + nm_elfclass = gelf_getclass(elf); + + if (kind == ELF_K_AR) { + if ((arhdr = elf_getarhdr(elf)) == NULL) + goto next_cmd; + objname = arhdr->ar_name != NULL ? arhdr->ar_name : + arhdr->ar_rawname; + } + if (!elf_getshnum(elf, &shnum)) { + if ((e_err = elf_errno()) != 0) + warnx("%s: %s", OBJNAME, elf_errmsg(e_err)); + else + warnx("%s: cannot get section number", OBJNAME); + rtn = 1; + goto next_cmd; + } + if (shnum == 0) { + warnx("%s: has no section", OBJNAME); + rtn = 1; + goto next_cmd; + } + if (!elf_getshstrndx(elf, &shstrndx)) { + warnx("%s: cannot get str index", OBJNAME); + rtn = 1; + goto next_cmd; + } + /* type_table for type determine */ + if ((type_table = malloc(sizeof(char) * shnum)) == NULL) { + warn("%s: malloc", OBJNAME); + rtn = 1; + goto next_cmd; + } + /* sec_table for section name to display in sysv format */ + if ((sec_table = calloc(shnum, sizeof(char *))) == NULL) { + warn("%s: calloc", OBJNAME); + rtn = 1; + goto next_cmd; + } + + type_table[0] = 'U'; + if ((sec_table[0] = strdup("*UND*")) == NULL) { + warn("strdup"); + goto next_cmd; + } + + for (i = 1; i < shnum; ++i) { + type_table[i] = 'U'; + if ((scn = elf_getscn(elf, i)) == NULL) { + if ((e_err = elf_errno()) != 0) + warnx("%s: %s", OBJNAME, elf_errmsg(e_err)); + else + warnx("%s: cannot get section", OBJNAME); + rtn = 1; + goto next_cmd; + } + if (gelf_getshdr(scn, &shdr) == NULL) + goto next_cmd; + + /* + * Cannot test by type and attribute for dynstr, strtab + */ + shname = elf_strptr(elf, shstrndx, (size_t) shdr.sh_name); + if (shname != NULL) { + if ((sec_table[i] = strdup(shname)) == NULL) { + warn("strdup"); + goto next_cmd; + } + if (!strncmp(shname, ".dynstr", 7)) { + dynndx = elf_ndxscn(scn); + if (dynndx == SHN_UNDEF) { + warnx("%s: elf_ndxscn failed: %s", + OBJNAME, elf_errmsg(-1)); + goto next_cmd; + } + } + if (!strncmp(shname, ".strtab", 7)) { + strndx = elf_ndxscn(scn); + if (strndx == SHN_UNDEF) { + warnx("%s: elf_ndxscn failed: %s", + OBJNAME, elf_errmsg(-1)); + goto next_cmd; + } + } + } else { + sec_table[i] = strdup("*UND*"); + if (sec_table[i] == NULL) { + warn("strdup"); + goto next_cmd; + } + } + + + if (is_sec_text(&shdr)) + type_table[i] = 'T'; + else if (is_sec_data(&shdr)) { + if (is_sec_readonly(&shdr)) + type_table[i] = 'R'; + else + type_table[i] = 'D'; + } else if (is_sec_nobits(&shdr)) + type_table[i] = 'B'; + else if (is_sec_debug(shname)) + type_table[i] = 'N'; + else if (is_sec_readonly(&shdr) && !is_sec_nobits(&shdr)) + type_table[i] = 'n'; + } + + print_header(filename, objname); + + if ((dynndx == SHN_UNDEF && nm_opts.print_symbol == PRINT_SYM_DYN) || + (strndx == SHN_UNDEF && nm_opts.print_symbol == PRINT_SYM_SYM)) { + warnx("%s: no symbols", OBJNAME); + /* This is not an error case */ + goto next_cmd; + } + + STAILQ_INIT(&list_head); + + if (!nm_opts.debug_line) + goto process_sym; + + /* + * Collect dwarf line number information. + */ + + if (dwarf_elf_init(elf, DW_DLC_READ, NULL, NULL, &dbg, &de) != + DW_DLV_OK) { + warnx("dwarf_elf_init failed: %s", dwarf_errmsg(de)); + goto process_sym; + } + + line_info = malloc(sizeof(struct line_info_head)); + func_info = malloc(sizeof(struct func_info_head)); + var_info = malloc(sizeof(struct var_info_head)); + if (line_info == NULL || func_info == NULL || var_info == NULL) { + warn("malloc"); + (void) dwarf_finish(dbg, &de); + goto process_sym; + } + SLIST_INIT(line_info); + SLIST_INIT(func_info); + SLIST_INIT(var_info); + + while ((ret = dwarf_next_cu_header(dbg, NULL, NULL, NULL, NULL, NULL, + &de)) == DW_DLV_OK) { + die = NULL; + while (dwarf_siblingof(dbg, die, &die, &de) == DW_DLV_OK) { + if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) { + warnx("dwarf_tag failed: %s", + dwarf_errmsg(de)); + continue; + } + /* XXX: What about DW_TAG_partial_unit? */ + if (tag == DW_TAG_compile_unit) + break; + } + if (die == NULL) { + warnx("could not find DW_TAG_compile_unit die"); + continue; + } + + /* Retrieve source file list. */ + ret = dwarf_srcfiles(die, &src_files, &filecount, &de); + if (ret == DW_DLV_ERROR) + warnx("dwarf_srclines: %s", dwarf_errmsg(de)); + if (ret != DW_DLV_OK) + continue; + + /* + * Retrieve line number information from .debug_line section. + */ + + ret = dwarf_srclines(die, &lbuf, &lcount, &de); + if (ret == DW_DLV_ERROR) + warnx("dwarf_srclines: %s", dwarf_errmsg(de)); + if (ret != DW_DLV_OK) + goto line_attr; + for (i = 0; (Dwarf_Signed) i < lcount; i++) { + if (dwarf_lineaddr(lbuf[i], &lineaddr, &de)) { + warnx("dwarf_lineaddr: %s", dwarf_errmsg(de)); + continue; + } + if (dwarf_lineno(lbuf[i], &lineno, &de)) { + warnx("dwarf_lineno: %s", dwarf_errmsg(de)); + continue; + } + if (dwarf_linesrc(lbuf[i], &sfile, &de)) { + warnx("dwarf_linesrc: %s", dwarf_errmsg(de)); + continue; + } + if ((lie = malloc(sizeof(*lie))) == NULL) { + warn("malloc"); + continue; + } + lie->addr = lineaddr; + lie->line = lineno; + lie->file = strdup(sfile); + if (lie->file == NULL) { + warn("strdup"); + free(lie); + continue; + } + SLIST_INSERT_HEAD(line_info, lie, entries); + } + + line_attr: + /* Retrieve line number information from DIEs. */ + search_line_attr(dbg, func_info, var_info, die, src_files, filecount); + } + + (void) dwarf_finish(dbg, &de); + +process_sym: + + p_data.list_num = get_sym(elf, &list_head, shnum, dynndx, strndx, + type_table, (void *) sec_table, shnum); + + if (p_data.list_num == 0) + goto next_cmd; + + p_data.headp = &list_head; + p_data.sh_num = shnum; + p_data.t_table = type_table; + p_data.s_table = (void *) sec_table; + p_data.filename = filename; + p_data.objname = objname; + + sym_list_print(&p_data, func_info, var_info, line_info); + +next_cmd: + if (nm_opts.debug_line) { + if (func_info != NULL) { + while (!SLIST_EMPTY(func_info)) { + func = SLIST_FIRST(func_info); + SLIST_REMOVE_HEAD(func_info, entries); + free(func->file); + free(func->name); + free(func); + } + free(func_info); + func_info = NULL; + } + if (var_info != NULL) { + while (!SLIST_EMPTY(var_info)) { + var = SLIST_FIRST(var_info); + SLIST_REMOVE_HEAD(var_info, entries); + free(var->file); + free(var->name); + free(var); + } + free(var_info); + var_info = NULL; + } + if (line_info != NULL) { + while (!SLIST_EMPTY(line_info)) { + lie = SLIST_FIRST(line_info); + SLIST_REMOVE_HEAD(line_info, entries); + free(lie->file); + free(lie); + } + free(line_info); + line_info = NULL; + } + } + + if (sec_table != NULL) + for (i = 0; i < shnum; ++i) + free(sec_table[i]); + free(sec_table); + free(type_table); + + sym_list_dest(&list_head); + + return (rtn); + +#undef OBJNAME +} + +static int +read_object(const char *filename) +{ + Elf *elf, *arf; + Elf_Cmd elf_cmd; + Elf_Kind kind; + int fd, rtn, e_err; + + assert(filename != NULL && "filename is null"); + + if ((fd = open(filename, O_RDONLY)) == -1) { + warn("'%s'", filename); + return (1); + } + + elf_cmd = ELF_C_READ; + if ((arf = elf_begin(fd, elf_cmd, (Elf *) NULL)) == NULL) { + if ((e_err = elf_errno()) != 0) + warnx("elf_begin error: %s", elf_errmsg(e_err)); + else + warnx("elf_begin error"); + close(fd); + return (1); + } + + assert(arf != NULL && "arf is null."); + + rtn = 0; + if ((kind = elf_kind(arf)) == ELF_K_NONE) { + warnx("%s: File format not recognized", filename); + elf_end(arf); + close(fd); + return (1); + } + if (kind == ELF_K_AR) { + if (nm_opts.print_name == PRINT_NAME_MULTI && + nm_opts.elem_print_fn == sym_elem_print_all) + printf("\n%s:\n", filename); + if (nm_opts.print_armap == true) + print_ar_index(fd, arf); + } + + while ((elf = elf_begin(fd, elf_cmd, arf)) != NULL) { + rtn |= read_elf(elf, filename, kind); + + /* + * If file is not archive, elf_next return ELF_C_NULL and + * stop the loop. + */ + elf_cmd = elf_next(elf); + elf_end(elf); + } + + elf_end(arf); + close(fd); + + return (rtn); +} + +static int +read_files(int argc, char **argv) +{ + int rtn = 0; + + if (argc < 0 || argv == NULL) + return (1); + + if (argc == 0) + rtn |= read_object(nm_info.def_filename); + else { + if (nm_opts.print_name == PRINT_NAME_NONE && argc > 1) + nm_opts.print_name = PRINT_NAME_MULTI; + while (argc > 0) { + rtn |= read_object(*argv); + --argc; + ++argv; + } + } + + return (rtn); +} + +static void +print_lineno(struct sym_entry *ep, struct func_info_head *func_info, + struct var_info_head *var_info, struct line_info_head *line_info) +{ + struct func_info_entry *func; + struct var_info_entry *var; + struct line_info_entry *lie; + + /* For function symbol, search the function line information list. */ + if ((ep->sym->st_info & 0xf) == STT_FUNC && func_info != NULL) { + SLIST_FOREACH(func, func_info, entries) { + if (func->name != NULL && + !strcmp(ep->name, func->name) && + ep->sym->st_value >= func->lowpc && + ep->sym->st_value < func->highpc) { + printf("\t%s:%" PRIu64, func->file, func->line); + return; + } + } + } + + /* For variable symbol, search the variable line information list. */ + if ((ep->sym->st_info & 0xf) == STT_OBJECT && var_info != NULL) { + SLIST_FOREACH(var, var_info, entries) { + if (!strcmp(ep->name, var->name) && + ep->sym->st_value == var->addr) { + printf("\t%s:%" PRIu64, var->file, var->line); + return; + } + } + } + + /* Otherwise search line number information the .debug_line section. */ + if (line_info != NULL) { + SLIST_FOREACH(lie, line_info, entries) { + if (ep->sym->st_value == lie->addr) { + printf("\t%s:%" PRIu64, lie->file, lie->line); + return; + } + } + } +} + +static void +set_opt_value_print_fn(enum radix t) +{ + + switch (t) { + case RADIX_OCT: + nm_opts.value_print_fn = &sym_value_oct_print; + nm_opts.size_print_fn = &sym_size_oct_print; + + break; + case RADIX_DEC: + nm_opts.value_print_fn = &sym_value_dec_print; + nm_opts.size_print_fn = &sym_size_dec_print; + + break; + case RADIX_HEX: + default : + nm_opts.value_print_fn = &sym_value_hex_print; + nm_opts.size_print_fn = &sym_size_hex_print; + } + + assert(nm_opts.value_print_fn != NULL && + "nm_opts.value_print_fn is null"); +} + +static void +sym_elem_print_all(char type, const char *sec, const GElf_Sym *sym, + const char *name) +{ + + if (sec == NULL || sym == NULL || name == NULL || + nm_opts.value_print_fn == NULL) + return; + + if (IS_UNDEF_SYM_TYPE(type)) { + if (nm_opts.t == RADIX_HEX && nm_elfclass == ELFCLASS32) + printf("%-8s", ""); + else + printf("%-16s", ""); + } else { + switch ((nm_opts.sort_fn == & cmp_size ? 2 : 0) + + nm_opts.print_size) { + case 3: + if (sym->st_size != 0) { + nm_opts.value_print_fn(sym); + printf(" "); + nm_opts.size_print_fn(sym); + } + break; + + case 2: + if (sym->st_size != 0) + nm_opts.size_print_fn(sym); + break; + + case 1: + nm_opts.value_print_fn(sym); + if (sym->st_size != 0) { + printf(" "); + nm_opts.size_print_fn(sym); + } + break; + + case 0: + default: + nm_opts.value_print_fn(sym); + } + } + + printf(" %c ", type); + PRINT_DEMANGLED_NAME("%s", name); +} + +static void +sym_elem_print_all_portable(char type, const char *sec, const GElf_Sym *sym, + const char *name) +{ + + if (sec == NULL || sym == NULL || name == NULL || + nm_opts.value_print_fn == NULL) + return; + + PRINT_DEMANGLED_NAME("%s", name); + printf(" %c ", type); + if (!IS_UNDEF_SYM_TYPE(type)) { + nm_opts.value_print_fn(sym); + printf(" "); + if (sym->st_size != 0) + nm_opts.size_print_fn(sym); + } else + printf(" "); +} + +static void +sym_elem_print_all_sysv(char type, const char *sec, const GElf_Sym *sym, + const char *name) +{ + + if (sec == NULL || sym == NULL || name == NULL || + nm_opts.value_print_fn == NULL) + return; + + PRINT_DEMANGLED_NAME("%-20s|", name); + if (IS_UNDEF_SYM_TYPE(type)) + printf(" "); + else + nm_opts.value_print_fn(sym); + + printf("| %c |", type); + + switch (sym->st_info & 0xf) { + case STT_OBJECT: + printf("%18s|", "OBJECT"); + break; + + case STT_FUNC: + printf("%18s|", "FUNC"); + break; + + case STT_SECTION: + printf("%18s|", "SECTION"); + break; + + case STT_FILE: + printf("%18s|", "FILE"); + break; + + case STT_LOPROC: + printf("%18s|", "LOPROC"); + break; + + case STT_HIPROC: + printf("%18s|", "HIPROC"); + break; + + case STT_NOTYPE: + default: + printf("%18s|", "NOTYPE"); + }; + + if (sym->st_size != 0) + nm_opts.size_print_fn(sym); + else + printf(" "); + + printf("| |%s", sec); +} + +static int +sym_elem_def(char type, const GElf_Sym *sym, const char *name) +{ + + assert(IS_SYM_TYPE((unsigned char) type)); + + UNUSED(sym); + UNUSED(name); + + return (!IS_UNDEF_SYM_TYPE((unsigned char) type)); +} + +static int +sym_elem_global(char type, const GElf_Sym *sym, const char *name) +{ + + assert(IS_SYM_TYPE((unsigned char) type)); + + UNUSED(sym); + UNUSED(name); + + /* weak symbols resemble global. */ + return (isupper((unsigned char) type) || type == 'w'); +} + +static int +sym_elem_global_static(char type, const GElf_Sym *sym, const char *name) +{ + unsigned char info; + + assert(sym != NULL); + + UNUSED(type); + UNUSED(name); + + info = sym->st_info >> 4; + + return (info == STB_LOCAL || + info == STB_GLOBAL || + info == STB_WEAK); +} + +static int +sym_elem_nondebug(char type, const GElf_Sym *sym, const char *name) +{ + + assert(sym != NULL); + + UNUSED(type); + UNUSED(name); + + if (sym->st_value == 0 && (sym->st_info & 0xf) == STT_FILE) + return (0); + if (sym->st_name == 0) + return (0); + + return (1); +} + +static int +sym_elem_nonzero_size(char type, const GElf_Sym *sym, const char *name) +{ + + assert(sym != NULL); + + UNUSED(type); + UNUSED(name); + + return (sym->st_size > 0); +} + +static int +sym_elem_undef(char type, const GElf_Sym *sym, const char *name) +{ + + assert(IS_SYM_TYPE((unsigned char) type)); + + UNUSED(sym); + UNUSED(name); + + return (IS_UNDEF_SYM_TYPE((unsigned char) type)); +} + +static void +sym_list_dest(struct sym_head *headp) +{ + struct sym_entry *ep, *ep_n; + + if (headp == NULL) + return; + + ep = STAILQ_FIRST(headp); + while (ep != NULL) { + ep_n = STAILQ_NEXT(ep, sym_entries); + free(ep->sym); + free(ep->name); + free(ep); + ep = ep_n; + } +} + +static int +sym_list_insert(struct sym_head *headp, const char *name, const GElf_Sym *sym) +{ + struct sym_entry *e; + + if (headp == NULL || name == NULL || sym == NULL) + return (0); + if ((e = malloc(sizeof(struct sym_entry))) == NULL) { + warn("malloc"); + return (0); + } + if ((e->name = strdup(name)) == NULL) { + warn("strdup"); + free(e); + return (0); + } + if ((e->sym = malloc(sizeof(GElf_Sym))) == NULL) { + warn("malloc"); + free(e->name); + free(e); + return (0); + } + + memcpy(e->sym, sym, sizeof(GElf_Sym)); + + /* Display size instead of value for common symbol. */ + if (sym->st_shndx == SHN_COMMON) + e->sym->st_value = sym->st_size; + + STAILQ_INSERT_TAIL(headp, e, sym_entries); + + return (1); +} + +/* If file has not .debug_info, line_info will be NULL */ +static void +sym_list_print(struct sym_print_data *p, struct func_info_head *func_info, + struct var_info_head *var_info, struct line_info_head *line_info) +{ + struct sym_entry *e_v; + size_t si; + int i; + + if (p == NULL || CHECK_SYM_PRINT_DATA(p)) + return; + if ((e_v = sym_list_sort(p)) == NULL) + return; + if (nm_opts.sort_reverse == false) + for (si = 0; si != p->list_num; ++si) + sym_list_print_each(&e_v[si], p, func_info, var_info, + line_info); + else + for (i = p->list_num - 1; i != -1; --i) + sym_list_print_each(&e_v[i], p, func_info, var_info, + line_info); + + free(e_v); +} + +/* If file has not .debug_info, line_info will be NULL */ +static void +sym_list_print_each(struct sym_entry *ep, struct sym_print_data *p, + struct func_info_head *func_info, struct var_info_head *var_info, + struct line_info_head *line_info) +{ + const char *sec; + char type; + + if (ep == NULL || CHECK_SYM_PRINT_DATA(p)) + return; + + assert(ep->name != NULL); + assert(ep->sym != NULL); + + type = get_sym_type(ep->sym, p->t_table); + + if (nm_opts.print_name == PRINT_NAME_FULL) { + printf("%s", p->filename); + if (nm_opts.elem_print_fn == &sym_elem_print_all_portable) { + if (p->objname != NULL) + printf("[%s]", p->objname); + printf(": "); + } else { + if (p->objname != NULL) + printf(":%s", p->objname); + printf(":"); + } + } + + switch (ep->sym->st_shndx) { + case SHN_LOPROC: + /* LOPROC or LORESERVE */ + sec = "*LOPROC*"; + break; + case SHN_HIPROC: + sec = "*HIPROC*"; + break; + case SHN_LOOS: + sec = "*LOOS*"; + break; + case SHN_HIOS: + sec = "*HIOS*"; + break; + case SHN_ABS: + sec = "*ABS*"; + break; + case SHN_COMMON: + sec = "*COM*"; + break; + case SHN_HIRESERVE: + /* HIRESERVE or XINDEX */ + sec = "*HIRESERVE*"; + break; + default: + if (ep->sym->st_shndx > p->sh_num) + return; + sec = p->s_table[ep->sym->st_shndx]; + break; + }; + + nm_opts.elem_print_fn(type, sec, ep->sym, ep->name); + + if (nm_opts.debug_line == true && !IS_UNDEF_SYM_TYPE(type)) + print_lineno(ep, func_info, var_info, line_info); + + printf("\n"); +} + +static struct sym_entry * +sym_list_sort(struct sym_print_data *p) +{ + struct sym_entry *ep, *e_v; + int idx; + + if (p == NULL || CHECK_SYM_PRINT_DATA(p)) + return (NULL); + + if ((e_v = malloc(sizeof(struct sym_entry) * p->list_num)) == NULL) { + warn("malloc"); + return (NULL); + } + + idx = 0; + STAILQ_FOREACH(ep, p->headp, sym_entries) { + if (ep->name != NULL && ep->sym != NULL) { + e_v[idx].name = ep->name; + e_v[idx].sym = ep->sym; + ++idx; + } + } + + assert((size_t)idx == p->list_num); + + if (nm_opts.sort_fn != &cmp_none) { + nm_print_data = p; + assert(nm_print_data != NULL); + qsort(e_v, p->list_num, sizeof(struct sym_entry), + nm_opts.sort_fn); + } + + return (e_v); +} + +static void +sym_size_oct_print(const GElf_Sym *sym) +{ + + assert(sym != NULL && "sym is null"); + printf("%016" PRIo64, sym->st_size); +} + +static void +sym_size_hex_print(const GElf_Sym *sym) +{ + + assert(sym != NULL && "sym is null"); + if (nm_elfclass == ELFCLASS32) + printf("%08" PRIx64, sym->st_size); + else + printf("%016" PRIx64, sym->st_size); +} + +static void +sym_size_dec_print(const GElf_Sym *sym) +{ + + assert(sym != NULL && "sym is null"); + printf("%016" PRId64, sym->st_size); +} + +static void +sym_value_oct_print(const GElf_Sym *sym) +{ + + assert(sym != NULL && "sym is null"); + printf("%016" PRIo64, sym->st_value); +} + +static void +sym_value_hex_print(const GElf_Sym *sym) +{ + + assert(sym != NULL && "sym is null"); + if (nm_elfclass == ELFCLASS32) + printf("%08" PRIx64, sym->st_value); + else + printf("%016" PRIx64, sym->st_value); +} + +static void +sym_value_dec_print(const GElf_Sym *sym) +{ + + assert(sym != NULL && "sym is null"); + printf("%016" PRId64, sym->st_value); +} + +static void +usage(int exitcode) +{ + + printf("Usage: %s [options] file ...\ +\n Display symbolic information in file.\n\ +\n Options: \ +\n -A, --print-file-name Write the full pathname or library name of an\ +\n object on each line.\ +\n -a, --debug-syms Display all symbols include debugger-only\ +\n symbols.", nm_info.name); + printf("\ +\n -B Equivalent to specifying \"--format=bsd\".\ +\n -C, --demangle[=style] Decode low-level symbol names.\ +\n --no-demangle Do not demangle low-level symbol names.\ +\n -D, --dynamic Display only dynamic symbols.\ +\n -e Display only global and static symbols."); + printf("\ +\n -f Produce full output (default).\ +\n --format=format Display output in specific format. Allowed\ +\n formats are: \"bsd\", \"posix\" and \"sysv\".\ +\n -g, --extern-only Display only global symbol information.\ +\n -h, --help Show this help message.\ +\n -l, --line-numbers Display filename and linenumber using\ +\n debugging information.\ +\n -n, --numeric-sort Sort symbols numerically by value."); + printf("\ +\n -o Write numeric values in octal. Equivalent to\ +\n specifying \"-t o\".\ +\n -p, --no-sort Do not sort symbols.\ +\n -P Write information in a portable output format.\ +\n Equivalent to specifying \"--format=posix\".\ +\n -r, --reverse-sort Reverse the order of the sort.\ +\n -S, --print-size Print symbol sizes instead values.\ +\n -s, --print-armap Include an index of archive members.\ +\n --size-sort Sort symbols by size."); + printf("\ +\n -t, --radix=format Write each numeric value in the specified\ +\n format:\ +\n d In decimal,\ +\n o In octal,\ +\n x In hexadecimal."); + printf("\ +\n -u, --undefined-only Display only undefined symbols.\ +\n --defined-only Display only defined symbols.\ +\n -V, --version Show the version identifier for %s.\ +\n -v Sort output by value.\ +\n -x Write numeric values in hexadecimal.\ +\n Equivalent to specifying \"-t x\".", + nm_info.name); + printf("\n\ +\n The default options are: output in bsd format, use a hexadecimal radix,\ +\n sort by symbol name, do not demangle names.\n"); + + exit(exitcode); +} + +/* + * Display symbolic information in file. + * Return 0 at success, >0 at failed. + */ +int +main(int argc, char **argv) +{ + int rtn; + + global_init(); + get_opt(argc, argv); + rtn = read_files(argc - optind, argv + optind); + global_dest(); + + exit(rtn); +} diff --git a/contrib/elftoolchain/readelf/Makefile b/contrib/elftoolchain/readelf/Makefile new file mode 100644 index 000000000000..09c8650cda6a --- /dev/null +++ b/contrib/elftoolchain/readelf/Makefile @@ -0,0 +1,15 @@ +# $Id: Makefile 2076 2011-10-27 03:50:33Z jkoshy $ + +TOP= .. + +PROG= readelf +SRCS= readelf.c + +WARNS?= 6 + +DPADD= ${LIBDWARF} ${LIBELF} +LDADD= -ldwarf -lelftc -lelf + +MAN1= readelf.1 + +.include "${TOP}/mk/elftoolchain.prog.mk" diff --git a/contrib/elftoolchain/readelf/readelf.1 b/contrib/elftoolchain/readelf/readelf.1 new file mode 100644 index 000000000000..a71e85f24c8f --- /dev/null +++ b/contrib/elftoolchain/readelf/readelf.1 @@ -0,0 +1,197 @@ +.\" Copyright (c) 2009,2011 Joseph Koshy <jkoshy@users.sourceforge.net> +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer +.\" in this position and unchanged. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR +.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +.\" (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: readelf.1 3195 2015-05-12 17:22:19Z emaste $ +.\" +.Dd September 13, 2012 +.Os +.Dt READELF 1 +.Sh NAME +.Nm readelf +.Nd display information about ELF objects +.Sh SYNOPSIS +.Nm +.Op Fl a | Fl -all +.Op Fl c | Fl -archive-index +.Op Fl d | Fl -dynamic +.Op Fl e | Fl -headers +.Op Fl g | Fl -section-groups +.Op Fl h | Fl -file-header +.Op Fl l | Fl -program-headers +.Op Fl n | Fl -notes +.Op Fl p Ar section | Fl -string-dump Ns = Ns Ar section +.Op Fl r | Fl -relocs +.Op Fl t | Fl -section-details +.Op Fl x Ar section | Fl -hex-dump Ns = Ns Ar section +.Op Fl v | Fl -version +.Oo +.Fl w Ns Oo Ns Ar afilmoprsFLR Ns Oc | +.Fl -debug-dump Ns Op Ns = Ns Ar long-option-name , Ns ... +.Oc +.Op Fl A | Fl -arch-specific +.Op Fl D | Fl -use-dynamic +.Op Fl H | Fl -help +.Op Fl I | Fl -histogram +.Op Fl N | -full-section-name +.Op Fl S | Fl -sections | Fl -section-headers +.Op Fl V | Fl -version-info +.Op Fl W | Fl -wide +.Ar file... +.Sh DESCRIPTION +The +.Nm +utility displays information about ELF objects and +.Xr ar 1 +archives. +.Pp +The +.Nm +utility recognizes the following options: +.Bl -tag -width indent +.It Fl a | Fl -all +Turn on the following flags: +.Fl d , +.Fl h , +.Fl I , +.Fl l , +.Fl r , +.Fl s , +.Fl A , +.Fl S +and +.Fl V . +.It Fl c | Fl -archive-index +Print the archive symbol table for archives. +.It Fl d | Fl -dynamic +Print the contents of the +.Li SHT_DYNAMIC +sections in the ELF object. +.It Fl e | Fl -headers +Print all program, file and section headers in the ELF object. +.It Fl g | Fl -section-groups +This option is recognized, but is ignored. +.It Fl h | Fl -file-header +Print the file header of the ELF object. +.It Fl l | Fl -program-headers +Print the content of the program header table for the object. +.It Fl n | Fl -notes +Print the contents of +.Li PT_NOTE +segments or +.Li SHT_NOTE +sections present in the ELF object. +.It Fl p Ar section | Fl -string-dump Ns = Ns Ar section +Print the contents of the specified section as printable strings. +The argument +.Ar section +should be the name of a section or a numeric section index. +.It Fl r | Fl -relocs +Print relocation information. +.It Fl s | Fl -syms | Fl -symbols +Print symbol tables. +.It Fl t | Fl -section-details +Print additional information about sections, such as the flags +fields in section headers. +.It Fl v | Fl -version +Prints a version identifier for +.Nm +and exits. +.It Fl w Ns Oo afilmoprsFLR Oc | Xo +.Fl -debug-dump Ns Op Ns = Ns Ar long-option-name , Ns ... +.Xc +Display DWARF information. +The +.Fl w +option is used with the short options in the following +table; the +.Fl -debug-dump +option is used with a comma-separated list of the corresponding long +option names: +.Bl -column ".Em Short Option" "aranges|ranges" +.It Em Short Option Ta Em Long Option Ta Em Description +.It a Ta abbrev Ta Show abbreviation information. +.It f Ta frames Ta Show frame information, displaying frame instructions. +.It i Ta info Ta Show debugging information entries. +.It l Ta rawline Ta Show line information in raw form. +.It m Ta macro Ta Show macro information. +.It o Ta loc Ta Show location list information. +.It p Ta pubnames Ta Show global names. +.It r Ta aranges|ranges Ta Show address range information. +.It s Ta str Ta Show the debug string table. +.It F Ta frames-interp Ta Show frame information, displaying register rules. +.It L Ta decodedline Ta Show line information in decoded form. +.It R Ta Ranges Ta Show range lists. +.El +.Pp +If no sub-options are specified, the default is to show information +corresponding to the +.Ar a , f , i, l , o , p , r , s +and +.Ar R +short options. +.It Fl x Ar section | Fl -hex-dump Ns = Ns Ar section +Display the contents of the specified section in hexadecimal. +The argument +.Ar section +should be the name of a section or a numeric section index. +.It Fl A | Fl -arch-specific +This option is accepted but is currently unimplemented. +.It Fl D | Fl -use-dynamic +Print the symbol table specified by the +.Li DT_SYMTAB +entry in the +.Dq Li .dynamic +section. +.It Fl H | Fl -help +Print a help message. +.It Fl I | Fl -histogram +Print information on bucket list lengths for sections of type +.Li SHT_HASH +and +.Li SHT_GNU_HASH . +.It Fl N | Fl -full-section-name +This option is accepted but is currently unimplemented. +.It Fl S | Fl -sections | Fl -section-headers +Print information in the section headers for each ELF object. +.It Fl V | Fl -version-info +Print symbol versioning information. +.It Fl W | Fl -wide +Print information about ELF structures using one long line per +structure. +If this option is not specified, +.Nm +will list information in the headers of 64 bit ELF objects on two +separate lines. +.El +.Sh EXIT STATUS +.Ex -std +.Sh SEE ALSO +.Xr nm 1 , +.Xr addr2line 1 , +.Xr elfcopy 1 , +.Sh AUTHORS +The +.Nm +utility was written by +.An Kai Wang Aq Mt kaiwang27@users.sourceforge.net . diff --git a/contrib/elftoolchain/readelf/readelf.c b/contrib/elftoolchain/readelf/readelf.c new file mode 100644 index 000000000000..29bc38955040 --- /dev/null +++ b/contrib/elftoolchain/readelf/readelf.c @@ -0,0 +1,7480 @@ +/*- + * Copyright (c) 2009-2014 Kai Wang + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <sys/param.h> +#include <sys/queue.h> +#include <ar.h> +#include <ctype.h> +#include <dwarf.h> +#include <err.h> +#include <fcntl.h> +#include <gelf.h> +#include <getopt.h> +#include <libdwarf.h> +#include <libelftc.h> +#include <libgen.h> +#include <stdarg.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> +#include <unistd.h> + +#include "_elftc.h" + +ELFTC_VCSID("$Id: readelf.c 3189 2015-04-20 17:02:01Z emaste $"); + +/* + * readelf(1) options. + */ +#define RE_AA 0x00000001 +#define RE_C 0x00000002 +#define RE_DD 0x00000004 +#define RE_D 0x00000008 +#define RE_G 0x00000010 +#define RE_H 0x00000020 +#define RE_II 0x00000040 +#define RE_I 0x00000080 +#define RE_L 0x00000100 +#define RE_NN 0x00000200 +#define RE_N 0x00000400 +#define RE_P 0x00000800 +#define RE_R 0x00001000 +#define RE_SS 0x00002000 +#define RE_S 0x00004000 +#define RE_T 0x00008000 +#define RE_U 0x00010000 +#define RE_VV 0x00020000 +#define RE_WW 0x00040000 +#define RE_W 0x00080000 +#define RE_X 0x00100000 + +/* + * dwarf dump options. + */ +#define DW_A 0x00000001 +#define DW_FF 0x00000002 +#define DW_F 0x00000004 +#define DW_I 0x00000008 +#define DW_LL 0x00000010 +#define DW_L 0x00000020 +#define DW_M 0x00000040 +#define DW_O 0x00000080 +#define DW_P 0x00000100 +#define DW_RR 0x00000200 +#define DW_R 0x00000400 +#define DW_S 0x00000800 + +#define DW_DEFAULT_OPTIONS (DW_A | DW_F | DW_I | DW_L | DW_O | DW_P | \ + DW_R | DW_RR | DW_S) + +/* + * readelf(1) run control flags. + */ +#define DISPLAY_FILENAME 0x0001 + +/* + * Internal data structure for sections. + */ +struct section { + const char *name; /* section name */ + Elf_Scn *scn; /* section scn */ + uint64_t off; /* section offset */ + uint64_t sz; /* section size */ + uint64_t entsize; /* section entsize */ + uint64_t align; /* section alignment */ + uint64_t type; /* section type */ + uint64_t flags; /* section flags */ + uint64_t addr; /* section virtual addr */ + uint32_t link; /* section link ndx */ + uint32_t info; /* section info ndx */ +}; + +struct dumpop { + union { + size_t si; /* section index */ + const char *sn; /* section name */ + } u; + enum { + DUMP_BY_INDEX = 0, + DUMP_BY_NAME + } type; /* dump type */ +#define HEX_DUMP 0x0001 +#define STR_DUMP 0x0002 + int op; /* dump operation */ + STAILQ_ENTRY(dumpop) dumpop_list; +}; + +struct symver { + const char *name; + int type; +}; + +/* + * Structure encapsulates the global data for readelf(1). + */ +struct readelf { + const char *filename; /* current processing file. */ + int options; /* command line options. */ + int flags; /* run control flags. */ + int dop; /* dwarf dump options. */ + Elf *elf; /* underlying ELF descriptor. */ + Elf *ar; /* archive ELF descriptor. */ + Dwarf_Debug dbg; /* DWARF handle. */ + Dwarf_Half cu_psize; /* DWARF CU pointer size. */ + Dwarf_Half cu_osize; /* DWARF CU offset size. */ + Dwarf_Half cu_ver; /* DWARF CU version. */ + GElf_Ehdr ehdr; /* ELF header. */ + int ec; /* ELF class. */ + size_t shnum; /* #sections. */ + struct section *vd_s; /* Verdef section. */ + struct section *vn_s; /* Verneed section. */ + struct section *vs_s; /* Versym section. */ + uint16_t *vs; /* Versym array. */ + int vs_sz; /* Versym array size. */ + struct symver *ver; /* Version array. */ + int ver_sz; /* Size of version array. */ + struct section *sl; /* list of sections. */ + STAILQ_HEAD(, dumpop) v_dumpop; /* list of dump ops. */ + uint64_t (*dw_read)(Elf_Data *, uint64_t *, int); + uint64_t (*dw_decode)(uint8_t **, int); +}; + +enum options +{ + OPTION_DEBUG_DUMP +}; + +static struct option longopts[] = { + {"all", no_argument, NULL, 'a'}, + {"arch-specific", no_argument, NULL, 'A'}, + {"archive-index", no_argument, NULL, 'c'}, + {"debug-dump", optional_argument, NULL, OPTION_DEBUG_DUMP}, + {"dynamic", no_argument, NULL, 'd'}, + {"file-header", no_argument, NULL, 'h'}, + {"full-section-name", no_argument, NULL, 'N'}, + {"headers", no_argument, NULL, 'e'}, + {"help", no_argument, 0, 'H'}, + {"hex-dump", required_argument, NULL, 'x'}, + {"histogram", no_argument, NULL, 'I'}, + {"notes", no_argument, NULL, 'n'}, + {"program-headers", no_argument, NULL, 'l'}, + {"relocs", no_argument, NULL, 'r'}, + {"sections", no_argument, NULL, 'S'}, + {"section-headers", no_argument, NULL, 'S'}, + {"section-groups", no_argument, NULL, 'g'}, + {"section-details", no_argument, NULL, 't'}, + {"segments", no_argument, NULL, 'l'}, + {"string-dump", required_argument, NULL, 'p'}, + {"symbols", no_argument, NULL, 's'}, + {"syms", no_argument, NULL, 's'}, + {"unwind", no_argument, NULL, 'u'}, + {"use-dynamic", no_argument, NULL, 'D'}, + {"version-info", no_argument, 0, 'V'}, + {"version", no_argument, 0, 'v'}, + {"wide", no_argument, 0, 'W'}, + {NULL, 0, NULL, 0} +}; + +struct eflags_desc { + uint64_t flag; + const char *desc; +}; + +struct mips_option { + uint64_t flag; + const char *desc; +}; + +static void add_dumpop(struct readelf *re, size_t si, const char *sn, int op, + int t); +static const char *aeabi_adv_simd_arch(uint64_t simd); +static const char *aeabi_align_needed(uint64_t an); +static const char *aeabi_align_preserved(uint64_t ap); +static const char *aeabi_arm_isa(uint64_t ai); +static const char *aeabi_cpu_arch(uint64_t arch); +static const char *aeabi_cpu_arch_profile(uint64_t pf); +static const char *aeabi_div(uint64_t du); +static const char *aeabi_enum_size(uint64_t es); +static const char *aeabi_fp_16bit_format(uint64_t fp16); +static const char *aeabi_fp_arch(uint64_t fp); +static const char *aeabi_fp_denormal(uint64_t fd); +static const char *aeabi_fp_exceptions(uint64_t fe); +static const char *aeabi_fp_hpext(uint64_t fh); +static const char *aeabi_fp_number_model(uint64_t fn); +static const char *aeabi_fp_optm_goal(uint64_t fog); +static const char *aeabi_fp_rounding(uint64_t fr); +static const char *aeabi_hardfp(uint64_t hfp); +static const char *aeabi_mpext(uint64_t mp); +static const char *aeabi_optm_goal(uint64_t og); +static const char *aeabi_pcs_config(uint64_t pcs); +static const char *aeabi_pcs_got(uint64_t got); +static const char *aeabi_pcs_r9(uint64_t r9); +static const char *aeabi_pcs_ro(uint64_t ro); +static const char *aeabi_pcs_rw(uint64_t rw); +static const char *aeabi_pcs_wchar_t(uint64_t wt); +static const char *aeabi_t2ee(uint64_t t2ee); +static const char *aeabi_thumb_isa(uint64_t ti); +static const char *aeabi_fp_user_exceptions(uint64_t fu); +static const char *aeabi_unaligned_access(uint64_t ua); +static const char *aeabi_vfp_args(uint64_t va); +static const char *aeabi_virtual(uint64_t vt); +static const char *aeabi_wmmx_arch(uint64_t wmmx); +static const char *aeabi_wmmx_args(uint64_t wa); +static const char *elf_class(unsigned int class); +static const char *elf_endian(unsigned int endian); +static const char *elf_machine(unsigned int mach); +static const char *elf_osabi(unsigned int abi); +static const char *elf_type(unsigned int type); +static const char *elf_ver(unsigned int ver); +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 void dump_dwarf(struct readelf *re); +static void dump_dwarf_abbrev(struct readelf *re); +static void dump_dwarf_aranges(struct readelf *re); +static void dump_dwarf_block(struct readelf *re, uint8_t *b, + Dwarf_Unsigned len); +static void dump_dwarf_die(struct readelf *re, Dwarf_Die die, int level); +static void dump_dwarf_frame(struct readelf *re, int alt); +static void dump_dwarf_frame_inst(struct readelf *re, Dwarf_Cie cie, + uint8_t *insts, Dwarf_Unsigned len, Dwarf_Unsigned caf, Dwarf_Signed daf, + Dwarf_Addr pc, Dwarf_Debug dbg); +static int dump_dwarf_frame_regtable(struct readelf *re, Dwarf_Fde fde, + Dwarf_Addr pc, Dwarf_Unsigned func_len, Dwarf_Half cie_ra); +static void dump_dwarf_frame_section(struct readelf *re, struct section *s, + int alt); +static void dump_dwarf_info(struct readelf *re, Dwarf_Bool is_info); +static void dump_dwarf_macinfo(struct readelf *re); +static void dump_dwarf_line(struct readelf *re); +static void dump_dwarf_line_decoded(struct readelf *re); +static void dump_dwarf_loc(struct readelf *re, Dwarf_Loc *lr); +static void dump_dwarf_loclist(struct readelf *re); +static void dump_dwarf_pubnames(struct readelf *re); +static void dump_dwarf_ranges(struct readelf *re); +static void dump_dwarf_ranges_foreach(struct readelf *re, Dwarf_Die die, + Dwarf_Addr base); +static void dump_dwarf_str(struct readelf *re); +static void dump_eflags(struct readelf *re, uint64_t e_flags); +static void dump_elf(struct readelf *re); +static void dump_dyn_val(struct readelf *re, GElf_Dyn *dyn, uint32_t stab); +static void dump_dynamic(struct readelf *re); +static void dump_liblist(struct readelf *re); +static void dump_mips_attributes(struct readelf *re, uint8_t *p, uint8_t *pe); +static void dump_mips_odk_reginfo(struct readelf *re, uint8_t *p, size_t sz); +static void dump_mips_options(struct readelf *re, struct section *s); +static void dump_mips_option_flags(const char *name, struct mips_option *opt, + uint64_t info); +static void dump_mips_reginfo(struct readelf *re, struct section *s); +static void dump_mips_specific_info(struct readelf *re); +static void dump_notes(struct readelf *re); +static void dump_notes_content(struct readelf *re, const char *buf, size_t sz, + off_t off); +static void dump_svr4_hash(struct section *s); +static void dump_svr4_hash64(struct readelf *re, struct section *s); +static void dump_gnu_hash(struct readelf *re, struct section *s); +static void dump_hash(struct readelf *re); +static void dump_phdr(struct readelf *re); +static void dump_ppc_attributes(uint8_t *p, uint8_t *pe); +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 void dump_ver(struct readelf *re); +static void dump_verdef(struct readelf *re, int dump); +static void dump_verneed(struct readelf *re, int dump); +static void dump_versym(struct readelf *re); +static const char *dwarf_reg(unsigned int mach, unsigned int reg); +static const char *dwarf_regname(struct readelf *re, unsigned int num); +static struct dumpop *find_dumpop(struct readelf *re, size_t si, + const char *sn, int op, int t); +static char *get_regoff_str(struct readelf *re, Dwarf_Half reg, + Dwarf_Addr off); +static const char *get_string(struct readelf *re, int strtab, size_t off); +static const char *get_symbol_name(struct readelf *re, int symtab, int i); +static uint64_t get_symbol_value(struct readelf *re, int symtab, int i); +static void load_sections(struct readelf *re); +static const char *mips_abi_fp(uint64_t fp); +static const char *note_type(const char *note_name, unsigned int et, + unsigned int nt); +static const char *note_type_freebsd(unsigned int nt); +static const char *note_type_freebsd_core(unsigned int nt); +static const char *note_type_linux_core(unsigned int nt); +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 *option_kind(uint8_t kind); +static const char *phdr_type(unsigned int ptype); +static const char *ppc_abi_fp(uint64_t fp); +static const char *ppc_abi_vector(uint64_t vec); +static const char *r_type(unsigned int mach, unsigned int type); +static void readelf_usage(void); +static void readelf_version(void); +static void search_loclist_at(struct readelf *re, Dwarf_Die die, + Dwarf_Unsigned lowpc); +static void search_ver(struct readelf *re); +static const char *section_type(unsigned int mach, unsigned int stype); +static void set_cu_context(struct readelf *re, Dwarf_Half psize, + Dwarf_Half osize, Dwarf_Half ver); +static const char *st_bind(unsigned int sbind); +static const char *st_shndx(unsigned int shndx); +static const char *st_type(unsigned int stype); +static const char *st_vis(unsigned int svis); +static const char *top_tag(unsigned int tag); +static void unload_sections(struct readelf *re); +static uint64_t _read_lsb(Elf_Data *d, uint64_t *offsetp, + int bytes_to_read); +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 struct eflags_desc arm_eflags_desc[] = { + {EF_ARM_RELEXEC, "relocatable executable"}, + {EF_ARM_HASENTRY, "has entry point"}, + {EF_ARM_SYMSARESORTED, "sorted symbol tables"}, + {EF_ARM_DYNSYMSUSESEGIDX, "dynamic symbols use segment index"}, + {EF_ARM_MAPSYMSFIRST, "mapping symbols precede others"}, + {EF_ARM_BE8, "BE8"}, + {EF_ARM_LE8, "LE8"}, + {EF_ARM_INTERWORK, "interworking enabled"}, + {EF_ARM_APCS_26, "uses APCS/26"}, + {EF_ARM_APCS_FLOAT, "uses APCS/float"}, + {EF_ARM_PIC, "position independent"}, + {EF_ARM_ALIGN8, "8 bit structure alignment"}, + {EF_ARM_NEW_ABI, "uses new ABI"}, + {EF_ARM_OLD_ABI, "uses old ABI"}, + {EF_ARM_SOFT_FLOAT, "software FP"}, + {EF_ARM_VFP_FLOAT, "VFP"}, + {EF_ARM_MAVERICK_FLOAT, "Maverick FP"}, + {0, NULL} +}; + +static struct eflags_desc mips_eflags_desc[] = { + {EF_MIPS_NOREORDER, "noreorder"}, + {EF_MIPS_PIC, "pic"}, + {EF_MIPS_CPIC, "cpic"}, + {EF_MIPS_UCODE, "ugen_reserved"}, + {EF_MIPS_ABI2, "abi2"}, + {EF_MIPS_OPTIONS_FIRST, "odk first"}, + {EF_MIPS_ARCH_ASE_MDMX, "mdmx"}, + {EF_MIPS_ARCH_ASE_M16, "mips16"}, + {0, NULL} +}; + +static struct eflags_desc powerpc_eflags_desc[] = { + {EF_PPC_EMB, "emb"}, + {EF_PPC_RELOCATABLE, "relocatable"}, + {EF_PPC_RELOCATABLE_LIB, "relocatable-lib"}, + {0, NULL} +}; + +static struct eflags_desc sparc_eflags_desc[] = { + {EF_SPARC_32PLUS, "v8+"}, + {EF_SPARC_SUN_US1, "ultrasparcI"}, + {EF_SPARC_HAL_R1, "halr1"}, + {EF_SPARC_SUN_US3, "ultrasparcIII"}, + {0, NULL} +}; + +static const char * +elf_osabi(unsigned int abi) +{ + static char s_abi[32]; + + switch(abi) { + case ELFOSABI_SYSV: return "SYSV"; + case ELFOSABI_HPUX: return "HPUS"; + case ELFOSABI_NETBSD: return "NetBSD"; + case ELFOSABI_GNU: return "GNU"; + case ELFOSABI_HURD: return "HURD"; + case ELFOSABI_86OPEN: return "86OPEN"; + case ELFOSABI_SOLARIS: return "Solaris"; + case ELFOSABI_AIX: return "AIX"; + case ELFOSABI_IRIX: return "IRIX"; + case ELFOSABI_FREEBSD: return "FreeBSD"; + case ELFOSABI_TRU64: return "TRU64"; + case ELFOSABI_MODESTO: return "MODESTO"; + case ELFOSABI_OPENBSD: return "OpenBSD"; + case ELFOSABI_OPENVMS: return "OpenVMS"; + case ELFOSABI_NSK: return "NSK"; + case ELFOSABI_ARM: return "ARM"; + case ELFOSABI_STANDALONE: return "StandAlone"; + default: + snprintf(s_abi, sizeof(s_abi), "<unknown: %#x>", abi); + return (s_abi); + } +}; + +static const char * +elf_machine(unsigned int mach) +{ + static char s_mach[32]; + + switch (mach) { + case EM_NONE: return "Unknown machine"; + case EM_M32: return "AT&T WE32100"; + case EM_SPARC: return "Sun SPARC"; + case EM_386: return "Intel i386"; + case EM_68K: return "Motorola 68000"; + case EM_88K: return "Motorola 88000"; + case EM_860: return "Intel i860"; + case EM_MIPS: return "MIPS R3000 Big-Endian only"; + case EM_S370: return "IBM System/370"; + case EM_MIPS_RS3_LE: return "MIPS R3000 Little-Endian"; + case EM_PARISC: return "HP PA-RISC"; + case EM_VPP500: return "Fujitsu VPP500"; + case EM_SPARC32PLUS: return "SPARC v8plus"; + case EM_960: return "Intel 80960"; + case EM_PPC: return "PowerPC 32-bit"; + case EM_PPC64: return "PowerPC 64-bit"; + case EM_S390: return "IBM System/390"; + case EM_V800: return "NEC V800"; + case EM_FR20: return "Fujitsu FR20"; + case EM_RH32: return "TRW RH-32"; + case EM_RCE: return "Motorola RCE"; + case EM_ARM: return "ARM"; + case EM_SH: return "Hitachi SH"; + case EM_SPARCV9: return "SPARC v9 64-bit"; + case EM_TRICORE: return "Siemens TriCore embedded processor"; + case EM_ARC: return "Argonaut RISC Core"; + case EM_H8_300: return "Hitachi H8/300"; + case EM_H8_300H: return "Hitachi H8/300H"; + case EM_H8S: return "Hitachi H8S"; + case EM_H8_500: return "Hitachi H8/500"; + case EM_IA_64: return "Intel IA-64 Processor"; + case EM_MIPS_X: return "Stanford MIPS-X"; + case EM_COLDFIRE: return "Motorola ColdFire"; + case EM_68HC12: return "Motorola M68HC12"; + case EM_MMA: return "Fujitsu MMA"; + case EM_PCP: return "Siemens PCP"; + case EM_NCPU: return "Sony nCPU"; + case EM_NDR1: return "Denso NDR1 microprocessor"; + case EM_STARCORE: return "Motorola Star*Core processor"; + case EM_ME16: return "Toyota ME16 processor"; + case EM_ST100: return "STMicroelectronics ST100 processor"; + case EM_TINYJ: return "Advanced Logic Corp. TinyJ processor"; + case EM_X86_64: return "Advanced Micro Devices x86-64"; + case EM_PDSP: return "Sony DSP Processor"; + case EM_FX66: return "Siemens FX66 microcontroller"; + case EM_ST9PLUS: return "STMicroelectronics ST9+ 8/16 microcontroller"; + case EM_ST7: return "STmicroelectronics ST7 8-bit microcontroller"; + case EM_68HC16: return "Motorola MC68HC16 microcontroller"; + case EM_68HC11: return "Motorola MC68HC11 microcontroller"; + case EM_68HC08: return "Motorola MC68HC08 microcontroller"; + case EM_68HC05: return "Motorola MC68HC05 microcontroller"; + case EM_SVX: return "Silicon Graphics SVx"; + case EM_ST19: return "STMicroelectronics ST19 8-bit mc"; + case EM_VAX: return "Digital VAX"; + case EM_CRIS: return "Axis Communications 32-bit embedded processor"; + case EM_JAVELIN: return "Infineon Tech. 32bit embedded processor"; + case EM_FIREPATH: return "Element 14 64-bit DSP Processor"; + case EM_ZSP: return "LSI Logic 16-bit DSP Processor"; + case EM_MMIX: return "Donald Knuth's educational 64-bit proc"; + case EM_HUANY: return "Harvard University MI object files"; + case EM_PRISM: return "SiTera Prism"; + case EM_AVR: return "Atmel AVR 8-bit microcontroller"; + case EM_FR30: return "Fujitsu FR30"; + case EM_D10V: return "Mitsubishi D10V"; + case EM_D30V: return "Mitsubishi D30V"; + case EM_V850: return "NEC v850"; + case EM_M32R: return "Mitsubishi M32R"; + case EM_MN10300: return "Matsushita MN10300"; + case EM_MN10200: return "Matsushita MN10200"; + case EM_PJ: return "picoJava"; + case EM_OPENRISC: return "OpenRISC 32-bit embedded processor"; + case EM_ARC_A5: return "ARC Cores Tangent-A5"; + case EM_XTENSA: return "Tensilica Xtensa Architecture"; + case EM_VIDEOCORE: return "Alphamosaic VideoCore processor"; + case EM_TMM_GPP: return "Thompson Multimedia General Purpose Processor"; + case EM_NS32K: return "National Semiconductor 32000 series"; + case EM_TPC: return "Tenor Network TPC processor"; + case EM_SNP1K: return "Trebia SNP 1000 processor"; + case EM_ST200: return "STMicroelectronics ST200 microcontroller"; + case EM_IP2K: return "Ubicom IP2xxx microcontroller family"; + case EM_MAX: return "MAX Processor"; + case EM_CR: return "National Semiconductor CompactRISC microprocessor"; + case EM_F2MC16: return "Fujitsu F2MC16"; + case EM_MSP430: return "TI embedded microcontroller msp430"; + case EM_BLACKFIN: return "Analog Devices Blackfin (DSP) processor"; + case EM_SE_C33: return "S1C33 Family of Seiko Epson processors"; + case EM_SEP: return "Sharp embedded microprocessor"; + case EM_ARCA: return "Arca RISC Microprocessor"; + case EM_UNICORE: return "Microprocessor series from PKU-Unity Ltd"; + case EM_AARCH64: return "AArch64"; + default: + snprintf(s_mach, sizeof(s_mach), "<unknown: %#x>", mach); + return (s_mach); + } + +} + +static const char * +elf_class(unsigned int class) +{ + static char s_class[32]; + + switch (class) { + case ELFCLASSNONE: return "none"; + case ELFCLASS32: return "ELF32"; + case ELFCLASS64: return "ELF64"; + default: + snprintf(s_class, sizeof(s_class), "<unknown: %#x>", class); + return (s_class); + } +} + +static const char * +elf_endian(unsigned int endian) +{ + static char s_endian[32]; + + switch (endian) { + case ELFDATANONE: return "none"; + case ELFDATA2LSB: return "2's complement, little endian"; + case ELFDATA2MSB: return "2's complement, big endian"; + default: + snprintf(s_endian, sizeof(s_endian), "<unknown: %#x>", endian); + return (s_endian); + } +} + +static const char * +elf_type(unsigned int type) +{ + static char s_type[32]; + + switch (type) { + case ET_NONE: return "NONE (None)"; + case ET_REL: return "REL (Relocatable file)"; + case ET_EXEC: return "EXEC (Executable file)"; + case ET_DYN: return "DYN (Shared object file)"; + case ET_CORE: return "CORE (Core file)"; + default: + if (type >= ET_LOPROC) + snprintf(s_type, sizeof(s_type), "<proc: %#x>", type); + else if (type >= ET_LOOS && type <= ET_HIOS) + snprintf(s_type, sizeof(s_type), "<os: %#x>", type); + else + snprintf(s_type, sizeof(s_type), "<unknown: %#x>", + type); + return (s_type); + } +} + +static const char * +elf_ver(unsigned int ver) +{ + static char s_ver[32]; + + switch (ver) { + case EV_CURRENT: return "(current)"; + case EV_NONE: return "(none)"; + default: + snprintf(s_ver, sizeof(s_ver), "<unknown: %#x>", + ver); + return (s_ver); + } +} + +static const char * +phdr_type(unsigned int ptype) +{ + static char s_ptype[32]; + + switch (ptype) { + case PT_NULL: return "NULL"; + case PT_LOAD: return "LOAD"; + case PT_DYNAMIC: return "DYNAMIC"; + case PT_INTERP: return "INTERP"; + case PT_NOTE: return "NOTE"; + case PT_SHLIB: return "SHLIB"; + case PT_PHDR: return "PHDR"; + case PT_TLS: return "TLS"; + case PT_GNU_EH_FRAME: return "GNU_EH_FRAME"; + case PT_GNU_STACK: return "GNU_STACK"; + case PT_GNU_RELRO: return "GNU_RELRO"; + default: + if (ptype >= PT_LOPROC && ptype <= PT_HIPROC) + snprintf(s_ptype, sizeof(s_ptype), "LOPROC+%#x", + ptype - PT_LOPROC); + else if (ptype >= PT_LOOS && ptype <= PT_HIOS) + snprintf(s_ptype, sizeof(s_ptype), "LOOS+%#x", + ptype - PT_LOOS); + else + snprintf(s_ptype, sizeof(s_ptype), "<unknown: %#x>", + ptype); + return (s_ptype); + } +} + +static const char * +section_type(unsigned int mach, unsigned int stype) +{ + static char s_stype[32]; + + if (stype >= SHT_LOPROC && stype <= SHT_HIPROC) { + switch (mach) { + case EM_X86_64: + switch (stype) { + case SHT_AMD64_UNWIND: return "AMD64_UNWIND"; + default: + break; + } + break; + case EM_MIPS: + case EM_MIPS_RS3_LE: + switch (stype) { + case SHT_MIPS_LIBLIST: return "MIPS_LIBLIST"; + case SHT_MIPS_MSYM: return "MIPS_MSYM"; + case SHT_MIPS_CONFLICT: return "MIPS_CONFLICT"; + case SHT_MIPS_GPTAB: return "MIPS_GPTAB"; + case SHT_MIPS_UCODE: return "MIPS_UCODE"; + case SHT_MIPS_DEBUG: return "MIPS_DEBUG"; + case SHT_MIPS_REGINFO: return "MIPS_REGINFO"; + case SHT_MIPS_PACKAGE: return "MIPS_PACKAGE"; + case SHT_MIPS_PACKSYM: return "MIPS_PACKSYM"; + case SHT_MIPS_RELD: return "MIPS_RELD"; + case SHT_MIPS_IFACE: return "MIPS_IFACE"; + case SHT_MIPS_CONTENT: return "MIPS_CONTENT"; + case SHT_MIPS_OPTIONS: return "MIPS_OPTIONS"; + case SHT_MIPS_DELTASYM: return "MIPS_DELTASYM"; + case SHT_MIPS_DELTAINST: return "MIPS_DELTAINST"; + case SHT_MIPS_DELTACLASS: return "MIPS_DELTACLASS"; + case SHT_MIPS_DWARF: return "MIPS_DWARF"; + case SHT_MIPS_DELTADECL: return "MIPS_DELTADECL"; + case SHT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB"; + case SHT_MIPS_EVENTS: return "MIPS_EVENTS"; + case SHT_MIPS_TRANSLATE: return "MIPS_TRANSLATE"; + case SHT_MIPS_PIXIE: return "MIPS_PIXIE"; + case SHT_MIPS_XLATE: return "MIPS_XLATE"; + case SHT_MIPS_XLATE_DEBUG: return "MIPS_XLATE_DEBUG"; + case SHT_MIPS_WHIRL: return "MIPS_WHIRL"; + case SHT_MIPS_EH_REGION: return "MIPS_EH_REGION"; + case SHT_MIPS_XLATE_OLD: return "MIPS_XLATE_OLD"; + case SHT_MIPS_PDR_EXCEPTION: return "MIPS_PDR_EXCEPTION"; + default: + break; + } + break; + default: + break; + } + + snprintf(s_stype, sizeof(s_stype), "LOPROC+%#x", + stype - SHT_LOPROC); + return (s_stype); + } + + switch (stype) { + case SHT_NULL: return "NULL"; + case SHT_PROGBITS: return "PROGBITS"; + case SHT_SYMTAB: return "SYMTAB"; + case SHT_STRTAB: return "STRTAB"; + case SHT_RELA: return "RELA"; + case SHT_HASH: return "HASH"; + case SHT_DYNAMIC: return "DYNAMIC"; + case SHT_NOTE: return "NOTE"; + case SHT_NOBITS: return "NOBITS"; + case SHT_REL: return "REL"; + case SHT_SHLIB: return "SHLIB"; + case SHT_DYNSYM: return "DYNSYM"; + case SHT_INIT_ARRAY: return "INIT_ARRAY"; + case SHT_FINI_ARRAY: return "FINI_ARRAY"; + case SHT_PREINIT_ARRAY: return "PREINIT_ARRAY"; + case SHT_GROUP: return "GROUP"; + case SHT_SYMTAB_SHNDX: return "SYMTAB_SHNDX"; + case SHT_SUNW_dof: return "SUNW_dof"; + case SHT_SUNW_cap: return "SUNW_cap"; + case SHT_GNU_HASH: return "GNU_HASH"; + case SHT_SUNW_ANNOTATE: return "SUNW_ANNOTATE"; + case SHT_SUNW_DEBUGSTR: return "SUNW_DEBUGSTR"; + case SHT_SUNW_DEBUG: return "SUNW_DEBUG"; + case SHT_SUNW_move: return "SUNW_move"; + case SHT_SUNW_COMDAT: return "SUNW_COMDAT"; + case SHT_SUNW_syminfo: return "SUNW_syminfo"; + case SHT_SUNW_verdef: return "SUNW_verdef"; + case SHT_SUNW_verneed: return "SUNW_verneed"; + case SHT_SUNW_versym: return "SUNW_versym"; + default: + if (stype >= SHT_LOOS && stype <= SHT_HIOS) + snprintf(s_stype, sizeof(s_stype), "LOOS+%#x", + stype - SHT_LOOS); + else if (stype >= SHT_LOUSER) + snprintf(s_stype, sizeof(s_stype), "LOUSER+%#x", + stype - SHT_LOUSER); + else + snprintf(s_stype, sizeof(s_stype), "<unknown: %#x>", + stype); + return (s_stype); + } +} + +static const char * +dt_type(unsigned int mach, unsigned int dtype) +{ + static char s_dtype[32]; + + if (dtype >= DT_LOPROC && dtype <= DT_HIPROC) { + switch (mach) { + case EM_ARM: + switch (dtype) { + case DT_ARM_SYMTABSZ: + return "ARM_SYMTABSZ"; + default: + break; + } + break; + case EM_MIPS: + case EM_MIPS_RS3_LE: + switch (dtype) { + case DT_MIPS_RLD_VERSION: + return "MIPS_RLD_VERSION"; + case DT_MIPS_TIME_STAMP: + return "MIPS_TIME_STAMP"; + case DT_MIPS_ICHECKSUM: + return "MIPS_ICHECKSUM"; + case DT_MIPS_IVERSION: + return "MIPS_IVERSION"; + case DT_MIPS_FLAGS: + return "MIPS_FLAGS"; + case DT_MIPS_BASE_ADDRESS: + return "MIPS_BASE_ADDRESS"; + case DT_MIPS_CONFLICT: + return "MIPS_CONFLICT"; + case DT_MIPS_LIBLIST: + return "MIPS_LIBLIST"; + case DT_MIPS_LOCAL_GOTNO: + return "MIPS_LOCAL_GOTNO"; + case DT_MIPS_CONFLICTNO: + return "MIPS_CONFLICTNO"; + case DT_MIPS_LIBLISTNO: + return "MIPS_LIBLISTNO"; + case DT_MIPS_SYMTABNO: + return "MIPS_SYMTABNO"; + case DT_MIPS_UNREFEXTNO: + return "MIPS_UNREFEXTNO"; + case DT_MIPS_GOTSYM: + return "MIPS_GOTSYM"; + case DT_MIPS_HIPAGENO: + return "MIPS_HIPAGENO"; + case DT_MIPS_RLD_MAP: + return "MIPS_RLD_MAP"; + case DT_MIPS_DELTA_CLASS: + return "MIPS_DELTA_CLASS"; + case DT_MIPS_DELTA_CLASS_NO: + return "MIPS_DELTA_CLASS_NO"; + case DT_MIPS_DELTA_INSTANCE: + return "MIPS_DELTA_INSTANCE"; + case DT_MIPS_DELTA_INSTANCE_NO: + return "MIPS_DELTA_INSTANCE_NO"; + case DT_MIPS_DELTA_RELOC: + return "MIPS_DELTA_RELOC"; + case DT_MIPS_DELTA_RELOC_NO: + return "MIPS_DELTA_RELOC_NO"; + case DT_MIPS_DELTA_SYM: + return "MIPS_DELTA_SYM"; + case DT_MIPS_DELTA_SYM_NO: + return "MIPS_DELTA_SYM_NO"; + case DT_MIPS_DELTA_CLASSSYM: + return "MIPS_DELTA_CLASSSYM"; + case DT_MIPS_DELTA_CLASSSYM_NO: + return "MIPS_DELTA_CLASSSYM_NO"; + case DT_MIPS_CXX_FLAGS: + return "MIPS_CXX_FLAGS"; + case DT_MIPS_PIXIE_INIT: + return "MIPS_PIXIE_INIT"; + case DT_MIPS_SYMBOL_LIB: + return "MIPS_SYMBOL_LIB"; + case DT_MIPS_LOCALPAGE_GOTIDX: + return "MIPS_LOCALPAGE_GOTIDX"; + case DT_MIPS_LOCAL_GOTIDX: + return "MIPS_LOCAL_GOTIDX"; + case DT_MIPS_HIDDEN_GOTIDX: + return "MIPS_HIDDEN_GOTIDX"; + case DT_MIPS_PROTECTED_GOTIDX: + return "MIPS_PROTECTED_GOTIDX"; + case DT_MIPS_OPTIONS: + return "MIPS_OPTIONS"; + case DT_MIPS_INTERFACE: + return "MIPS_INTERFACE"; + case DT_MIPS_DYNSTR_ALIGN: + return "MIPS_DYNSTR_ALIGN"; + case DT_MIPS_INTERFACE_SIZE: + return "MIPS_INTERFACE_SIZE"; + case DT_MIPS_RLD_TEXT_RESOLVE_ADDR: + return "MIPS_RLD_TEXT_RESOLVE_ADDR"; + case DT_MIPS_PERF_SUFFIX: + return "MIPS_PERF_SUFFIX"; + case DT_MIPS_COMPACT_SIZE: + return "MIPS_COMPACT_SIZE"; + case DT_MIPS_GP_VALUE: + return "MIPS_GP_VALUE"; + case DT_MIPS_AUX_DYNAMIC: + return "MIPS_AUX_DYNAMIC"; + case DT_MIPS_PLTGOT: + return "MIPS_PLTGOT"; + case DT_MIPS_RLD_OBJ_UPDATE: + return "MIPS_RLD_OBJ_UPDATE"; + case DT_MIPS_RWPLT: + return "MIPS_RWPLT"; + default: + break; + } + break; + case EM_SPARC: + case EM_SPARC32PLUS: + case EM_SPARCV9: + switch (dtype) { + case DT_SPARC_REGISTER: + return "DT_SPARC_REGISTER"; + default: + break; + } + break; + default: + break; + } + snprintf(s_dtype, sizeof(s_dtype), "<unknown: %#x>", dtype); + return (s_dtype); + } + + switch (dtype) { + case DT_NULL: return "NULL"; + case DT_NEEDED: return "NEEDED"; + case DT_PLTRELSZ: return "PLTRELSZ"; + case DT_PLTGOT: return "PLTGOT"; + case DT_HASH: return "HASH"; + case DT_STRTAB: return "STRTAB"; + case DT_SYMTAB: return "SYMTAB"; + case DT_RELA: return "RELA"; + case DT_RELASZ: return "RELASZ"; + case DT_RELAENT: return "RELAENT"; + case DT_STRSZ: return "STRSZ"; + case DT_SYMENT: return "SYMENT"; + case DT_INIT: return "INIT"; + case DT_FINI: return "FINI"; + case DT_SONAME: return "SONAME"; + case DT_RPATH: return "RPATH"; + case DT_SYMBOLIC: return "SYMBOLIC"; + case DT_REL: return "REL"; + case DT_RELSZ: return "RELSZ"; + case DT_RELENT: return "RELENT"; + case DT_PLTREL: return "PLTREL"; + case DT_DEBUG: return "DEBUG"; + case DT_TEXTREL: return "TEXTREL"; + case DT_JMPREL: return "JMPREL"; + case DT_BIND_NOW: return "BIND_NOW"; + case DT_INIT_ARRAY: return "INIT_ARRAY"; + case DT_FINI_ARRAY: return "FINI_ARRAY"; + case DT_INIT_ARRAYSZ: return "INIT_ARRAYSZ"; + case DT_FINI_ARRAYSZ: return "FINI_ARRAYSZ"; + case DT_RUNPATH: return "RUNPATH"; + case DT_FLAGS: return "FLAGS"; + case DT_PREINIT_ARRAY: return "PREINIT_ARRAY"; + case DT_PREINIT_ARRAYSZ: return "PREINIT_ARRAYSZ"; + case DT_MAXPOSTAGS: return "MAXPOSTAGS"; + case DT_SUNW_AUXILIARY: return "SUNW_AUXILIARY"; + case DT_SUNW_RTLDINF: return "SUNW_RTLDINF"; + case DT_SUNW_FILTER: return "SUNW_FILTER"; + case DT_SUNW_CAP: return "SUNW_CAP"; + case DT_CHECKSUM: return "CHECKSUM"; + case DT_PLTPADSZ: return "PLTPADSZ"; + case DT_MOVEENT: return "MOVEENT"; + case DT_MOVESZ: return "MOVESZ"; + case DT_FEATURE: return "FEATURE"; + case DT_POSFLAG_1: return "POSFLAG_1"; + case DT_SYMINSZ: return "SYMINSZ"; + case DT_SYMINENT: return "SYMINENT"; + case DT_GNU_HASH: return "GNU_HASH"; + case DT_GNU_CONFLICT: return "GNU_CONFLICT"; + case DT_GNU_LIBLIST: return "GNU_LIBLIST"; + case DT_CONFIG: return "CONFIG"; + case DT_DEPAUDIT: return "DEPAUDIT"; + case DT_AUDIT: return "AUDIT"; + case DT_PLTPAD: return "PLTPAD"; + case DT_MOVETAB: return "MOVETAB"; + case DT_SYMINFO: return "SYMINFO"; + case DT_VERSYM: return "VERSYM"; + case DT_RELACOUNT: return "RELACOUNT"; + case DT_RELCOUNT: return "RELCOUNT"; + case DT_FLAGS_1: return "FLAGS_1"; + case DT_VERDEF: return "VERDEF"; + case DT_VERDEFNUM: return "VERDEFNUM"; + case DT_VERNEED: return "VERNEED"; + case DT_VERNEEDNUM: return "VERNEEDNUM"; + case DT_AUXILIARY: return "AUXILIARY"; + case DT_USED: return "USED"; + case DT_FILTER: return "FILTER"; + case DT_GNU_PRELINKED: return "GNU_PRELINKED"; + case DT_GNU_CONFLICTSZ: return "GNU_CONFLICTSZ"; + case DT_GNU_LIBLISTSZ: return "GNU_LIBLISTSZ"; + default: + snprintf(s_dtype, sizeof(s_dtype), "<unknown: %#x>", dtype); + return (s_dtype); + } +} + +static const char * +st_bind(unsigned int sbind) +{ + static char s_sbind[32]; + + switch (sbind) { + case STB_LOCAL: return "LOCAL"; + case STB_GLOBAL: return "GLOBAL"; + case STB_WEAK: return "WEAK"; + default: + if (sbind >= STB_LOOS && sbind <= STB_HIOS) + return "OS"; + else if (sbind >= STB_LOPROC && sbind <= STB_HIPROC) + return "PROC"; + else + snprintf(s_sbind, sizeof(s_sbind), "<unknown: %#x>", + sbind); + return (s_sbind); + } +} + +static const char * +st_type(unsigned int stype) +{ + static char s_stype[32]; + + switch (stype) { + case STT_NOTYPE: return "NOTYPE"; + case STT_OBJECT: return "OBJECT"; + case STT_FUNC: return "FUNC"; + case STT_SECTION: return "SECTION"; + case STT_FILE: return "FILE"; + case STT_COMMON: return "COMMON"; + case STT_TLS: return "TLS"; + default: + if (stype >= STT_LOOS && stype <= STT_HIOS) + snprintf(s_stype, sizeof(s_stype), "OS+%#x", + stype - STT_LOOS); + else if (stype >= STT_LOPROC && stype <= STT_HIPROC) + snprintf(s_stype, sizeof(s_stype), "PROC+%#x", + stype - STT_LOPROC); + else + snprintf(s_stype, sizeof(s_stype), "<unknown: %#x>", + stype); + return (s_stype); + } +} + +static const char * +st_vis(unsigned int svis) +{ + static char s_svis[32]; + + switch(svis) { + case STV_DEFAULT: return "DEFAULT"; + case STV_INTERNAL: return "INTERNAL"; + case STV_HIDDEN: return "HIDDEN"; + case STV_PROTECTED: return "PROTECTED"; + default: + snprintf(s_svis, sizeof(s_svis), "<unknown: %#x>", svis); + return (s_svis); + } +} + +static const char * +st_shndx(unsigned int shndx) +{ + static char s_shndx[32]; + + switch (shndx) { + case SHN_UNDEF: return "UND"; + case SHN_ABS: return "ABS"; + case SHN_COMMON: return "COM"; + default: + if (shndx >= SHN_LOPROC && shndx <= SHN_HIPROC) + return "PRC"; + else if (shndx >= SHN_LOOS && shndx <= SHN_HIOS) + return "OS"; + else + snprintf(s_shndx, sizeof(s_shndx), "%u", shndx); + return (s_shndx); + } +} + +static struct { + const char *ln; + char sn; + int value; +} section_flag[] = { + {"WRITE", 'W', SHF_WRITE}, + {"ALLOC", 'A', SHF_ALLOC}, + {"EXEC", 'X', SHF_EXECINSTR}, + {"MERGE", 'M', SHF_MERGE}, + {"STRINGS", 'S', SHF_STRINGS}, + {"INFO LINK", 'I', SHF_INFO_LINK}, + {"OS NONCONF", 'O', SHF_OS_NONCONFORMING}, + {"GROUP", 'G', SHF_GROUP}, + {"TLS", 'T', SHF_TLS}, + {NULL, 0, 0} +}; + +static const char * +r_type(unsigned int mach, unsigned int type) +{ + switch(mach) { + case EM_NONE: return ""; + case EM_386: + switch(type) { + case 0: return "R_386_NONE"; + case 1: return "R_386_32"; + case 2: return "R_386_PC32"; + case 3: return "R_386_GOT32"; + case 4: return "R_386_PLT32"; + case 5: return "R_386_COPY"; + case 6: return "R_386_GLOB_DAT"; + case 7: return "R_386_JMP_SLOT"; + case 8: return "R_386_RELATIVE"; + case 9: return "R_386_GOTOFF"; + case 10: return "R_386_GOTPC"; + case 14: return "R_386_TLS_TPOFF"; + case 15: return "R_386_TLS_IE"; + case 16: return "R_386_TLS_GOTIE"; + case 17: return "R_386_TLS_LE"; + case 18: return "R_386_TLS_GD"; + case 19: return "R_386_TLS_LDM"; + case 24: return "R_386_TLS_GD_32"; + case 25: return "R_386_TLS_GD_PUSH"; + case 26: return "R_386_TLS_GD_CALL"; + case 27: return "R_386_TLS_GD_POP"; + case 28: return "R_386_TLS_LDM_32"; + case 29: return "R_386_TLS_LDM_PUSH"; + case 30: return "R_386_TLS_LDM_CALL"; + case 31: return "R_386_TLS_LDM_POP"; + case 32: return "R_386_TLS_LDO_32"; + case 33: return "R_386_TLS_IE_32"; + case 34: return "R_386_TLS_LE_32"; + case 35: return "R_386_TLS_DTPMOD32"; + case 36: return "R_386_TLS_DTPOFF32"; + case 37: return "R_386_TLS_TPOFF32"; + default: return ""; + } + case EM_AARCH64: + switch(type) { + case 0: return "R_AARCH64_NONE"; + case 257: return "R_AARCH64_ABS64"; + case 258: return "R_AARCH64_ABS32"; + case 259: return "R_AARCH64_ABS16"; + case 260: return "R_AARCH64_PREL64"; + case 261: return "R_AARCH64_PREL32"; + case 262: return "R_AARCH64_PREL16"; + case 263: return "R_AARCH64_MOVW_UABS_G0"; + case 264: return "R_AARCH64_MOVW_UABS_G0_NC"; + case 265: return "R_AARCH64_MOVW_UABS_G1"; + case 266: return "R_AARCH64_MOVW_UABS_G1_NC"; + case 267: return "R_AARCH64_MOVW_UABS_G2"; + case 268: return "R_AARCH64_MOVW_UABS_G2_NC"; + case 269: return "R_AARCH64_MOVW_UABS_G3"; + case 270: return "R_AARCH64_MOVW_SABS_G0"; + case 271: return "R_AARCH64_MOVW_SABS_G1"; + case 272: return "R_AARCH64_MOVW_SABS_G2"; + case 273: return "R_AARCH64_LD_PREL_LO19"; + case 274: return "R_AARCH64_ADR_PREL_LO21"; + case 275: return "R_AARCH64_ADR_PREL_PG_HI21"; + case 276: return "R_AARCH64_ADR_PREL_PG_HI21_NC"; + case 277: return "R_AARCH64_ADD_ABS_LO12_NC"; + case 278: return "R_AARCH64_LDST8_ABS_LO12_NC"; + case 279: return "R_AARCH64_TSTBR14"; + case 280: return "R_AARCH64_CONDBR19"; + case 282: return "R_AARCH64_JUMP26"; + case 283: return "R_AARCH64_CALL26"; + case 284: return "R_AARCH64_LDST16_ABS_LO12_NC"; + case 285: return "R_AARCH64_LDST32_ABS_LO12_NC"; + case 286: return "R_AARCH64_LDST64_ABS_LO12_NC"; + case 287: return "R_AARCH64_MOVW_PREL_G0"; + case 288: return "R_AARCH64_MOVW_PREL_G0_NC"; + case 289: return "R_AARCH64_MOVW_PREL_G1"; + case 290: return "R_AARCH64_MOVW_PREL_G1_NC"; + case 291: return "R_AARCH64_MOVW_PREL_G2"; + case 292: return "R_AARCH64_MOVW_PREL_G2_NC"; + case 293: return "R_AARCH64_MOVW_PREL_G3"; + case 299: return "R_AARCH64_LDST128_ABS_LO12_NC"; + case 300: return "R_AARCH64_MOVW_GOTOFF_G0"; + case 301: return "R_AARCH64_MOVW_GOTOFF_G0_NC"; + case 302: return "R_AARCH64_MOVW_GOTOFF_G1"; + case 303: return "R_AARCH64_MOVW_GOTOFF_G1_NC"; + case 304: return "R_AARCH64_MOVW_GOTOFF_G2"; + case 305: return "R_AARCH64_MOVW_GOTOFF_G2_NC"; + case 306: return "R_AARCH64_MOVW_GOTOFF_G3"; + case 307: return "R_AARCH64_GOTREL64"; + case 308: return "R_AARCH64_GOTREL32"; + case 309: return "R_AARCH64_GOT_LD_PREL19"; + case 310: return "R_AARCH64_LD64_GOTOFF_LO15"; + case 311: return "R_AARCH64_ADR_GOT_PAGE"; + case 312: return "R_AARCH64_LD64_GOT_LO12_NC"; + case 313: return "R_AARCH64_LD64_GOTPAGE_LO15"; + case 1024: return "R_AARCH64_COPY"; + case 1025: return "R_AARCH64_GLOB_DAT"; + case 1026: return "R_AARCH64_JUMP_SLOT"; + case 1027: return "R_AARCH64_RELATIVE"; + case 1028: return "R_AARCH64_TLS_DTPREL64"; + case 1029: return "R_AARCH64_TLS_DTPMOD64"; + case 1030: return "R_AARCH64_TLS_TPREL64"; + case 1031: return "R_AARCH64_TLSDESC"; + case 1032: return "R_AARCH64_IRELATIVE"; + default: return ""; + } + case EM_ARM: + switch(type) { + case 0: return "R_ARM_NONE"; + case 1: return "R_ARM_PC24"; + case 2: return "R_ARM_ABS32"; + case 3: return "R_ARM_REL32"; + case 4: return "R_ARM_PC13"; + case 5: return "R_ARM_ABS16"; + case 6: return "R_ARM_ABS12"; + case 7: return "R_ARM_THM_ABS5"; + case 8: return "R_ARM_ABS8"; + case 9: return "R_ARM_SBREL32"; + 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 14: return "R_ARM_THM_SWI8"; + case 15: return "R_ARM_XPC25"; + case 16: return "R_ARM_THM_XPC22"; + case 20: return "R_ARM_COPY"; + case 21: return "R_ARM_GLOB_DAT"; + case 22: return "R_ARM_JUMP_SLOT"; + case 23: return "R_ARM_RELATIVE"; + case 24: return "R_ARM_GOTOFF"; + case 25: return "R_ARM_GOTPC"; + case 26: return "R_ARM_GOT32"; + case 27: return "R_ARM_PLT32"; + case 100: return "R_ARM_GNU_VTENTRY"; + case 101: return "R_ARM_GNU_VTINHERIT"; + case 250: return "R_ARM_RSBREL32"; + case 251: return "R_ARM_THM_RPC22"; + case 252: return "R_ARM_RREL32"; + case 253: return "R_ARM_RABS32"; + case 254: return "R_ARM_RPC24"; + case 255: return "R_ARM_RBASE"; + default: return ""; + } + case EM_IA_64: + switch(type) { + case 0: return "R_IA_64_NONE"; + case 33: return "R_IA_64_IMM14"; + case 34: return "R_IA_64_IMM22"; + case 35: return "R_IA_64_IMM64"; + case 36: return "R_IA_64_DIR32MSB"; + case 37: return "R_IA_64_DIR32LSB"; + case 38: return "R_IA_64_DIR64MSB"; + case 39: return "R_IA_64_DIR64LSB"; + case 42: return "R_IA_64_GPREL22"; + case 43: return "R_IA_64_GPREL64I"; + case 44: return "R_IA_64_GPREL32MSB"; + case 45: return "R_IA_64_GPREL32LSB"; + case 46: return "R_IA_64_GPREL64MSB"; + case 47: return "R_IA_64_GPREL64LSB"; + case 50: return "R_IA_64_LTOFF22"; + case 51: return "R_IA_64_LTOFF64I"; + case 58: return "R_IA_64_PLTOFF22"; + case 59: return "R_IA_64_PLTOFF64I"; + case 62: return "R_IA_64_PLTOFF64MSB"; + case 63: return "R_IA_64_PLTOFF64LSB"; + case 67: return "R_IA_64_FPTR64I"; + case 68: return "R_IA_64_FPTR32MSB"; + case 69: return "R_IA_64_FPTR32LSB"; + case 70: return "R_IA_64_FPTR64MSB"; + case 71: return "R_IA_64_FPTR64LSB"; + case 72: return "R_IA_64_PCREL60B"; + case 73: return "R_IA_64_PCREL21B"; + case 74: return "R_IA_64_PCREL21M"; + case 75: return "R_IA_64_PCREL21F"; + case 76: return "R_IA_64_PCREL32MSB"; + case 77: return "R_IA_64_PCREL32LSB"; + case 78: return "R_IA_64_PCREL64MSB"; + case 79: return "R_IA_64_PCREL64LSB"; + case 82: return "R_IA_64_LTOFF_FPTR22"; + case 83: return "R_IA_64_LTOFF_FPTR64I"; + case 84: return "R_IA_64_LTOFF_FPTR32MSB"; + case 85: return "R_IA_64_LTOFF_FPTR32LSB"; + case 86: return "R_IA_64_LTOFF_FPTR64MSB"; + case 87: return "R_IA_64_LTOFF_FPTR64LSB"; + case 92: return "R_IA_64_SEGREL32MSB"; + case 93: return "R_IA_64_SEGREL32LSB"; + case 94: return "R_IA_64_SEGREL64MSB"; + case 95: return "R_IA_64_SEGREL64LSB"; + case 100: return "R_IA_64_SECREL32MSB"; + case 101: return "R_IA_64_SECREL32LSB"; + case 102: return "R_IA_64_SECREL64MSB"; + case 103: return "R_IA_64_SECREL64LSB"; + case 108: return "R_IA_64_REL32MSB"; + case 109: return "R_IA_64_REL32LSB"; + case 110: return "R_IA_64_REL64MSB"; + case 111: return "R_IA_64_REL64LSB"; + case 116: return "R_IA_64_LTV32MSB"; + case 117: return "R_IA_64_LTV32LSB"; + case 118: return "R_IA_64_LTV64MSB"; + case 119: return "R_IA_64_LTV64LSB"; + case 121: return "R_IA_64_PCREL21BI"; + case 122: return "R_IA_64_PCREL22"; + case 123: return "R_IA_64_PCREL64I"; + case 128: return "R_IA_64_IPLTMSB"; + case 129: return "R_IA_64_IPLTLSB"; + case 133: return "R_IA_64_SUB"; + case 134: return "R_IA_64_LTOFF22X"; + case 135: return "R_IA_64_LDXMOV"; + case 145: return "R_IA_64_TPREL14"; + case 146: return "R_IA_64_TPREL22"; + case 147: return "R_IA_64_TPREL64I"; + case 150: return "R_IA_64_TPREL64MSB"; + case 151: return "R_IA_64_TPREL64LSB"; + case 154: return "R_IA_64_LTOFF_TPREL22"; + case 166: return "R_IA_64_DTPMOD64MSB"; + case 167: return "R_IA_64_DTPMOD64LSB"; + case 170: return "R_IA_64_LTOFF_DTPMOD22"; + case 177: return "R_IA_64_DTPREL14"; + case 178: return "R_IA_64_DTPREL22"; + case 179: return "R_IA_64_DTPREL64I"; + case 180: return "R_IA_64_DTPREL32MSB"; + case 181: return "R_IA_64_DTPREL32LSB"; + case 182: return "R_IA_64_DTPREL64MSB"; + case 183: return "R_IA_64_DTPREL64LSB"; + case 186: return "R_IA_64_LTOFF_DTPREL22"; + default: return ""; + } + case EM_MIPS: + switch(type) { + case 0: return "R_MIPS_NONE"; + case 1: return "R_MIPS_16"; + case 2: return "R_MIPS_32"; + case 3: return "R_MIPS_REL32"; + case 4: return "R_MIPS_26"; + case 5: return "R_MIPS_HI16"; + case 6: return "R_MIPS_LO16"; + case 7: return "R_MIPS_GPREL16"; + case 8: return "R_MIPS_LITERAL"; + case 9: return "R_MIPS_GOT16"; + case 10: return "R_MIPS_PC16"; + case 11: return "R_MIPS_CALL16"; + case 12: return "R_MIPS_GPREL32"; + case 21: return "R_MIPS_GOTHI16"; + case 22: return "R_MIPS_GOTLO16"; + case 30: return "R_MIPS_CALLHI16"; + case 31: return "R_MIPS_CALLLO16"; + default: return ""; + } + case EM_PPC: + switch(type) { + case 0: return "R_PPC_NONE"; + case 1: return "R_PPC_ADDR32"; + case 2: return "R_PPC_ADDR24"; + case 3: return "R_PPC_ADDR16"; + case 4: return "R_PPC_ADDR16_LO"; + case 5: return "R_PPC_ADDR16_HI"; + case 6: return "R_PPC_ADDR16_HA"; + case 7: return "R_PPC_ADDR14"; + case 8: return "R_PPC_ADDR14_BRTAKEN"; + case 9: return "R_PPC_ADDR14_BRNTAKEN"; + case 10: return "R_PPC_REL24"; + case 11: return "R_PPC_REL14"; + case 12: return "R_PPC_REL14_BRTAKEN"; + case 13: return "R_PPC_REL14_BRNTAKEN"; + case 14: return "R_PPC_GOT16"; + case 15: return "R_PPC_GOT16_LO"; + case 16: return "R_PPC_GOT16_HI"; + case 17: return "R_PPC_GOT16_HA"; + case 18: return "R_PPC_PLTREL24"; + case 19: return "R_PPC_COPY"; + case 20: return "R_PPC_GLOB_DAT"; + case 21: return "R_PPC_JMP_SLOT"; + case 22: return "R_PPC_RELATIVE"; + case 23: return "R_PPC_LOCAL24PC"; + case 24: return "R_PPC_UADDR32"; + case 25: return "R_PPC_UADDR16"; + case 26: return "R_PPC_REL32"; + case 27: return "R_PPC_PLT32"; + case 28: return "R_PPC_PLTREL32"; + case 29: return "R_PPC_PLT16_LO"; + case 30: return "R_PPC_PLT16_HI"; + case 31: return "R_PPC_PLT16_HA"; + case 32: return "R_PPC_SDAREL16"; + case 33: return "R_PPC_SECTOFF"; + case 34: return "R_PPC_SECTOFF_LO"; + case 35: return "R_PPC_SECTOFF_HI"; + case 36: return "R_PPC_SECTOFF_HA"; + case 67: return "R_PPC_TLS"; + case 68: return "R_PPC_DTPMOD32"; + case 69: return "R_PPC_TPREL16"; + case 70: return "R_PPC_TPREL16_LO"; + case 71: return "R_PPC_TPREL16_HI"; + case 72: return "R_PPC_TPREL16_HA"; + case 73: return "R_PPC_TPREL32"; + case 74: return "R_PPC_DTPREL16"; + case 75: return "R_PPC_DTPREL16_LO"; + case 76: return "R_PPC_DTPREL16_HI"; + case 77: return "R_PPC_DTPREL16_HA"; + case 78: return "R_PPC_DTPREL32"; + case 79: return "R_PPC_GOT_TLSGD16"; + case 80: return "R_PPC_GOT_TLSGD16_LO"; + case 81: return "R_PPC_GOT_TLSGD16_HI"; + case 82: return "R_PPC_GOT_TLSGD16_HA"; + case 83: return "R_PPC_GOT_TLSLD16"; + case 84: return "R_PPC_GOT_TLSLD16_LO"; + case 85: return "R_PPC_GOT_TLSLD16_HI"; + case 86: return "R_PPC_GOT_TLSLD16_HA"; + case 87: return "R_PPC_GOT_TPREL16"; + case 88: return "R_PPC_GOT_TPREL16_LO"; + case 89: return "R_PPC_GOT_TPREL16_HI"; + case 90: return "R_PPC_GOT_TPREL16_HA"; + case 101: return "R_PPC_EMB_NADDR32"; + case 102: return "R_PPC_EMB_NADDR16"; + case 103: return "R_PPC_EMB_NADDR16_LO"; + case 104: return "R_PPC_EMB_NADDR16_HI"; + case 105: return "R_PPC_EMB_NADDR16_HA"; + case 106: return "R_PPC_EMB_SDAI16"; + case 107: return "R_PPC_EMB_SDA2I16"; + case 108: return "R_PPC_EMB_SDA2REL"; + case 109: return "R_PPC_EMB_SDA21"; + case 110: return "R_PPC_EMB_MRKREF"; + case 111: return "R_PPC_EMB_RELSEC16"; + case 112: return "R_PPC_EMB_RELST_LO"; + case 113: return "R_PPC_EMB_RELST_HI"; + case 114: return "R_PPC_EMB_RELST_HA"; + case 115: return "R_PPC_EMB_BIT_FLD"; + case 116: return "R_PPC_EMB_RELSDA"; + default: return ""; + } + case EM_SPARC: + case EM_SPARCV9: + switch(type) { + case 0: return "R_SPARC_NONE"; + case 1: return "R_SPARC_8"; + case 2: return "R_SPARC_16"; + case 3: return "R_SPARC_32"; + case 4: return "R_SPARC_DISP8"; + case 5: return "R_SPARC_DISP16"; + case 6: return "R_SPARC_DISP32"; + case 7: return "R_SPARC_WDISP30"; + case 8: return "R_SPARC_WDISP22"; + case 9: return "R_SPARC_HI22"; + case 10: return "R_SPARC_22"; + case 11: return "R_SPARC_13"; + case 12: return "R_SPARC_LO10"; + case 13: return "R_SPARC_GOT10"; + case 14: return "R_SPARC_GOT13"; + case 15: return "R_SPARC_GOT22"; + case 16: return "R_SPARC_PC10"; + case 17: return "R_SPARC_PC22"; + case 18: return "R_SPARC_WPLT30"; + case 19: return "R_SPARC_COPY"; + case 20: return "R_SPARC_GLOB_DAT"; + case 21: return "R_SPARC_JMP_SLOT"; + case 22: return "R_SPARC_RELATIVE"; + case 23: return "R_SPARC_UA32"; + case 24: return "R_SPARC_PLT32"; + case 25: return "R_SPARC_HIPLT22"; + case 26: return "R_SPARC_LOPLT10"; + case 27: return "R_SPARC_PCPLT32"; + case 28: return "R_SPARC_PCPLT22"; + case 29: return "R_SPARC_PCPLT10"; + case 30: return "R_SPARC_10"; + case 31: return "R_SPARC_11"; + case 32: return "R_SPARC_64"; + case 33: return "R_SPARC_OLO10"; + case 34: return "R_SPARC_HH22"; + case 35: return "R_SPARC_HM10"; + case 36: return "R_SPARC_LM22"; + case 37: return "R_SPARC_PC_HH22"; + case 38: return "R_SPARC_PC_HM10"; + case 39: return "R_SPARC_PC_LM22"; + case 40: return "R_SPARC_WDISP16"; + case 41: return "R_SPARC_WDISP19"; + case 42: return "R_SPARC_GLOB_JMP"; + case 43: return "R_SPARC_7"; + case 44: return "R_SPARC_5"; + case 45: return "R_SPARC_6"; + case 46: return "R_SPARC_DISP64"; + case 47: return "R_SPARC_PLT64"; + case 48: return "R_SPARC_HIX22"; + case 49: return "R_SPARC_LOX10"; + case 50: return "R_SPARC_H44"; + case 51: return "R_SPARC_M44"; + case 52: return "R_SPARC_L44"; + case 53: return "R_SPARC_REGISTER"; + case 54: return "R_SPARC_UA64"; + case 55: return "R_SPARC_UA16"; + case 56: return "R_SPARC_TLS_GD_HI22"; + case 57: return "R_SPARC_TLS_GD_LO10"; + case 58: return "R_SPARC_TLS_GD_ADD"; + case 59: return "R_SPARC_TLS_GD_CALL"; + case 60: return "R_SPARC_TLS_LDM_HI22"; + case 61: return "R_SPARC_TLS_LDM_LO10"; + case 62: return "R_SPARC_TLS_LDM_ADD"; + case 63: return "R_SPARC_TLS_LDM_CALL"; + case 64: return "R_SPARC_TLS_LDO_HIX22"; + case 65: return "R_SPARC_TLS_LDO_LOX10"; + case 66: return "R_SPARC_TLS_LDO_ADD"; + case 67: return "R_SPARC_TLS_IE_HI22"; + case 68: return "R_SPARC_TLS_IE_LO10"; + case 69: return "R_SPARC_TLS_IE_LD"; + case 70: return "R_SPARC_TLS_IE_LDX"; + case 71: return "R_SPARC_TLS_IE_ADD"; + case 72: return "R_SPARC_TLS_LE_HIX22"; + case 73: return "R_SPARC_TLS_LE_LOX10"; + case 74: return "R_SPARC_TLS_DTPMOD32"; + case 75: return "R_SPARC_TLS_DTPMOD64"; + case 76: return "R_SPARC_TLS_DTPOFF32"; + case 77: return "R_SPARC_TLS_DTPOFF64"; + case 78: return "R_SPARC_TLS_TPOFF32"; + case 79: return "R_SPARC_TLS_TPOFF64"; + default: return ""; + } + case EM_X86_64: + switch(type) { + case 0: return "R_X86_64_NONE"; + case 1: return "R_X86_64_64"; + case 2: return "R_X86_64_PC32"; + case 3: return "R_X86_64_GOT32"; + case 4: return "R_X86_64_PLT32"; + case 5: return "R_X86_64_COPY"; + case 6: return "R_X86_64_GLOB_DAT"; + case 7: return "R_X86_64_JMP_SLOT"; + case 8: return "R_X86_64_RELATIVE"; + case 9: return "R_X86_64_GOTPCREL"; + case 10: return "R_X86_64_32"; + case 11: return "R_X86_64_32S"; + case 12: return "R_X86_64_16"; + case 13: return "R_X86_64_PC16"; + case 14: return "R_X86_64_8"; + case 15: return "R_X86_64_PC8"; + case 16: return "R_X86_64_DTPMOD64"; + case 17: return "R_X86_64_DTPOFF64"; + case 18: return "R_X86_64_TPOFF64"; + case 19: return "R_X86_64_TLSGD"; + case 20: return "R_X86_64_TLSLD"; + case 21: return "R_X86_64_DTPOFF32"; + case 22: return "R_X86_64_GOTTPOFF"; + case 23: return "R_X86_64_TPOFF32"; + case 24: return "R_X86_64_PC64"; + case 25: return "R_X86_64_GOTOFF64"; + case 26: return "R_X86_64_GOTPC32"; + case 27: return "R_X86_64_GOT64"; + case 28: return "R_X86_64_GOTPCREL64"; + case 29: return "R_X86_64_GOTPC64"; + case 30: return "R_X86_64_GOTPLT64"; + case 31: return "R_X86_64_PLTOFF64"; + case 32: return "R_X86_64_SIZE32"; + case 33: return "R_X86_64_SIZE64"; + case 34: return "R_X86_64_GOTPC32_TLSDESC"; + case 35: return "R_X86_64_TLSDESC_CALL"; + case 36: return "R_X86_64_TLSDESC"; + case 37: return "R_X86_64_IRELATIVE"; + default: return ""; + } + default: return ""; + } +} + +static const char * +note_type(const char *name, unsigned int et, unsigned int nt) +{ + if ((strcmp(name, "CORE") == 0 || strcmp(name, "LINUX") == 0) && + et == ET_CORE) + return note_type_linux_core(nt); + else if (strcmp(name, "FreeBSD") == 0) + if (et == ET_CORE) + return note_type_freebsd_core(nt); + else + return note_type_freebsd(nt); + else if (strcmp(name, "GNU") == 0 && et != ET_CORE) + return note_type_gnu(nt); + else if (strcmp(name, "NetBSD") == 0 && et != ET_CORE) + return note_type_netbsd(nt); + else if (strcmp(name, "OpenBSD") == 0 && et != ET_CORE) + return note_type_openbsd(nt); + return note_type_unknown(nt); +} + +static const char * +note_type_freebsd(unsigned int nt) +{ + switch (nt) { + case 1: return "NT_FREEBSD_ABI_TAG"; + case 2: return "NT_FREEBSD_NOINIT_TAG"; + case 3: return "NT_FREEBSD_ARCH_TAG"; + default: return (note_type_unknown(nt)); + } +} + +static const char * +note_type_freebsd_core(unsigned int nt) +{ + switch (nt) { + case 1: return "NT_PRSTATUS"; + case 2: return "NT_FPREGSET"; + case 3: return "NT_PRPSINFO"; + case 7: return "NT_THRMISC"; + case 8: return "NT_PROCSTAT_PROC"; + case 9: return "NT_PROCSTAT_FILES"; + case 10: return "NT_PROCSTAT_VMMAP"; + case 11: return "NT_PROCSTAT_GROUPS"; + case 12: return "NT_PROCSTAT_UMASK"; + case 13: return "NT_PROCSTAT_RLIMIT"; + case 14: return "NT_PROCSTAT_OSREL"; + case 15: return "NT_PROCSTAT_PSSTRINGS"; + case 16: return "NT_PROCSTAT_AUXV"; + case 0x202: return "NT_X86_XSTATE (x86 XSAVE extended state)"; + default: return (note_type_unknown(nt)); + } +} + +static const char * +note_type_linux_core(unsigned int nt) +{ + switch (nt) { + case 1: return "NT_PRSTATUS (Process status)"; + case 2: return "NT_FPREGSET (Floating point information)"; + case 3: return "NT_PRPSINFO (Process information)"; + case 4: return "NT_TASKSTRUCT (Task structure)"; + case 6: return "NT_AUXV (Auxiliary vector)"; + case 10: return "NT_PSTATUS (Linux process status)"; + case 12: return "NT_FPREGS (Linux floating point regset)"; + case 13: return "NT_PSINFO (Linux process information)"; + case 16: return "NT_LWPSTATUS (Linux lwpstatus_t type)"; + case 17: return "NT_LWPSINFO (Linux lwpinfo_t type)"; + case 18: return "NT_WIN32PSTATUS (win32_pstatus structure)"; + case 0x100: return "NT_PPC_VMX (ppc Altivec registers)"; + case 0x102: return "NT_PPC_VSX (ppc VSX registers)"; + case 0x202: return "NT_X86_XSTATE (x86 XSAVE extended state)"; + case 0x300: return "NT_S390_HIGH_GPRS (s390 upper register halves)"; + case 0x301: return "NT_S390_TIMER (s390 timer register)"; + case 0x302: return "NT_S390_TODCMP (s390 TOD comparator register)"; + case 0x303: return "NT_S390_TODPREG (s390 TOD programmable register)"; + case 0x304: return "NT_S390_CTRS (s390 control registers)"; + case 0x305: return "NT_S390_PREFIX (s390 prefix register)"; + case 0x400: return "NT_ARM_VFP (arm VFP registers)"; + case 0x46494c45UL: return "NT_FILE (mapped files)"; + case 0x46E62B7FUL: return "NT_PRXFPREG (Linux user_xfpregs structure)"; + case 0x53494749UL: return "NT_SIGINFO (siginfo_t data)"; + default: return (note_type_unknown(nt)); + } +} + +static const char * +note_type_gnu(unsigned int nt) +{ + switch (nt) { + case 1: return "NT_GNU_ABI_TAG"; + case 2: return "NT_GNU_HWCAP (Hardware capabilities)"; + case 3: return "NT_GNU_BUILD_ID (Build id set by ld(1))"; + case 4: return "NT_GNU_GOLD_VERSION (GNU gold version)"; + default: return (note_type_unknown(nt)); + } +} + +static const char * +note_type_netbsd(unsigned int nt) +{ + switch (nt) { + case 1: return "NT_NETBSD_IDENT"; + default: return (note_type_unknown(nt)); + } +} + +static const char * +note_type_openbsd(unsigned int nt) +{ + switch (nt) { + case 1: return "NT_OPENBSD_IDENT"; + default: return (note_type_unknown(nt)); + } +} + +static const char * +note_type_unknown(unsigned int nt) +{ + static char s_nt[32]; + + snprintf(s_nt, sizeof(s_nt), + nt >= 0x100 ? "<unknown: 0x%x>" : "<unknown: %u>", nt); + return (s_nt); +} + +static struct { + const char *name; + int value; +} l_flag[] = { + {"EXACT_MATCH", LL_EXACT_MATCH}, + {"IGNORE_INT_VER", LL_IGNORE_INT_VER}, + {"REQUIRE_MINOR", LL_REQUIRE_MINOR}, + {"EXPORTS", LL_EXPORTS}, + {"DELAY_LOAD", LL_DELAY_LOAD}, + {"DELTA", LL_DELTA}, + {NULL, 0} +}; + +static struct mips_option mips_exceptions_option[] = { + {OEX_PAGE0, "PAGE0"}, + {OEX_SMM, "SMM"}, + {OEX_PRECISEFP, "PRECISEFP"}, + {OEX_DISMISS, "DISMISS"}, + {0, NULL} +}; + +static struct mips_option mips_pad_option[] = { + {OPAD_PREFIX, "PREFIX"}, + {OPAD_POSTFIX, "POSTFIX"}, + {OPAD_SYMBOL, "SYMBOL"}, + {0, NULL} +}; + +static struct mips_option mips_hwpatch_option[] = { + {OHW_R4KEOP, "R4KEOP"}, + {OHW_R8KPFETCH, "R8KPFETCH"}, + {OHW_R5KEOP, "R5KEOP"}, + {OHW_R5KCVTL, "R5KCVTL"}, + {0, NULL} +}; + +static struct mips_option mips_hwa_option[] = { + {OHWA0_R4KEOP_CHECKED, "R4KEOP_CHECKED"}, + {OHWA0_R4KEOP_CLEAN, "R4KEOP_CLEAN"}, + {0, NULL} +}; + +static struct mips_option mips_hwo_option[] = { + {OHWO0_FIXADE, "FIXADE"}, + {0, NULL} +}; + +static const char * +option_kind(uint8_t kind) +{ + static char s_kind[32]; + + switch (kind) { + case ODK_NULL: return "NULL"; + case ODK_REGINFO: return "REGINFO"; + case ODK_EXCEPTIONS: return "EXCEPTIONS"; + case ODK_PAD: return "PAD"; + case ODK_HWPATCH: return "HWPATCH"; + case ODK_FILL: return "FILL"; + case ODK_TAGS: return "TAGS"; + case ODK_HWAND: return "HWAND"; + case ODK_HWOR: return "HWOR"; + case ODK_GP_GROUP: return "GP_GROUP"; + case ODK_IDENT: return "IDENT"; + default: + snprintf(s_kind, sizeof(s_kind), "<unknown: %u>", kind); + return (s_kind); + } +} + +static const char * +top_tag(unsigned int tag) +{ + static char s_top_tag[32]; + + switch (tag) { + case 1: return "File Attributes"; + case 2: return "Section Attributes"; + case 3: return "Symbol Attributes"; + default: + snprintf(s_top_tag, sizeof(s_top_tag), "Unknown tag: %u", tag); + return (s_top_tag); + } +} + +static const char * +aeabi_cpu_arch(uint64_t arch) +{ + static char s_cpu_arch[32]; + + switch (arch) { + case 0: return "Pre-V4"; + case 1: return "ARM v4"; + case 2: return "ARM v4T"; + case 3: return "ARM v5T"; + case 4: return "ARM v5TE"; + case 5: return "ARM v5TEJ"; + case 6: return "ARM v6"; + case 7: return "ARM v6KZ"; + case 8: return "ARM v6T2"; + case 9: return "ARM v6K"; + case 10: return "ARM v7"; + case 11: return "ARM v6-M"; + case 12: return "ARM v6S-M"; + case 13: return "ARM v7E-M"; + default: + snprintf(s_cpu_arch, sizeof(s_cpu_arch), + "Unknown (%ju)", (uintmax_t) arch); + return (s_cpu_arch); + } +} + +static const char * +aeabi_cpu_arch_profile(uint64_t pf) +{ + static char s_arch_profile[32]; + + switch (pf) { + case 0: + return "Not applicable"; + case 0x41: /* 'A' */ + return "Application Profile"; + case 0x52: /* 'R' */ + return "Real-Time Profile"; + case 0x4D: /* 'M' */ + return "Microcontroller Profile"; + case 0x53: /* 'S' */ + return "Application or Real-Time Profile"; + default: + snprintf(s_arch_profile, sizeof(s_arch_profile), + "Unknown (%ju)\n", (uintmax_t) pf); + return (s_arch_profile); + } +} + +static const char * +aeabi_arm_isa(uint64_t ai) +{ + static char s_ai[32]; + + switch (ai) { + case 0: return "No"; + case 1: return "Yes"; + default: + snprintf(s_ai, sizeof(s_ai), "Unknown (%ju)\n", + (uintmax_t) ai); + return (s_ai); + } +} + +static const char * +aeabi_thumb_isa(uint64_t ti) +{ + static char s_ti[32]; + + switch (ti) { + case 0: return "No"; + case 1: return "16-bit Thumb"; + case 2: return "32-bit Thumb"; + default: + snprintf(s_ti, sizeof(s_ti), "Unknown (%ju)\n", + (uintmax_t) ti); + return (s_ti); + } +} + +static const char * +aeabi_fp_arch(uint64_t fp) +{ + static char s_fp_arch[32]; + + switch (fp) { + case 0: return "No"; + case 1: return "VFPv1"; + case 2: return "VFPv2"; + case 3: return "VFPv3"; + case 4: return "VFPv3-D16"; + case 5: return "VFPv4"; + case 6: return "VFPv4-D16"; + default: + snprintf(s_fp_arch, sizeof(s_fp_arch), "Unknown (%ju)", + (uintmax_t) fp); + return (s_fp_arch); + } +} + +static const char * +aeabi_wmmx_arch(uint64_t wmmx) +{ + static char s_wmmx[32]; + + switch (wmmx) { + case 0: return "No"; + case 1: return "WMMXv1"; + case 2: return "WMMXv2"; + default: + snprintf(s_wmmx, sizeof(s_wmmx), "Unknown (%ju)", + (uintmax_t) wmmx); + return (s_wmmx); + } +} + +static const char * +aeabi_adv_simd_arch(uint64_t simd) +{ + static char s_simd[32]; + + switch (simd) { + case 0: return "No"; + case 1: return "NEONv1"; + case 2: return "NEONv2"; + default: + snprintf(s_simd, sizeof(s_simd), "Unknown (%ju)", + (uintmax_t) simd); + return (s_simd); + } +} + +static const char * +aeabi_pcs_config(uint64_t pcs) +{ + static char s_pcs[32]; + + switch (pcs) { + case 0: return "None"; + case 1: return "Bare platform"; + case 2: return "Linux"; + case 3: return "Linux DSO"; + case 4: return "Palm OS 2004"; + case 5: return "Palm OS (future)"; + case 6: return "Symbian OS 2004"; + case 7: return "Symbian OS (future)"; + default: + snprintf(s_pcs, sizeof(s_pcs), "Unknown (%ju)", + (uintmax_t) pcs); + return (s_pcs); + } +} + +static const char * +aeabi_pcs_r9(uint64_t r9) +{ + static char s_r9[32]; + + switch (r9) { + case 0: return "V6"; + case 1: return "SB"; + case 2: return "TLS pointer"; + case 3: return "Unused"; + default: + snprintf(s_r9, sizeof(s_r9), "Unknown (%ju)", (uintmax_t) r9); + return (s_r9); + } +} + +static const char * +aeabi_pcs_rw(uint64_t rw) +{ + static char s_rw[32]; + + switch (rw) { + case 0: return "Absolute"; + case 1: return "PC-relative"; + case 2: return "SB-relative"; + case 3: return "None"; + default: + snprintf(s_rw, sizeof(s_rw), "Unknown (%ju)", (uintmax_t) rw); + return (s_rw); + } +} + +static const char * +aeabi_pcs_ro(uint64_t ro) +{ + static char s_ro[32]; + + switch (ro) { + case 0: return "Absolute"; + case 1: return "PC-relative"; + case 2: return "None"; + default: + snprintf(s_ro, sizeof(s_ro), "Unknown (%ju)", (uintmax_t) ro); + return (s_ro); + } +} + +static const char * +aeabi_pcs_got(uint64_t got) +{ + static char s_got[32]; + + switch (got) { + case 0: return "None"; + case 1: return "direct"; + case 2: return "indirect via GOT"; + default: + snprintf(s_got, sizeof(s_got), "Unknown (%ju)", + (uintmax_t) got); + return (s_got); + } +} + +static const char * +aeabi_pcs_wchar_t(uint64_t wt) +{ + static char s_wt[32]; + + switch (wt) { + case 0: return "None"; + case 2: return "wchar_t size 2"; + case 4: return "wchar_t size 4"; + default: + snprintf(s_wt, sizeof(s_wt), "Unknown (%ju)", (uintmax_t) wt); + return (s_wt); + } +} + +static const char * +aeabi_enum_size(uint64_t es) +{ + static char s_es[32]; + + switch (es) { + case 0: return "None"; + case 1: return "smallest"; + case 2: return "32-bit"; + case 3: return "visible 32-bit"; + default: + snprintf(s_es, sizeof(s_es), "Unknown (%ju)", (uintmax_t) es); + return (s_es); + } +} + +static const char * +aeabi_align_needed(uint64_t an) +{ + static char s_align_n[64]; + + switch (an) { + case 0: return "No"; + case 1: return "8-byte align"; + case 2: return "4-byte align"; + case 3: return "Reserved"; + default: + if (an >= 4 && an <= 12) + snprintf(s_align_n, sizeof(s_align_n), "8-byte align" + " and up to 2^%ju-byte extended align", + (uintmax_t) an); + else + snprintf(s_align_n, sizeof(s_align_n), "Unknown (%ju)", + (uintmax_t) an); + return (s_align_n); + } +} + +static const char * +aeabi_align_preserved(uint64_t ap) +{ + static char s_align_p[128]; + + switch (ap) { + case 0: return "No"; + case 1: return "8-byte align"; + case 2: return "8-byte align and SP % 8 == 0"; + case 3: return "Reserved"; + default: + if (ap >= 4 && ap <= 12) + snprintf(s_align_p, sizeof(s_align_p), "8-byte align" + " and SP %% 8 == 0 and up to 2^%ju-byte extended" + " align", (uintmax_t) ap); + else + snprintf(s_align_p, sizeof(s_align_p), "Unknown (%ju)", + (uintmax_t) ap); + return (s_align_p); + } +} + +static const char * +aeabi_fp_rounding(uint64_t fr) +{ + static char s_fp_r[32]; + + switch (fr) { + case 0: return "Unused"; + case 1: return "Needed"; + default: + snprintf(s_fp_r, sizeof(s_fp_r), "Unknown (%ju)", + (uintmax_t) fr); + return (s_fp_r); + } +} + +static const char * +aeabi_fp_denormal(uint64_t fd) +{ + static char s_fp_d[32]; + + switch (fd) { + case 0: return "Unused"; + case 1: return "Needed"; + case 2: return "Sign Only"; + default: + snprintf(s_fp_d, sizeof(s_fp_d), "Unknown (%ju)", + (uintmax_t) fd); + return (s_fp_d); + } +} + +static const char * +aeabi_fp_exceptions(uint64_t fe) +{ + static char s_fp_e[32]; + + switch (fe) { + case 0: return "Unused"; + case 1: return "Needed"; + default: + snprintf(s_fp_e, sizeof(s_fp_e), "Unknown (%ju)", + (uintmax_t) fe); + return (s_fp_e); + } +} + +static const char * +aeabi_fp_user_exceptions(uint64_t fu) +{ + static char s_fp_u[32]; + + switch (fu) { + case 0: return "Unused"; + case 1: return "Needed"; + default: + snprintf(s_fp_u, sizeof(s_fp_u), "Unknown (%ju)", + (uintmax_t) fu); + return (s_fp_u); + } +} + +static const char * +aeabi_fp_number_model(uint64_t fn) +{ + static char s_fp_n[32]; + + switch (fn) { + case 0: return "Unused"; + case 1: return "IEEE 754 normal"; + case 2: return "RTABI"; + case 3: return "IEEE 754"; + default: + snprintf(s_fp_n, sizeof(s_fp_n), "Unknown (%ju)", + (uintmax_t) fn); + return (s_fp_n); + } +} + +static const char * +aeabi_fp_16bit_format(uint64_t fp16) +{ + static char s_fp_16[64]; + + switch (fp16) { + case 0: return "None"; + case 1: return "IEEE 754"; + case 2: return "VFPv3/Advanced SIMD (alternative format)"; + default: + snprintf(s_fp_16, sizeof(s_fp_16), "Unknown (%ju)", + (uintmax_t) fp16); + return (s_fp_16); + } +} + +static const char * +aeabi_mpext(uint64_t mp) +{ + static char s_mp[32]; + + switch (mp) { + case 0: return "Not allowed"; + case 1: return "Allowed"; + default: + snprintf(s_mp, sizeof(s_mp), "Unknown (%ju)", + (uintmax_t) mp); + return (s_mp); + } +} + +static const char * +aeabi_div(uint64_t du) +{ + static char s_du[32]; + + switch (du) { + case 0: return "Yes (V7-R/V7-M)"; + case 1: return "No"; + case 2: return "Yes (V7-A)"; + default: + snprintf(s_du, sizeof(s_du), "Unknown (%ju)", + (uintmax_t) du); + return (s_du); + } +} + +static const char * +aeabi_t2ee(uint64_t t2ee) +{ + static char s_t2ee[32]; + + switch (t2ee) { + case 0: return "Not allowed"; + case 1: return "Allowed"; + default: + snprintf(s_t2ee, sizeof(s_t2ee), "Unknown(%ju)", + (uintmax_t) t2ee); + return (s_t2ee); + } + +} + +static const char * +aeabi_hardfp(uint64_t hfp) +{ + static char s_hfp[32]; + + switch (hfp) { + case 0: return "Tag_FP_arch"; + case 1: return "only SP"; + case 2: return "only DP"; + case 3: return "both SP and DP"; + default: + snprintf(s_hfp, sizeof(s_hfp), "Unknown (%ju)", + (uintmax_t) hfp); + return (s_hfp); + } +} + +static const char * +aeabi_vfp_args(uint64_t va) +{ + static char s_va[32]; + + switch (va) { + case 0: return "AAPCS (base variant)"; + case 1: return "AAPCS (VFP variant)"; + case 2: return "toolchain-specific"; + default: + snprintf(s_va, sizeof(s_va), "Unknown (%ju)", (uintmax_t) va); + return (s_va); + } +} + +static const char * +aeabi_wmmx_args(uint64_t wa) +{ + static char s_wa[32]; + + switch (wa) { + case 0: return "AAPCS (base variant)"; + case 1: return "Intel WMMX"; + case 2: return "toolchain-specific"; + default: + snprintf(s_wa, sizeof(s_wa), "Unknown(%ju)", (uintmax_t) wa); + return (s_wa); + } +} + +static const char * +aeabi_unaligned_access(uint64_t ua) +{ + static char s_ua[32]; + + switch (ua) { + case 0: return "Not allowed"; + case 1: return "Allowed"; + default: + snprintf(s_ua, sizeof(s_ua), "Unknown(%ju)", (uintmax_t) ua); + return (s_ua); + } +} + +static const char * +aeabi_fp_hpext(uint64_t fh) +{ + static char s_fh[32]; + + switch (fh) { + case 0: return "Not allowed"; + case 1: return "Allowed"; + default: + snprintf(s_fh, sizeof(s_fh), "Unknown(%ju)", (uintmax_t) fh); + return (s_fh); + } +} + +static const char * +aeabi_optm_goal(uint64_t og) +{ + static char s_og[32]; + + switch (og) { + case 0: return "None"; + case 1: return "Speed"; + case 2: return "Speed aggressive"; + case 3: return "Space"; + case 4: return "Space aggressive"; + case 5: return "Debugging"; + case 6: return "Best Debugging"; + default: + snprintf(s_og, sizeof(s_og), "Unknown(%ju)", (uintmax_t) og); + return (s_og); + } +} + +static const char * +aeabi_fp_optm_goal(uint64_t fog) +{ + static char s_fog[32]; + + switch (fog) { + case 0: return "None"; + case 1: return "Speed"; + case 2: return "Speed aggressive"; + case 3: return "Space"; + case 4: return "Space aggressive"; + case 5: return "Accurary"; + case 6: return "Best Accurary"; + default: + snprintf(s_fog, sizeof(s_fog), "Unknown(%ju)", + (uintmax_t) fog); + return (s_fog); + } +} + +static const char * +aeabi_virtual(uint64_t vt) +{ + static char s_virtual[64]; + + switch (vt) { + case 0: return "No"; + case 1: return "TrustZone"; + case 2: return "Virtualization extension"; + case 3: return "TrustZone and virtualization extension"; + default: + snprintf(s_virtual, sizeof(s_virtual), "Unknown(%ju)", + (uintmax_t) vt); + return (s_virtual); + } +} + +static struct { + uint64_t tag; + const char *s_tag; + const char *(*get_desc)(uint64_t val); +} aeabi_tags[] = { + {4, "Tag_CPU_raw_name", NULL}, + {5, "Tag_CPU_name", NULL}, + {6, "Tag_CPU_arch", aeabi_cpu_arch}, + {7, "Tag_CPU_arch_profile", aeabi_cpu_arch_profile}, + {8, "Tag_ARM_ISA_use", aeabi_arm_isa}, + {9, "Tag_THUMB_ISA_use", aeabi_thumb_isa}, + {10, "Tag_FP_arch", aeabi_fp_arch}, + {11, "Tag_WMMX_arch", aeabi_wmmx_arch}, + {12, "Tag_Advanced_SIMD_arch", aeabi_adv_simd_arch}, + {13, "Tag_PCS_config", aeabi_pcs_config}, + {14, "Tag_ABI_PCS_R9_use", aeabi_pcs_r9}, + {15, "Tag_ABI_PCS_RW_data", aeabi_pcs_rw}, + {16, "Tag_ABI_PCS_RO_data", aeabi_pcs_ro}, + {17, "Tag_ABI_PCS_GOT_use", aeabi_pcs_got}, + {18, "Tag_ABI_PCS_wchar_t", aeabi_pcs_wchar_t}, + {19, "Tag_ABI_FP_rounding", aeabi_fp_rounding}, + {20, "Tag_ABI_FP_denormal", aeabi_fp_denormal}, + {21, "Tag_ABI_FP_exceptions", aeabi_fp_exceptions}, + {22, "Tag_ABI_FP_user_exceptions", aeabi_fp_user_exceptions}, + {23, "Tag_ABI_FP_number_model", aeabi_fp_number_model}, + {24, "Tag_ABI_align_needed", aeabi_align_needed}, + {25, "Tag_ABI_align_preserved", aeabi_align_preserved}, + {26, "Tag_ABI_enum_size", aeabi_enum_size}, + {27, "Tag_ABI_HardFP_use", aeabi_hardfp}, + {28, "Tag_ABI_VFP_args", aeabi_vfp_args}, + {29, "Tag_ABI_WMMX_args", aeabi_wmmx_args}, + {30, "Tag_ABI_optimization_goals", aeabi_optm_goal}, + {31, "Tag_ABI_FP_optimization_goals", aeabi_fp_optm_goal}, + {32, "Tag_compatibility", NULL}, + {34, "Tag_CPU_unaligned_access", aeabi_unaligned_access}, + {36, "Tag_FP_HP_extension", aeabi_fp_hpext}, + {38, "Tag_ABI_FP_16bit_format", aeabi_fp_16bit_format}, + {42, "Tag_MPextension_use", aeabi_mpext}, + {44, "Tag_DIV_use", aeabi_div}, + {64, "Tag_nodefaults", NULL}, + {65, "Tag_also_compatible_with", NULL}, + {66, "Tag_T2EE_use", aeabi_t2ee}, + {67, "Tag_conformance", NULL}, + {68, "Tag_Virtualization_use", aeabi_virtual}, + {70, "Tag_MPextension_use", aeabi_mpext}, +}; + +static const char * +mips_abi_fp(uint64_t fp) +{ + static char s_mips_abi_fp[64]; + + switch (fp) { + case 0: return "N/A"; + case 1: return "Hard float (double precision)"; + case 2: return "Hard float (single precision)"; + case 3: return "Soft float"; + case 4: return "64-bit float (-mips32r2 -mfp64)"; + default: + snprintf(s_mips_abi_fp, sizeof(s_mips_abi_fp), "Unknown(%ju)", + (uintmax_t) fp); + return (s_mips_abi_fp); + } +} + +static const char * +ppc_abi_fp(uint64_t fp) +{ + static char s_ppc_abi_fp[64]; + + switch (fp) { + case 0: return "N/A"; + case 1: return "Hard float (double precision)"; + case 2: return "Soft float"; + case 3: return "Hard float (single precision)"; + default: + snprintf(s_ppc_abi_fp, sizeof(s_ppc_abi_fp), "Unknown(%ju)", + (uintmax_t) fp); + return (s_ppc_abi_fp); + } +} + +static const char * +ppc_abi_vector(uint64_t vec) +{ + static char s_vec[64]; + + switch (vec) { + case 0: return "N/A"; + case 1: return "Generic purpose registers"; + case 2: return "AltiVec registers"; + case 3: return "SPE registers"; + default: + snprintf(s_vec, sizeof(s_vec), "Unknown(%ju)", (uintmax_t) vec); + return (s_vec); + } +} + +static const char * +dwarf_reg(unsigned int mach, unsigned int reg) +{ + + switch (mach) { + case EM_386: + switch (reg) { + case 0: return "eax"; + case 1: return "ecx"; + case 2: return "edx"; + case 3: return "ebx"; + case 4: return "esp"; + case 5: return "ebp"; + case 6: return "esi"; + case 7: return "edi"; + case 8: return "eip"; + case 9: return "eflags"; + case 11: return "st0"; + case 12: return "st1"; + case 13: return "st2"; + case 14: return "st3"; + case 15: return "st4"; + case 16: return "st5"; + case 17: return "st6"; + case 18: return "st7"; + case 21: return "xmm0"; + case 22: return "xmm1"; + case 23: return "xmm2"; + case 24: return "xmm3"; + case 25: return "xmm4"; + case 26: return "xmm5"; + case 27: return "xmm6"; + case 28: return "xmm7"; + case 29: return "mm0"; + case 30: return "mm1"; + case 31: return "mm2"; + case 32: return "mm3"; + case 33: return "mm4"; + case 34: return "mm5"; + case 35: return "mm6"; + case 36: return "mm7"; + case 37: return "fcw"; + case 38: return "fsw"; + case 39: return "mxcsr"; + case 40: return "es"; + case 41: return "cs"; + case 42: return "ss"; + case 43: return "ds"; + case 44: return "fs"; + case 45: return "gs"; + case 48: return "tr"; + case 49: return "ldtr"; + default: return (NULL); + } + case EM_X86_64: + switch (reg) { + case 0: return "rax"; + case 1: return "rdx"; + case 2: return "rcx"; + case 3: return "rbx"; + case 4: return "rsi"; + case 5: return "rdi"; + case 6: return "rbp"; + case 7: return "rsp"; + case 16: return "rip"; + case 17: return "xmm0"; + case 18: return "xmm1"; + case 19: return "xmm2"; + case 20: return "xmm3"; + case 21: return "xmm4"; + case 22: return "xmm5"; + case 23: return "xmm6"; + case 24: return "xmm7"; + case 25: return "xmm8"; + case 26: return "xmm9"; + case 27: return "xmm10"; + case 28: return "xmm11"; + case 29: return "xmm12"; + case 30: return "xmm13"; + case 31: return "xmm14"; + case 32: return "xmm15"; + case 33: return "st0"; + case 34: return "st1"; + case 35: return "st2"; + case 36: return "st3"; + case 37: return "st4"; + case 38: return "st5"; + case 39: return "st6"; + case 40: return "st7"; + case 41: return "mm0"; + case 42: return "mm1"; + case 43: return "mm2"; + case 44: return "mm3"; + case 45: return "mm4"; + case 46: return "mm5"; + case 47: return "mm6"; + case 48: return "mm7"; + case 49: return "rflags"; + case 50: return "es"; + case 51: return "cs"; + case 52: return "ss"; + case 53: return "ds"; + case 54: return "fs"; + case 55: return "gs"; + case 58: return "fs.base"; + case 59: return "gs.base"; + case 62: return "tr"; + case 63: return "ldtr"; + case 64: return "mxcsr"; + case 65: return "fcw"; + case 66: return "fsw"; + default: return (NULL); + } + default: + return (NULL); + } +} + +static void +dump_ehdr(struct readelf *re) +{ + size_t shnum, shstrndx; + int i; + + printf("ELF Header:\n"); + + /* e_ident[]. */ + printf(" Magic: "); + for (i = 0; i < EI_NIDENT; i++) + printf("%.2x ", re->ehdr.e_ident[i]); + putchar('\n'); + + /* EI_CLASS. */ + printf("%-37s%s\n", " Class:", elf_class(re->ehdr.e_ident[EI_CLASS])); + + /* EI_DATA. */ + printf("%-37s%s\n", " Data:", elf_endian(re->ehdr.e_ident[EI_DATA])); + + /* EI_VERSION. */ + printf("%-37s%d %s\n", " Version:", re->ehdr.e_ident[EI_VERSION], + elf_ver(re->ehdr.e_ident[EI_VERSION])); + + /* EI_OSABI. */ + printf("%-37s%s\n", " OS/ABI:", elf_osabi(re->ehdr.e_ident[EI_OSABI])); + + /* EI_ABIVERSION. */ + printf("%-37s%d\n", " ABI Version:", re->ehdr.e_ident[EI_ABIVERSION]); + + /* e_type. */ + printf("%-37s%s\n", " Type:", elf_type(re->ehdr.e_type)); + + /* e_machine. */ + printf("%-37s%s\n", " Machine:", elf_machine(re->ehdr.e_machine)); + + /* e_version. */ + printf("%-37s%#x\n", " Version:", re->ehdr.e_version); + + /* e_entry. */ + printf("%-37s%#jx\n", " Entry point address:", + (uintmax_t)re->ehdr.e_entry); + + /* e_phoff. */ + printf("%-37s%ju (bytes into file)\n", " Start of program headers:", + (uintmax_t)re->ehdr.e_phoff); + + /* e_shoff. */ + printf("%-37s%ju (bytes into file)\n", " Start of section headers:", + (uintmax_t)re->ehdr.e_shoff); + + /* e_flags. */ + printf("%-37s%#x", " Flags:", re->ehdr.e_flags); + dump_eflags(re, re->ehdr.e_flags); + putchar('\n'); + + /* e_ehsize. */ + printf("%-37s%u (bytes)\n", " Size of this header:", + re->ehdr.e_ehsize); + + /* e_phentsize. */ + printf("%-37s%u (bytes)\n", " Size of program headers:", + re->ehdr.e_phentsize); + + /* e_phnum. */ + printf("%-37s%u\n", " Number of program headers:", re->ehdr.e_phnum); + + /* e_shentsize. */ + printf("%-37s%u (bytes)\n", " Size of section headers:", + re->ehdr.e_shentsize); + + /* e_shnum. */ + printf("%-37s%u", " Number of section headers:", re->ehdr.e_shnum); + if (re->ehdr.e_shnum == SHN_UNDEF) { + /* Extended section numbering is in use. */ + if (elf_getshnum(re->elf, &shnum)) + printf(" (%ju)", (uintmax_t)shnum); + } + putchar('\n'); + + /* e_shstrndx. */ + printf("%-37s%u", " Section header string table index:", + re->ehdr.e_shstrndx); + if (re->ehdr.e_shstrndx == SHN_XINDEX) { + /* Extended section numbering is in use. */ + if (elf_getshstrndx(re->elf, &shstrndx)) + printf(" (%ju)", (uintmax_t)shstrndx); + } + putchar('\n'); +} + +static void +dump_eflags(struct readelf *re, uint64_t e_flags) +{ + struct eflags_desc *edesc; + int arm_eabi; + + edesc = NULL; + switch (re->ehdr.e_machine) { + case EM_ARM: + arm_eabi = (e_flags & EF_ARM_EABIMASK) >> 24; + if (arm_eabi == 0) + printf(", GNU EABI"); + else if (arm_eabi <= 5) + printf(", Version%d EABI", arm_eabi); + edesc = arm_eflags_desc; + break; + case EM_MIPS: + case EM_MIPS_RS3_LE: + switch ((e_flags & EF_MIPS_ARCH) >> 28) { + case 0: printf(", mips1"); break; + case 1: printf(", mips2"); break; + case 2: printf(", mips3"); break; + case 3: printf(", mips4"); break; + case 4: printf(", mips5"); break; + case 5: printf(", mips32"); break; + case 6: printf(", mips64"); break; + case 7: printf(", mips32r2"); break; + case 8: printf(", mips64r2"); break; + default: break; + } + switch ((e_flags & 0x00FF0000) >> 16) { + case 0x81: printf(", 3900"); break; + case 0x82: printf(", 4010"); break; + case 0x83: printf(", 4100"); break; + case 0x85: printf(", 4650"); break; + case 0x87: printf(", 4120"); break; + case 0x88: printf(", 4111"); break; + case 0x8a: printf(", sb1"); break; + case 0x8b: printf(", octeon"); break; + case 0x8c: printf(", xlr"); break; + case 0x91: printf(", 5400"); break; + case 0x98: printf(", 5500"); break; + case 0x99: printf(", 9000"); break; + case 0xa0: printf(", loongson-2e"); break; + case 0xa1: printf(", loongson-2f"); break; + default: break; + } + switch ((e_flags & 0x0000F000) >> 12) { + case 1: printf(", o32"); break; + case 2: printf(", o64"); break; + case 3: printf(", eabi32"); break; + case 4: printf(", eabi64"); break; + default: break; + } + edesc = mips_eflags_desc; + break; + case EM_PPC: + case EM_PPC64: + edesc = powerpc_eflags_desc; + break; + case EM_SPARC: + case EM_SPARC32PLUS: + case EM_SPARCV9: + switch ((e_flags & EF_SPARCV9_MM)) { + case EF_SPARCV9_TSO: printf(", tso"); break; + case EF_SPARCV9_PSO: printf(", pso"); break; + case EF_SPARCV9_MM: printf(", rmo"); break; + default: break; + } + edesc = sparc_eflags_desc; + break; + default: + break; + } + + if (edesc != NULL) { + while (edesc->desc != NULL) { + if (e_flags & edesc->flag) + printf(", %s", edesc->desc); + edesc++; + } + } +} + +static void +dump_phdr(struct readelf *re) +{ + const char *rawfile; + GElf_Phdr phdr; + size_t phnum, size; + int i, j; + +#define PH_HDR "Type", "Offset", "VirtAddr", "PhysAddr", "FileSiz", \ + "MemSiz", "Flg", "Align" +#define PH_CT phdr_type(phdr.p_type), (uintmax_t)phdr.p_offset, \ + (uintmax_t)phdr.p_vaddr, (uintmax_t)phdr.p_paddr, \ + (uintmax_t)phdr.p_filesz, (uintmax_t)phdr.p_memsz, \ + phdr.p_flags & PF_R ? 'R' : ' ', \ + phdr.p_flags & PF_W ? 'W' : ' ', \ + phdr.p_flags & PF_X ? 'E' : ' ', \ + (uintmax_t)phdr.p_align + + if (elf_getphnum(re->elf, &phnum) == 0) { + warnx("elf_getphnum failed: %s", elf_errmsg(-1)); + return; + } + if (phnum == 0) { + printf("\nThere are no program headers in this file.\n"); + return; + } + + printf("\nElf file type is %s", elf_type(re->ehdr.e_type)); + printf("\nEntry point 0x%jx\n", (uintmax_t)re->ehdr.e_entry); + printf("There are %ju program headers, starting at offset %ju\n", + (uintmax_t)phnum, (uintmax_t)re->ehdr.e_phoff); + + /* Dump program headers. */ + printf("\nProgram Headers:\n"); + if (re->ec == ELFCLASS32) + printf(" %-15s%-9s%-11s%-11s%-8s%-8s%-4s%s\n", PH_HDR); + else if (re->options & RE_WW) + printf(" %-15s%-9s%-19s%-19s%-9s%-9s%-4s%s\n", PH_HDR); + else + printf(" %-15s%-19s%-19s%s\n %-19s%-20s" + "%-7s%s\n", PH_HDR); + for (i = 0; (size_t) i < phnum; i++) { + if (gelf_getphdr(re->elf, i, &phdr) != &phdr) { + warnx("gelf_getphdr failed: %s", elf_errmsg(-1)); + continue; + } + /* TODO: Add arch-specific segment type dump. */ + if (re->ec == ELFCLASS32) + printf(" %-14.14s 0x%6.6jx 0x%8.8jx 0x%8.8jx " + "0x%5.5jx 0x%5.5jx %c%c%c %#jx\n", PH_CT); + else if (re->options & RE_WW) + printf(" %-14.14s 0x%6.6jx 0x%16.16jx 0x%16.16jx " + "0x%6.6jx 0x%6.6jx %c%c%c %#jx\n", PH_CT); + else + printf(" %-14.14s 0x%16.16jx 0x%16.16jx 0x%16.16jx\n" + " 0x%16.16jx 0x%16.16jx %c%c%c" + " %#jx\n", PH_CT); + if (phdr.p_type == PT_INTERP) { + if ((rawfile = elf_rawfile(re->elf, &size)) == NULL) { + warnx("elf_rawfile failed: %s", elf_errmsg(-1)); + continue; + } + if (phdr.p_offset >= size) { + warnx("invalid program header offset"); + continue; + } + printf(" [Requesting program interpreter: %s]\n", + rawfile + phdr.p_offset); + } + } + + /* Dump section to segment mapping. */ + if (re->shnum == 0) + return; + printf("\n Section to Segment mapping:\n"); + printf(" Segment Sections...\n"); + for (i = 0; (size_t)i < phnum; i++) { + if (gelf_getphdr(re->elf, i, &phdr) != &phdr) { + warnx("gelf_getphdr failed: %s", elf_errmsg(-1)); + continue; + } + 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) + printf("%s ", re->sl[j].name); + printf("\n"); + } +#undef PH_HDR +#undef PH_CT +} + +static char * +section_flags(struct readelf *re, struct section *s) +{ +#define BUF_SZ 256 + static char buf[BUF_SZ]; + int i, p, nb; + + p = 0; + nb = re->ec == ELFCLASS32 ? 8 : 16; + if (re->options & RE_T) { + snprintf(buf, BUF_SZ, "[%*.*jx]: ", nb, nb, + (uintmax_t)s->flags); + p += nb + 4; + } + for (i = 0; section_flag[i].ln != NULL; i++) { + if ((s->flags & section_flag[i].value) == 0) + continue; + if (re->options & RE_T) { + snprintf(&buf[p], BUF_SZ - p, "%s, ", + section_flag[i].ln); + p += strlen(section_flag[i].ln) + 2; + } else + buf[p++] = section_flag[i].sn; + } + if (re->options & RE_T && p > nb + 4) + p -= 2; + buf[p] = '\0'; + + return (buf); +} + +static void +dump_shdr(struct readelf *re) +{ + struct section *s; + int i; + +#define S_HDR "[Nr] Name", "Type", "Addr", "Off", "Size", "ES", \ + "Flg", "Lk", "Inf", "Al" +#define S_HDRL "[Nr] Name", "Type", "Address", "Offset", "Size", \ + "EntSize", "Flags", "Link", "Info", "Align" +#define ST_HDR "[Nr] Name", "Type", "Addr", "Off", "Size", "ES", \ + "Lk", "Inf", "Al", "Flags" +#define ST_HDRL "[Nr] Name", "Type", "Address", "Offset", "Link", \ + "Size", "EntSize", "Info", "Align", "Flags" +#define S_CT i, s->name, section_type(re->ehdr.e_machine, s->type), \ + (uintmax_t)s->addr, (uintmax_t)s->off, (uintmax_t)s->sz,\ + (uintmax_t)s->entsize, section_flags(re, s), \ + s->link, s->info, (uintmax_t)s->align +#define ST_CT i, s->name, section_type(re->ehdr.e_machine, s->type), \ + (uintmax_t)s->addr, (uintmax_t)s->off, (uintmax_t)s->sz,\ + (uintmax_t)s->entsize, s->link, s->info, \ + (uintmax_t)s->align, section_flags(re, s) +#define ST_CTL i, s->name, section_type(re->ehdr.e_machine, s->type), \ + (uintmax_t)s->addr, (uintmax_t)s->off, s->link, \ + (uintmax_t)s->sz, (uintmax_t)s->entsize, s->info, \ + (uintmax_t)s->align, section_flags(re, s) + + if (re->shnum == 0) { + printf("\nThere are no sections in this file.\n"); + return; + } + printf("There are %ju section headers, starting at offset 0x%jx:\n", + (uintmax_t)re->shnum, (uintmax_t)re->ehdr.e_shoff); + printf("\nSection Headers:\n"); + if (re->ec == ELFCLASS32) { + if (re->options & RE_T) + printf(" %s\n %-16s%-9s%-7s%-7s%-5s%-3s%-4s%s\n" + "%12s\n", ST_HDR); + else + printf(" %-23s%-16s%-9s%-7s%-7s%-3s%-4s%-3s%-4s%s\n", + S_HDR); + } else if (re->options & RE_WW) { + if (re->options & RE_T) + printf(" %s\n %-16s%-17s%-7s%-7s%-5s%-3s%-4s%s\n" + "%12s\n", ST_HDR); + else + printf(" %-23s%-16s%-17s%-7s%-7s%-3s%-4s%-3s%-4s%s\n", + S_HDR); + } else { + if (re->options & RE_T) + printf(" %s\n %-18s%-17s%-18s%s\n %-18s" + "%-17s%-18s%s\n%12s\n", ST_HDRL); + else + printf(" %-23s%-17s%-18s%s\n %-18s%-17s%-7s%" + "-6s%-6s%s\n", S_HDRL); + } + for (i = 0; (size_t)i < re->shnum; i++) { + s = &re->sl[i]; + if (re->ec == ELFCLASS32) { + if (re->options & RE_T) + printf(" [%2d] %s\n %-15.15s %8.8jx" + " %6.6jx %6.6jx %2.2jx %2u %3u %2ju\n" + " %s\n", ST_CT); + else + printf(" [%2d] %-17.17s %-15.15s %8.8jx" + " %6.6jx %6.6jx %2.2jx %3s %2u %3u %2ju\n", + S_CT); + } else if (re->options & RE_WW) { + if (re->options & RE_T) + printf(" [%2d] %s\n %-15.15s %16.16jx" + " %6.6jx %6.6jx %2.2jx %2u %3u %2ju\n" + " %s\n", ST_CT); + else + printf(" [%2d] %-17.17s %-15.15s %16.16jx" + " %6.6jx %6.6jx %2.2jx %3s %2u %3u %2ju\n", + S_CT); + } else { + if (re->options & RE_T) + printf(" [%2d] %s\n %-15.15s %16.16jx" + " %16.16jx %u\n %16.16jx %16.16jx" + " %-16u %ju\n %s\n", ST_CTL); + else + printf(" [%2d] %-17.17s %-15.15s %16.16jx" + " %8.8jx\n %16.16jx %16.16jx " + "%3s %2u %3u %ju\n", S_CT); + } + } + if ((re->options & RE_T) == 0) + printf("Key to Flags:\n W (write), A (alloc)," + " X (execute), M (merge), S (strings)\n" + " I (info), L (link order), G (group), x (unknown)\n" + " O (extra OS processing required)" + " o (OS specific), p (processor specific)\n"); + +#undef S_HDR +#undef S_HDRL +#undef ST_HDR +#undef ST_HDRL +#undef S_CT +#undef ST_CT +#undef ST_CTL +} + +static void +dump_dynamic(struct readelf *re) +{ + GElf_Dyn dyn; + Elf_Data *d; + struct section *s; + int elferr, i, is_dynamic, j, jmax, nentries; + + is_dynamic = 0; + + for (i = 0; (size_t)i < re->shnum; i++) { + s = &re->sl[i]; + if (s->type != SHT_DYNAMIC) + continue; + (void) elf_errno(); + if ((d = elf_getdata(s->scn, NULL)) == NULL) { + elferr = elf_errno(); + if (elferr != 0) + warnx("elf_getdata failed: %s", elf_errmsg(-1)); + continue; + } + if (d->d_size <= 0) + continue; + + is_dynamic = 1; + + /* Determine the actual number of table entries. */ + nentries = 0; + jmax = (int) (s->sz / s->entsize); + + for (j = 0; j < jmax; j++) { + if (gelf_getdyn(d, j, &dyn) != &dyn) { + warnx("gelf_getdyn failed: %s", + elf_errmsg(-1)); + continue; + } + nentries ++; + if (dyn.d_tag == DT_NULL) + break; + } + + printf("\nDynamic section at offset 0x%jx", (uintmax_t)s->off); + printf(" contains %u entries:\n", nentries); + + if (re->ec == ELFCLASS32) + printf("%5s%12s%28s\n", "Tag", "Type", "Name/Value"); + else + printf("%5s%20s%28s\n", "Tag", "Type", "Name/Value"); + + for (j = 0; j < nentries; j++) { + if (gelf_getdyn(d, j, &dyn) != &dyn) + continue; + /* Dump dynamic entry type. */ + if (re->ec == ELFCLASS32) + printf(" 0x%8.8jx", (uintmax_t)dyn.d_tag); + else + printf(" 0x%16.16jx", (uintmax_t)dyn.d_tag); + printf(" %-20s", dt_type(re->ehdr.e_machine, + dyn.d_tag)); + /* Dump dynamic entry value. */ + dump_dyn_val(re, &dyn, s->link); + } + } + + if (!is_dynamic) + printf("\nThere is no dynamic section in this file.\n"); +} + +static char * +timestamp(time_t ti) +{ + static char ts[32]; + struct tm *t; + + t = gmtime(&ti); + snprintf(ts, sizeof(ts), "%04d-%02d-%02dT%02d:%02d:%02d", + t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour, + t->tm_min, t->tm_sec); + + return (ts); +} + +static const char * +dyn_str(struct readelf *re, uint32_t stab, uint64_t d_val) +{ + const char *name; + + if (stab == SHN_UNDEF) + name = "ERROR"; + else if ((name = elf_strptr(re->elf, stab, d_val)) == NULL) { + (void) elf_errno(); /* clear error */ + name = "ERROR"; + } + + return (name); +} + +static void +dump_arch_dyn_val(struct readelf *re, GElf_Dyn *dyn, uint32_t stab) +{ + const char *name; + + switch (re->ehdr.e_machine) { + case EM_MIPS: + case EM_MIPS_RS3_LE: + switch (dyn->d_tag) { + case DT_MIPS_RLD_VERSION: + case DT_MIPS_LOCAL_GOTNO: + case DT_MIPS_CONFLICTNO: + case DT_MIPS_LIBLISTNO: + case DT_MIPS_SYMTABNO: + case DT_MIPS_UNREFEXTNO: + case DT_MIPS_GOTSYM: + case DT_MIPS_HIPAGENO: + case DT_MIPS_DELTA_CLASS_NO: + case DT_MIPS_DELTA_INSTANCE_NO: + case DT_MIPS_DELTA_RELOC_NO: + case DT_MIPS_DELTA_SYM_NO: + case DT_MIPS_DELTA_CLASSSYM_NO: + case DT_MIPS_LOCALPAGE_GOTIDX: + case DT_MIPS_LOCAL_GOTIDX: + case DT_MIPS_HIDDEN_GOTIDX: + case DT_MIPS_PROTECTED_GOTIDX: + printf(" %ju\n", (uintmax_t) dyn->d_un.d_val); + break; + case DT_MIPS_ICHECKSUM: + case DT_MIPS_FLAGS: + case DT_MIPS_BASE_ADDRESS: + case DT_MIPS_CONFLICT: + case DT_MIPS_LIBLIST: + case DT_MIPS_RLD_MAP: + case DT_MIPS_DELTA_CLASS: + case DT_MIPS_DELTA_INSTANCE: + case DT_MIPS_DELTA_RELOC: + case DT_MIPS_DELTA_SYM: + case DT_MIPS_DELTA_CLASSSYM: + case DT_MIPS_CXX_FLAGS: + case DT_MIPS_PIXIE_INIT: + case DT_MIPS_SYMBOL_LIB: + case DT_MIPS_OPTIONS: + case DT_MIPS_INTERFACE: + case DT_MIPS_DYNSTR_ALIGN: + case DT_MIPS_INTERFACE_SIZE: + case DT_MIPS_RLD_TEXT_RESOLVE_ADDR: + case DT_MIPS_COMPACT_SIZE: + case DT_MIPS_GP_VALUE: + case DT_MIPS_AUX_DYNAMIC: + case DT_MIPS_PLTGOT: + case DT_MIPS_RLD_OBJ_UPDATE: + case DT_MIPS_RWPLT: + printf(" 0x%jx\n", (uintmax_t) dyn->d_un.d_val); + break; + case DT_MIPS_IVERSION: + case DT_MIPS_PERF_SUFFIX: + case DT_AUXILIARY: + case DT_FILTER: + name = dyn_str(re, stab, dyn->d_un.d_val); + printf(" %s\n", name); + break; + case DT_MIPS_TIME_STAMP: + printf(" %s\n", timestamp(dyn->d_un.d_val)); + break; + } + break; + default: + printf("\n"); + break; + } +} + +static void +dump_dyn_val(struct readelf *re, GElf_Dyn *dyn, uint32_t stab) +{ + const char *name; + + if (dyn->d_tag >= DT_LOPROC && dyn->d_tag <= DT_HIPROC) { + dump_arch_dyn_val(re, dyn, stab); + return; + } + + /* These entry values are index into the string table. */ + name = NULL; + if (dyn->d_tag == DT_NEEDED || dyn->d_tag == DT_SONAME || + dyn->d_tag == DT_RPATH || dyn->d_tag == DT_RUNPATH) + name = dyn_str(re, stab, dyn->d_un.d_val); + + switch(dyn->d_tag) { + case DT_NULL: + case DT_PLTGOT: + case DT_HASH: + case DT_STRTAB: + case DT_SYMTAB: + case DT_RELA: + case DT_INIT: + case DT_SYMBOLIC: + case DT_REL: + case DT_DEBUG: + case DT_TEXTREL: + case DT_JMPREL: + case DT_FINI: + case DT_VERDEF: + case DT_VERNEED: + case DT_VERSYM: + case DT_GNU_HASH: + case DT_GNU_LIBLIST: + case DT_GNU_CONFLICT: + printf(" 0x%jx\n", (uintmax_t) dyn->d_un.d_val); + break; + case DT_PLTRELSZ: + case DT_RELASZ: + case DT_RELAENT: + case DT_STRSZ: + case DT_SYMENT: + case DT_RELSZ: + case DT_RELENT: + case DT_INIT_ARRAYSZ: + case DT_FINI_ARRAYSZ: + case DT_GNU_CONFLICTSZ: + case DT_GNU_LIBLISTSZ: + printf(" %ju (bytes)\n", (uintmax_t) dyn->d_un.d_val); + break; + case DT_RELACOUNT: + case DT_RELCOUNT: + case DT_VERDEFNUM: + case DT_VERNEEDNUM: + printf(" %ju\n", (uintmax_t) dyn->d_un.d_val); + break; + case DT_NEEDED: + printf(" Shared library: [%s]\n", name); + break; + case DT_SONAME: + printf(" Library soname: [%s]\n", name); + break; + case DT_RPATH: + printf(" Library rpath: [%s]\n", name); + break; + case DT_RUNPATH: + printf(" Library runpath: [%s]\n", name); + break; + case DT_PLTREL: + printf(" %s\n", dt_type(re->ehdr.e_machine, dyn->d_un.d_val)); + break; + case DT_GNU_PRELINKED: + printf(" %s\n", timestamp(dyn->d_un.d_val)); + break; + default: + printf("\n"); + } +} + +static void +dump_rel(struct readelf *re, struct section *s, Elf_Data *d) +{ + GElf_Rel r; + const char *symname; + uint64_t symval; + int i, len; + +#define REL_HDR "r_offset", "r_info", "r_type", "st_value", "st_name" +#define REL_CT32 (uintmax_t)r.r_offset, (uintmax_t)r.r_info, \ + r_type(re->ehdr.e_machine, ELF32_R_TYPE(r.r_info)), \ + (uintmax_t)symval, symname +#define REL_CT64 (uintmax_t)r.r_offset, (uintmax_t)r.r_info, \ + r_type(re->ehdr.e_machine, ELF64_R_TYPE(r.r_info)), \ + (uintmax_t)symval, symname + + printf("\nRelocation section (%s):\n", s->name); + if (re->ec == ELFCLASS32) + printf("%-8s %-8s %-19s %-8s %s\n", REL_HDR); + else { + if (re->options & RE_WW) + printf("%-16s %-16s %-24s %-16s %s\n", REL_HDR); + else + printf("%-12s %-12s %-19s %-16s %s\n", REL_HDR); + } + len = d->d_size / s->entsize; + for (i = 0; i < len; i++) { + if (gelf_getrel(d, i, &r) != &r) { + warnx("gelf_getrel failed: %s", elf_errmsg(-1)); + continue; + } + if (s->link >= re->shnum) { + warnx("invalid section link index %u", s->link); + continue; + } + symname = get_symbol_name(re, s->link, GELF_R_SYM(r.r_info)); + symval = get_symbol_value(re, s->link, GELF_R_SYM(r.r_info)); + if (re->ec == ELFCLASS32) { + r.r_info = ELF32_R_INFO(ELF64_R_SYM(r.r_info), + ELF64_R_TYPE(r.r_info)); + printf("%8.8jx %8.8jx %-19.19s %8.8jx %s\n", REL_CT32); + } else { + if (re->options & RE_WW) + printf("%16.16jx %16.16jx %-24.24s" + " %16.16jx %s\n", REL_CT64); + else + printf("%12.12jx %12.12jx %-19.19s" + " %16.16jx %s\n", REL_CT64); + } + } + +#undef REL_HDR +#undef REL_CT +} + +static void +dump_rela(struct readelf *re, struct section *s, Elf_Data *d) +{ + GElf_Rela r; + const char *symname; + uint64_t symval; + int i, len; + +#define RELA_HDR "r_offset", "r_info", "r_type", "st_value", \ + "st_name + r_addend" +#define RELA_CT32 (uintmax_t)r.r_offset, (uintmax_t)r.r_info, \ + r_type(re->ehdr.e_machine, ELF32_R_TYPE(r.r_info)), \ + (uintmax_t)symval, symname +#define RELA_CT64 (uintmax_t)r.r_offset, (uintmax_t)r.r_info, \ + r_type(re->ehdr.e_machine, ELF64_R_TYPE(r.r_info)), \ + (uintmax_t)symval, symname + + printf("\nRelocation section with addend (%s):\n", s->name); + if (re->ec == ELFCLASS32) + printf("%-8s %-8s %-19s %-8s %s\n", RELA_HDR); + else { + if (re->options & RE_WW) + printf("%-16s %-16s %-24s %-16s %s\n", RELA_HDR); + else + printf("%-12s %-12s %-19s %-16s %s\n", RELA_HDR); + } + len = d->d_size / s->entsize; + for (i = 0; i < len; i++) { + if (gelf_getrela(d, i, &r) != &r) { + warnx("gelf_getrel failed: %s", elf_errmsg(-1)); + continue; + } + if (s->link >= re->shnum) { + warnx("invalid section link index %u", s->link); + continue; + } + symname = get_symbol_name(re, s->link, GELF_R_SYM(r.r_info)); + symval = get_symbol_value(re, s->link, GELF_R_SYM(r.r_info)); + if (re->ec == ELFCLASS32) { + r.r_info = ELF32_R_INFO(ELF64_R_SYM(r.r_info), + ELF64_R_TYPE(r.r_info)); + printf("%8.8jx %8.8jx %-19.19s %8.8jx %s", RELA_CT32); + printf(" + %x\n", (uint32_t) r.r_addend); + } else { + if (re->options & RE_WW) + printf("%16.16jx %16.16jx %-24.24s" + " %16.16jx %s", RELA_CT64); + else + printf("%12.12jx %12.12jx %-19.19s" + " %16.16jx %s", RELA_CT64); + printf(" + %jx\n", (uintmax_t) r.r_addend); + } + } + +#undef RELA_HDR +#undef RELA_CT +} + +static void +dump_reloc(struct readelf *re) +{ + struct section *s; + Elf_Data *d; + int i, elferr; + + for (i = 0; (size_t)i < re->shnum; i++) { + s = &re->sl[i]; + if (s->type == SHT_REL || s->type == SHT_RELA) { + (void) elf_errno(); + if ((d = elf_getdata(s->scn, NULL)) == NULL) { + elferr = elf_errno(); + if (elferr != 0) + warnx("elf_getdata failed: %s", + elf_errmsg(elferr)); + continue; + } + if (s->type == SHT_REL) + dump_rel(re, s, d); + else + dump_rela(re, s, d); + } + } +} + +static void +dump_symtab(struct readelf *re, int i) +{ + struct section *s; + Elf_Data *d; + GElf_Sym sym; + const char *name; + int elferr, stab, j; + + s = &re->sl[i]; + stab = s->link; + (void) elf_errno(); + if ((d = elf_getdata(s->scn, NULL)) == NULL) { + elferr = elf_errno(); + if (elferr != 0) + warnx("elf_getdata failed: %s", elf_errmsg(elferr)); + return; + } + if (d->d_size <= 0) + return; + printf("Symbol table (%s)", s->name); + printf(" contains %ju entries:\n", s->sz / s->entsize); + printf("%7s%9s%14s%5s%8s%6s%9s%5s\n", "Num:", "Value", "Size", "Type", + "Bind", "Vis", "Ndx", "Name"); + + for (j = 0; (uint64_t)j < s->sz / s->entsize; j++) { + if (gelf_getsym(d, j, &sym) != &sym) { + warnx("gelf_getsym failed: %s", elf_errmsg(-1)); + continue; + } + printf("%6d:", j); + printf(" %16.16jx", (uintmax_t)sym.st_value); + printf(" %5ju", sym.st_size); + printf(" %-7s", st_type(GELF_ST_TYPE(sym.st_info))); + printf(" %-6s", st_bind(GELF_ST_BIND(sym.st_info))); + printf(" %-8s", st_vis(GELF_ST_VISIBILITY(sym.st_other))); + printf(" %3s", st_shndx(sym.st_shndx)); + if ((name = elf_strptr(re->elf, stab, sym.st_name)) != NULL) + printf(" %s", name); + /* Append symbol version string for SHT_DYNSYM symbol table. */ + if (s->type == SHT_DYNSYM && re->ver != NULL && + re->vs != NULL && re->vs[j] > 1) { + if (re->vs[j] & 0x8000 || + re->ver[re->vs[j] & 0x7fff].type == 0) + printf("@%s (%d)", + re->ver[re->vs[j] & 0x7fff].name, + re->vs[j] & 0x7fff); + else + printf("@@%s (%d)", re->ver[re->vs[j]].name, + re->vs[j]); + } + putchar('\n'); + } + +} + +static void +dump_symtabs(struct readelf *re) +{ + GElf_Dyn dyn; + Elf_Data *d; + struct section *s; + uint64_t dyn_off; + int elferr, i; + + /* + * If -D is specified, only dump the symbol table specified by + * the DT_SYMTAB entry in the .dynamic section. + */ + dyn_off = 0; + if (re->options & RE_DD) { + s = NULL; + for (i = 0; (size_t)i < re->shnum; i++) + if (re->sl[i].type == SHT_DYNAMIC) { + s = &re->sl[i]; + break; + } + if (s == NULL) + return; + (void) elf_errno(); + if ((d = elf_getdata(s->scn, NULL)) == NULL) { + elferr = elf_errno(); + if (elferr != 0) + warnx("elf_getdata failed: %s", elf_errmsg(-1)); + return; + } + if (d->d_size <= 0) + return; + + for (i = 0; (uint64_t)i < s->sz / s->entsize; i++) { + if (gelf_getdyn(d, i, &dyn) != &dyn) { + warnx("gelf_getdyn failed: %s", elf_errmsg(-1)); + continue; + } + if (dyn.d_tag == DT_SYMTAB) { + dyn_off = dyn.d_un.d_val; + break; + } + } + } + + /* Find and dump symbol tables. */ + for (i = 0; (size_t)i < re->shnum; i++) { + s = &re->sl[i]; + if (s->type == SHT_SYMTAB || s->type == SHT_DYNSYM) { + if (re->options & RE_DD) { + if (dyn_off == s->addr) { + dump_symtab(re, i); + break; + } + } else + dump_symtab(re, i); + } + } +} + +static void +dump_svr4_hash(struct section *s) +{ + Elf_Data *d; + uint32_t *buf; + uint32_t nbucket, nchain; + uint32_t *bucket, *chain; + uint32_t *bl, *c, maxl, total; + int elferr, i, j; + + /* Read and parse the content of .hash section. */ + (void) elf_errno(); + if ((d = elf_getdata(s->scn, NULL)) == NULL) { + elferr = elf_errno(); + if (elferr != 0) + warnx("elf_getdata failed: %s", elf_errmsg(elferr)); + return; + } + if (d->d_size < 2 * sizeof(uint32_t)) { + warnx(".hash section too small"); + return; + } + buf = d->d_buf; + nbucket = buf[0]; + nchain = buf[1]; + if (nbucket <= 0 || nchain <= 0) { + warnx("Malformed .hash section"); + return; + } + if (d->d_size != (nbucket + nchain + 2) * sizeof(uint32_t)) { + warnx("Malformed .hash section"); + return; + } + bucket = &buf[2]; + chain = &buf[2 + nbucket]; + + maxl = 0; + if ((bl = calloc(nbucket, sizeof(*bl))) == NULL) + errx(EXIT_FAILURE, "calloc failed"); + for (i = 0; (uint32_t)i < nbucket; i++) + for (j = bucket[i]; j > 0 && (uint32_t)j < nchain; j = chain[j]) + if (++bl[i] > maxl) + maxl = bl[i]; + if ((c = calloc(maxl + 1, sizeof(*c))) == NULL) + errx(EXIT_FAILURE, "calloc failed"); + for (i = 0; (uint32_t)i < nbucket; i++) + c[bl[i]]++; + printf("\nHistogram for bucket list length (total of %u buckets):\n", + nbucket); + printf(" Length\tNumber\t\t%% of total\tCoverage\n"); + total = 0; + for (i = 0; (uint32_t)i <= maxl; i++) { + total += c[i] * i; + printf("%7u\t%-10u\t(%5.1f%%)\t%5.1f%%\n", i, c[i], + c[i] * 100.0 / nbucket, total * 100.0 / (nchain - 1)); + } + free(c); + free(bl); +} + +static void +dump_svr4_hash64(struct readelf *re, struct section *s) +{ + Elf_Data *d, dst; + uint64_t *buf; + uint64_t nbucket, nchain; + uint64_t *bucket, *chain; + uint64_t *bl, *c, maxl, total; + int elferr, i, j; + + /* + * ALPHA uses 64-bit hash entries. Since libelf assumes that + * .hash section contains only 32-bit entry, an explicit + * gelf_xlatetom is needed here. + */ + (void) elf_errno(); + if ((d = elf_rawdata(s->scn, NULL)) == NULL) { + elferr = elf_errno(); + if (elferr != 0) + warnx("elf_rawdata failed: %s", + elf_errmsg(elferr)); + return; + } + d->d_type = ELF_T_XWORD; + memcpy(&dst, d, sizeof(Elf_Data)); + if (gelf_xlatetom(re->elf, &dst, d, + re->ehdr.e_ident[EI_DATA]) != &dst) { + warnx("gelf_xlatetom failed: %s", elf_errmsg(-1)); + return; + } + if (dst.d_size < 2 * sizeof(uint64_t)) { + warnx(".hash section too small"); + return; + } + buf = dst.d_buf; + nbucket = buf[0]; + nchain = buf[1]; + if (nbucket <= 0 || nchain <= 0) { + warnx("Malformed .hash section"); + return; + } + if (d->d_size != (nbucket + nchain + 2) * sizeof(uint32_t)) { + warnx("Malformed .hash section"); + return; + } + bucket = &buf[2]; + chain = &buf[2 + nbucket]; + + maxl = 0; + if ((bl = calloc(nbucket, sizeof(*bl))) == NULL) + errx(EXIT_FAILURE, "calloc failed"); + for (i = 0; (uint32_t)i < nbucket; i++) + for (j = bucket[i]; j > 0 && (uint32_t)j < nchain; j = chain[j]) + if (++bl[i] > maxl) + maxl = bl[i]; + if ((c = calloc(maxl + 1, sizeof(*c))) == NULL) + errx(EXIT_FAILURE, "calloc failed"); + for (i = 0; (uint64_t)i < nbucket; i++) + c[bl[i]]++; + printf("Histogram for bucket list length (total of %ju buckets):\n", + (uintmax_t)nbucket); + printf(" Length\tNumber\t\t%% of total\tCoverage\n"); + total = 0; + for (i = 0; (uint64_t)i <= maxl; i++) { + total += c[i] * i; + printf("%7u\t%-10ju\t(%5.1f%%)\t%5.1f%%\n", i, (uintmax_t)c[i], + c[i] * 100.0 / nbucket, total * 100.0 / (nchain - 1)); + } + free(c); + free(bl); +} + +static void +dump_gnu_hash(struct readelf *re, struct section *s) +{ + struct section *ds; + Elf_Data *d; + uint32_t *buf; + uint32_t *bucket, *chain; + uint32_t nbucket, nchain, symndx, maskwords; + uint32_t *bl, *c, maxl, total; + int elferr, dynsymcount, i, j; + + (void) elf_errno(); + if ((d = elf_getdata(s->scn, NULL)) == NULL) { + elferr = elf_errno(); + if (elferr != 0) + warnx("elf_getdata failed: %s", + elf_errmsg(elferr)); + return; + } + if (d->d_size < 4 * sizeof(uint32_t)) { + warnx(".gnu.hash section too small"); + return; + } + buf = d->d_buf; + nbucket = buf[0]; + symndx = buf[1]; + maskwords = buf[2]; + buf += 4; + ds = &re->sl[s->link]; + dynsymcount = ds->sz / ds->entsize; + nchain = dynsymcount - symndx; + if (d->d_size != 4 * sizeof(uint32_t) + maskwords * + (re->ec == ELFCLASS32 ? sizeof(uint32_t) : sizeof(uint64_t)) + + (nbucket + nchain) * sizeof(uint32_t)) { + warnx("Malformed .gnu.hash section"); + return; + } + bucket = buf + (re->ec == ELFCLASS32 ? maskwords : maskwords * 2); + chain = bucket + nbucket; + + maxl = 0; + if ((bl = calloc(nbucket, sizeof(*bl))) == NULL) + errx(EXIT_FAILURE, "calloc failed"); + for (i = 0; (uint32_t)i < nbucket; i++) + for (j = bucket[i]; j > 0 && (uint32_t)j - symndx < nchain; + j++) { + if (++bl[i] > maxl) + maxl = bl[i]; + if (chain[j - symndx] & 1) + break; + } + if ((c = calloc(maxl + 1, sizeof(*c))) == NULL) + errx(EXIT_FAILURE, "calloc failed"); + for (i = 0; (uint32_t)i < nbucket; i++) + c[bl[i]]++; + printf("Histogram for bucket list length (total of %u buckets):\n", + nbucket); + printf(" Length\tNumber\t\t%% of total\tCoverage\n"); + total = 0; + for (i = 0; (uint32_t)i <= maxl; i++) { + total += c[i] * i; + printf("%7u\t%-10u\t(%5.1f%%)\t%5.1f%%\n", i, c[i], + c[i] * 100.0 / nbucket, total * 100.0 / (nchain - 1)); + } + free(c); + free(bl); +} + +static void +dump_hash(struct readelf *re) +{ + struct section *s; + int i; + + for (i = 0; (size_t) i < re->shnum; i++) { + s = &re->sl[i]; + if (s->type == SHT_HASH || s->type == SHT_GNU_HASH) { + if (s->type == SHT_GNU_HASH) + dump_gnu_hash(re, s); + else if (re->ehdr.e_machine == EM_ALPHA && + s->entsize == 8) + dump_svr4_hash64(re, s); + else + dump_svr4_hash(s); + } + } +} + +static void +dump_notes(struct readelf *re) +{ + struct section *s; + const char *rawfile; + GElf_Phdr phdr; + Elf_Data *d; + size_t phnum; + int i, elferr; + + if (re->ehdr.e_type == ET_CORE) { + /* + * Search program headers in the core file for + * PT_NOTE entry. + */ + if (elf_getphnum(re->elf, &phnum) == 0) { + warnx("elf_getphnum failed: %s", elf_errmsg(-1)); + return; + } + if (phnum == 0) + return; + if ((rawfile = elf_rawfile(re->elf, NULL)) == NULL) { + warnx("elf_rawfile failed: %s", elf_errmsg(-1)); + return; + } + for (i = 0; (size_t) i < phnum; i++) { + if (gelf_getphdr(re->elf, i, &phdr) != &phdr) { + warnx("gelf_getphdr failed: %s", + elf_errmsg(-1)); + continue; + } + if (phdr.p_type == PT_NOTE) + dump_notes_content(re, rawfile + phdr.p_offset, + phdr.p_filesz, phdr.p_offset); + } + + } else { + /* + * For objects other than core files, Search for + * SHT_NOTE sections. + */ + for (i = 0; (size_t) i < re->shnum; i++) { + s = &re->sl[i]; + if (s->type == SHT_NOTE) { + (void) elf_errno(); + if ((d = elf_getdata(s->scn, NULL)) == NULL) { + elferr = elf_errno(); + if (elferr != 0) + warnx("elf_getdata failed: %s", + elf_errmsg(elferr)); + continue; + } + dump_notes_content(re, d->d_buf, d->d_size, + s->off); + } + } + } +} + +static void +dump_notes_content(struct readelf *re, const char *buf, size_t sz, off_t off) +{ + Elf_Note *note; + const char *end, *name; + + printf("\nNotes at offset %#010jx with length %#010jx:\n", + (uintmax_t) off, (uintmax_t) sz); + printf(" %-13s %-15s %s\n", "Owner", "Data size", "Description"); + end = buf + sz; + while (buf < end) { + if (buf + sizeof(*note) > end) { + warnx("invalid note header"); + return; + } + note = (Elf_Note *)(uintptr_t) buf; + name = (char *)(uintptr_t)(note + 1); + /* + * The name field is required to be nul-terminated, and + * n_namesz includes the terminating nul in observed + * implementations (contrary to the ELF-64 spec). A special + * case is needed for cores generated by some older Linux + * versions, which write a note named "CORE" without a nul + * terminator and n_namesz = 4. + */ + if (note->n_namesz == 0) + name = ""; + else if (note->n_namesz == 4 && strncmp(name, "CORE", 4) == 0) + name = "CORE"; + else if (strnlen(name, note->n_namesz) >= note->n_namesz) + name = "<invalid>"; + printf(" %-13s %#010jx", name, (uintmax_t) note->n_descsz); + printf(" %s\n", note_type(name, re->ehdr.e_type, + note->n_type)); + buf += sizeof(Elf_Note) + roundup2(note->n_namesz, 4) + + roundup2(note->n_descsz, 4); + } +} + +/* + * Symbol versioning sections are the same for 32bit and 64bit + * ELF objects. + */ +#define Elf_Verdef Elf32_Verdef +#define Elf_Verdaux Elf32_Verdaux +#define Elf_Verneed Elf32_Verneed +#define Elf_Vernaux Elf32_Vernaux + +#define SAVE_VERSION_NAME(x, n, t) \ + do { \ + while (x >= re->ver_sz) { \ + nv = realloc(re->ver, \ + sizeof(*re->ver) * re->ver_sz * 2); \ + if (nv == NULL) { \ + warn("realloc failed"); \ + free(re->ver); \ + return; \ + } \ + re->ver = nv; \ + for (i = re->ver_sz; i < re->ver_sz * 2; i++) { \ + re->ver[i].name = NULL; \ + re->ver[i].type = 0; \ + } \ + re->ver_sz *= 2; \ + } \ + if (x > 1) { \ + re->ver[x].name = n; \ + re->ver[x].type = t; \ + } \ + } while (0) + + +static void +dump_verdef(struct readelf *re, int dump) +{ + struct section *s; + struct symver *nv; + Elf_Data *d; + Elf_Verdef *vd; + Elf_Verdaux *vda; + uint8_t *buf, *end, *buf2; + const char *name; + int elferr, i, j; + + if ((s = re->vd_s) == NULL) + return; + + if (re->ver == NULL) { + re->ver_sz = 16; + if ((re->ver = calloc(re->ver_sz, sizeof(*re->ver))) == + NULL) { + warn("calloc failed"); + return; + } + re->ver[0].name = "*local*"; + re->ver[1].name = "*global*"; + } + + if (dump) + printf("\nVersion definition section (%s):\n", s->name); + (void) elf_errno(); + if ((d = elf_getdata(s->scn, NULL)) == NULL) { + elferr = elf_errno(); + if (elferr != 0) + warnx("elf_getdata failed: %s", elf_errmsg(elferr)); + return; + } + if (d->d_size == 0) + return; + + buf = d->d_buf; + end = buf + d->d_size; + while (buf + sizeof(Elf_Verdef) <= end) { + vd = (Elf_Verdef *) (uintptr_t) buf; + if (dump) { + printf(" 0x%4.4lx", (unsigned long) + (buf - (uint8_t *)d->d_buf)); + printf(" vd_version: %u vd_flags: %d" + " vd_ndx: %u vd_cnt: %u", vd->vd_version, + vd->vd_flags, vd->vd_ndx, vd->vd_cnt); + } + buf2 = buf + vd->vd_aux; + j = 0; + while (buf2 + sizeof(Elf_Verdaux) <= end && j < vd->vd_cnt) { + vda = (Elf_Verdaux *) (uintptr_t) buf2; + name = get_string(re, s->link, vda->vda_name); + if (j == 0) { + if (dump) + printf(" vda_name: %s\n", name); + SAVE_VERSION_NAME((int)vd->vd_ndx, name, 1); + } else if (dump) + printf(" 0x%4.4lx parent: %s\n", + (unsigned long) (buf2 - + (uint8_t *)d->d_buf), name); + if (vda->vda_next == 0) + break; + buf2 += vda->vda_next; + j++; + } + if (vd->vd_next == 0) + break; + buf += vd->vd_next; + } +} + +static void +dump_verneed(struct readelf *re, int dump) +{ + struct section *s; + struct symver *nv; + Elf_Data *d; + Elf_Verneed *vn; + Elf_Vernaux *vna; + uint8_t *buf, *end, *buf2; + const char *name; + int elferr, i, j; + + if ((s = re->vn_s) == NULL) + return; + + if (re->ver == NULL) { + re->ver_sz = 16; + if ((re->ver = calloc(re->ver_sz, sizeof(*re->ver))) == + NULL) { + warn("calloc failed"); + return; + } + re->ver[0].name = "*local*"; + re->ver[1].name = "*global*"; + } + + if (dump) + printf("\nVersion needed section (%s):\n", s->name); + (void) elf_errno(); + if ((d = elf_getdata(s->scn, NULL)) == NULL) { + elferr = elf_errno(); + if (elferr != 0) + warnx("elf_getdata failed: %s", elf_errmsg(elferr)); + return; + } + if (d->d_size == 0) + return; + + buf = d->d_buf; + end = buf + d->d_size; + while (buf + sizeof(Elf_Verneed) <= end) { + vn = (Elf_Verneed *) (uintptr_t) buf; + if (dump) { + printf(" 0x%4.4lx", (unsigned long) + (buf - (uint8_t *)d->d_buf)); + printf(" vn_version: %u vn_file: %s vn_cnt: %u\n", + vn->vn_version, + get_string(re, s->link, vn->vn_file), + vn->vn_cnt); + } + buf2 = buf + vn->vn_aux; + j = 0; + while (buf2 + sizeof(Elf_Vernaux) <= end && j < vn->vn_cnt) { + vna = (Elf32_Vernaux *) (uintptr_t) buf2; + if (dump) + printf(" 0x%4.4lx", (unsigned long) + (buf2 - (uint8_t *)d->d_buf)); + name = get_string(re, s->link, vna->vna_name); + if (dump) + printf(" vna_name: %s vna_flags: %u" + " vna_other: %u\n", name, + vna->vna_flags, vna->vna_other); + SAVE_VERSION_NAME((int)vna->vna_other, name, 0); + if (vna->vna_next == 0) + break; + buf2 += vna->vna_next; + j++; + } + if (vn->vn_next == 0) + break; + buf += vn->vn_next; + } +} + +static void +dump_versym(struct readelf *re) +{ + int i; + + if (re->vs_s == NULL || re->ver == NULL || re->vs == NULL) + return; + printf("\nVersion symbol section (%s):\n", re->vs_s->name); + for (i = 0; i < re->vs_sz; i++) { + if ((i & 3) == 0) { + if (i > 0) + putchar('\n'); + printf(" %03x:", i); + } + if (re->vs[i] & 0x8000) + printf(" %3xh %-12s ", re->vs[i] & 0x7fff, + re->ver[re->vs[i] & 0x7fff].name); + else + printf(" %3x %-12s ", re->vs[i], + re->ver[re->vs[i]].name); + } + putchar('\n'); +} + +static void +dump_ver(struct readelf *re) +{ + + if (re->vs_s && re->ver && re->vs) + dump_versym(re); + if (re->vd_s) + dump_verdef(re, 1); + if (re->vn_s) + dump_verneed(re, 1); +} + +static void +search_ver(struct readelf *re) +{ + struct section *s; + Elf_Data *d; + int elferr, i; + + for (i = 0; (size_t) i < re->shnum; i++) { + s = &re->sl[i]; + if (s->type == SHT_SUNW_versym) + re->vs_s = s; + if (s->type == SHT_SUNW_verneed) + re->vn_s = s; + if (s->type == SHT_SUNW_verdef) + re->vd_s = s; + } + if (re->vd_s) + dump_verdef(re, 0); + if (re->vn_s) + dump_verneed(re, 0); + if (re->vs_s && re->ver != NULL) { + (void) elf_errno(); + if ((d = elf_getdata(re->vs_s->scn, NULL)) == NULL) { + elferr = elf_errno(); + if (elferr != 0) + warnx("elf_getdata failed: %s", + elf_errmsg(elferr)); + return; + } + if (d->d_size == 0) + return; + re->vs = d->d_buf; + re->vs_sz = d->d_size / sizeof(Elf32_Half); + } +} + +#undef Elf_Verdef +#undef Elf_Verdaux +#undef Elf_Verneed +#undef Elf_Vernaux +#undef SAVE_VERSION_NAME + +/* + * Elf32_Lib and Elf64_Lib are identical. + */ +#define Elf_Lib Elf32_Lib + +static void +dump_liblist(struct readelf *re) +{ + struct section *s; + struct tm *t; + time_t ti; + char tbuf[20]; + Elf_Data *d; + Elf_Lib *lib; + int i, j, k, elferr, first; + + for (i = 0; (size_t) i < re->shnum; i++) { + s = &re->sl[i]; + if (s->type != SHT_GNU_LIBLIST) + continue; + (void) elf_errno(); + if ((d = elf_getdata(s->scn, NULL)) == NULL) { + elferr = elf_errno(); + if (elferr != 0) + warnx("elf_getdata failed: %s", + elf_errmsg(elferr)); + continue; + } + if (d->d_size <= 0) + continue; + lib = d->d_buf; + printf("\nLibrary list section '%s' ", s->name); + printf("contains %ju entries:\n", s->sz / s->entsize); + printf("%12s%24s%18s%10s%6s\n", "Library", "Time Stamp", + "Checksum", "Version", "Flags"); + for (j = 0; (uint64_t) j < s->sz / s->entsize; j++) { + printf("%3d: ", j); + printf("%-20.20s ", + get_string(re, s->link, lib->l_name)); + ti = lib->l_time_stamp; + t = gmtime(&ti); + snprintf(tbuf, sizeof(tbuf), "%04d-%02d-%02dT%02d:%02d" + ":%2d", t->tm_year + 1900, t->tm_mon + 1, + t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec); + printf("%-19.19s ", tbuf); + printf("0x%08x ", lib->l_checksum); + printf("%-7d %#x", lib->l_version, lib->l_flags); + if (lib->l_flags != 0) { + first = 1; + putchar('('); + for (k = 0; l_flag[k].name != NULL; k++) { + if ((l_flag[k].value & lib->l_flags) == + 0) + continue; + if (!first) + putchar(','); + else + first = 0; + printf("%s", l_flag[k].name); + } + putchar(')'); + } + putchar('\n'); + lib++; + } + } +} + +#undef Elf_Lib + +static uint8_t * +dump_unknown_tag(uint64_t tag, uint8_t *p) +{ + uint64_t val; + + /* + * According to ARM EABI: For tags > 32, even numbered tags have + * a ULEB128 param and odd numbered ones have NUL-terminated + * string param. This rule probably also applies for tags <= 32 + * if the object arch is not ARM. + */ + + printf(" Tag_unknown_%ju: ", (uintmax_t) tag); + + if (tag & 1) { + printf("%s\n", (char *) p); + p += strlen((char *) p) + 1; + } else { + val = _decode_uleb128(&p); + printf("%ju\n", (uintmax_t) val); + } + + return (p); +} + +static uint8_t * +dump_compatibility_tag(uint8_t *p) +{ + uint64_t val; + + val = _decode_uleb128(&p); + printf("flag = %ju, vendor = %s\n", val, p); + p += strlen((char *) p) + 1; + + return (p); +} + +static void +dump_arm_attributes(struct readelf *re, uint8_t *p, uint8_t *pe) +{ + uint64_t tag, val; + size_t i; + int found, desc; + + (void) re; + + while (p < pe) { + tag = _decode_uleb128(&p); + found = desc = 0; + for (i = 0; i < sizeof(aeabi_tags) / sizeof(aeabi_tags[0]); + i++) { + if (tag == aeabi_tags[i].tag) { + found = 1; + printf(" %s: ", aeabi_tags[i].s_tag); + if (aeabi_tags[i].get_desc) { + desc = 1; + val = _decode_uleb128(&p); + printf("%s\n", + aeabi_tags[i].get_desc(val)); + } + break; + } + if (tag < aeabi_tags[i].tag) + break; + } + if (!found) { + p = dump_unknown_tag(tag, p); + continue; + } + if (desc) + continue; + + switch (tag) { + case 4: /* Tag_CPU_raw_name */ + case 5: /* Tag_CPU_name */ + case 67: /* Tag_conformance */ + printf("%s\n", (char *) p); + p += strlen((char *) p) + 1; + break; + case 32: /* Tag_compatibility */ + p = dump_compatibility_tag(p); + break; + case 64: /* Tag_nodefaults */ + /* ignored, written as 0. */ + (void) _decode_uleb128(&p); + printf("True\n"); + break; + case 65: /* Tag_also_compatible_with */ + val = _decode_uleb128(&p); + /* Must be Tag_CPU_arch */ + if (val != 6) { + printf("unknown\n"); + break; + } + val = _decode_uleb128(&p); + printf("%s\n", aeabi_cpu_arch(val)); + /* Skip NUL terminator. */ + p++; + break; + default: + putchar('\n'); + break; + } + } +} + +#ifndef Tag_GNU_MIPS_ABI_FP +#define Tag_GNU_MIPS_ABI_FP 4 +#endif + +static void +dump_mips_attributes(struct readelf *re, uint8_t *p, uint8_t *pe) +{ + uint64_t tag, val; + + (void) re; + + while (p < pe) { + tag = _decode_uleb128(&p); + switch (tag) { + case Tag_GNU_MIPS_ABI_FP: + val = _decode_uleb128(&p); + printf(" Tag_GNU_MIPS_ABI_FP: %s\n", mips_abi_fp(val)); + break; + case 32: /* Tag_compatibility */ + p = dump_compatibility_tag(p); + break; + default: + p = dump_unknown_tag(tag, p); + break; + } + } +} + +#ifndef Tag_GNU_Power_ABI_FP +#define Tag_GNU_Power_ABI_FP 4 +#endif + +#ifndef Tag_GNU_Power_ABI_Vector +#define Tag_GNU_Power_ABI_Vector 8 +#endif + +static void +dump_ppc_attributes(uint8_t *p, uint8_t *pe) +{ + uint64_t tag, val; + + while (p < pe) { + tag = _decode_uleb128(&p); + switch (tag) { + case Tag_GNU_Power_ABI_FP: + val = _decode_uleb128(&p); + printf(" Tag_GNU_Power_ABI_FP: %s\n", ppc_abi_fp(val)); + break; + case Tag_GNU_Power_ABI_Vector: + val = _decode_uleb128(&p); + printf(" Tag_GNU_Power_ABI_Vector: %s\n", + ppc_abi_vector(val)); + break; + case 32: /* Tag_compatibility */ + p = dump_compatibility_tag(p); + break; + default: + p = dump_unknown_tag(tag, p); + break; + } + } +} + +static void +dump_attributes(struct readelf *re) +{ + struct section *s; + Elf_Data *d; + uint8_t *p, *sp; + size_t len, seclen, nlen, sublen; + uint64_t val; + int tag, i, elferr; + + for (i = 0; (size_t) i < re->shnum; i++) { + s = &re->sl[i]; + if (s->type != SHT_GNU_ATTRIBUTES && + (re->ehdr.e_machine != EM_ARM || s->type != SHT_LOPROC + 3)) + continue; + (void) elf_errno(); + if ((d = elf_rawdata(s->scn, NULL)) == NULL) { + elferr = elf_errno(); + if (elferr != 0) + warnx("elf_rawdata failed: %s", + elf_errmsg(elferr)); + continue; + } + if (d->d_size <= 0) + continue; + p = d->d_buf; + if (*p != 'A') { + printf("Unknown Attribute Section Format: %c\n", + (char) *p); + continue; + } + len = d->d_size - 1; + p++; + while (len > 0) { + if (len < 4) { + warnx("truncated attribute section length"); + break; + } + seclen = re->dw_decode(&p, 4); + if (seclen > len) { + warnx("invalid attribute section length"); + break; + } + len -= seclen; + nlen = strlen((char *) p) + 1; + if (nlen + 4 > seclen) { + warnx("invalid attribute section name"); + break; + } + printf("Attribute Section: %s\n", (char *) p); + p += nlen; + seclen -= nlen + 4; + while (seclen > 0) { + sp = p; + tag = *p++; + sublen = re->dw_decode(&p, 4); + if (sublen > seclen) { + warnx("invalid attribute sub-section" + " length"); + break; + } + seclen -= sublen; + printf("%s", top_tag(tag)); + if (tag == 2 || tag == 3) { + putchar(':'); + for (;;) { + val = _decode_uleb128(&p); + if (val == 0) + break; + printf(" %ju", (uintmax_t) val); + } + } + putchar('\n'); + if (re->ehdr.e_machine == EM_ARM && + s->type == SHT_LOPROC + 3) + dump_arm_attributes(re, p, sp + sublen); + else if (re->ehdr.e_machine == EM_MIPS || + re->ehdr.e_machine == EM_MIPS_RS3_LE) + dump_mips_attributes(re, p, + sp + sublen); + else if (re->ehdr.e_machine == EM_PPC) + dump_ppc_attributes(p, sp + sublen); + p = sp + sublen; + } + } + } +} + +static void +dump_mips_specific_info(struct readelf *re) +{ + struct section *s; + int i, options_found; + + options_found = 0; + s = NULL; + for (i = 0; (size_t) i < re->shnum; i++) { + s = &re->sl[i]; + if (s->name != NULL && (!strcmp(s->name, ".MIPS.options") || + (s->type == SHT_MIPS_OPTIONS))) { + dump_mips_options(re, s); + options_found = 1; + } + } + + /* + * According to SGI mips64 spec, .reginfo should be ignored if + * .MIPS.options section is present. + */ + if (!options_found) { + for (i = 0; (size_t) i < re->shnum; i++) { + s = &re->sl[i]; + if (s->name != NULL && (!strcmp(s->name, ".reginfo") || + (s->type == SHT_MIPS_REGINFO))) + dump_mips_reginfo(re, s); + } + } +} + +static void +dump_mips_reginfo(struct readelf *re, struct section *s) +{ + Elf_Data *d; + int elferr; + + (void) elf_errno(); + if ((d = elf_rawdata(s->scn, NULL)) == NULL) { + elferr = elf_errno(); + if (elferr != 0) + warnx("elf_rawdata failed: %s", + elf_errmsg(elferr)); + return; + } + if (d->d_size <= 0) + return; + + printf("\nSection '%s' contains %ju entries:\n", s->name, + s->sz / s->entsize); + dump_mips_odk_reginfo(re, d->d_buf, d->d_size); +} + +static void +dump_mips_options(struct readelf *re, struct section *s) +{ + Elf_Data *d; + uint32_t info; + uint16_t sndx; + uint8_t *p, *pe; + uint8_t kind, size; + int elferr; + + (void) elf_errno(); + if ((d = elf_rawdata(s->scn, NULL)) == NULL) { + elferr = elf_errno(); + if (elferr != 0) + warnx("elf_rawdata failed: %s", + elf_errmsg(elferr)); + return; + } + if (d->d_size == 0) + return; + + printf("\nSection %s contains:\n", s->name); + p = d->d_buf; + pe = p + d->d_size; + while (p < pe) { + if (pe - p < 8) { + warnx("Truncated MIPS option header"); + return; + } + kind = re->dw_decode(&p, 1); + size = re->dw_decode(&p, 1); + sndx = re->dw_decode(&p, 2); + info = re->dw_decode(&p, 4); + if (size < 8 || size - 8 > pe - p) { + warnx("Malformed MIPS option header"); + return; + } + size -= 8; + switch (kind) { + case ODK_REGINFO: + dump_mips_odk_reginfo(re, p, size); + break; + case ODK_EXCEPTIONS: + printf(" EXCEPTIONS FPU_MIN: %#x\n", + info & OEX_FPU_MIN); + printf("%11.11s FPU_MAX: %#x\n", "", + info & OEX_FPU_MAX); + dump_mips_option_flags("", mips_exceptions_option, + info); + break; + case ODK_PAD: + printf(" %-10.10s section: %ju\n", "OPAD", + (uintmax_t) sndx); + dump_mips_option_flags("", mips_pad_option, info); + break; + case ODK_HWPATCH: + dump_mips_option_flags("HWPATCH", mips_hwpatch_option, + info); + break; + case ODK_HWAND: + dump_mips_option_flags("HWAND", mips_hwa_option, info); + break; + case ODK_HWOR: + dump_mips_option_flags("HWOR", mips_hwo_option, info); + break; + case ODK_FILL: + printf(" %-10.10s %#jx\n", "FILL", (uintmax_t) info); + break; + case ODK_TAGS: + printf(" %-10.10s\n", "TAGS"); + break; + case ODK_GP_GROUP: + printf(" %-10.10s GP group number: %#x\n", "GP_GROUP", + info & 0xFFFF); + if (info & 0x10000) + printf(" %-10.10s GP group is " + "self-contained\n", ""); + break; + case ODK_IDENT: + printf(" %-10.10s default GP group number: %#x\n", + "IDENT", info & 0xFFFF); + if (info & 0x10000) + printf(" %-10.10s default GP group is " + "self-contained\n", ""); + break; + case ODK_PAGESIZE: + printf(" %-10.10s\n", "PAGESIZE"); + break; + default: + break; + } + p += size; + } +} + +static void +dump_mips_option_flags(const char *name, struct mips_option *opt, uint64_t info) +{ + int first; + + first = 1; + for (; opt->desc != NULL; opt++) { + if (info & opt->flag) { + printf(" %-10.10s %s\n", first ? name : "", + opt->desc); + first = 0; + } + } +} + +static void +dump_mips_odk_reginfo(struct readelf *re, uint8_t *p, size_t sz) +{ + uint32_t ri_gprmask; + uint32_t ri_cprmask[4]; + uint64_t ri_gp_value; + uint8_t *pe; + int i; + + pe = p + sz; + while (p < pe) { + ri_gprmask = re->dw_decode(&p, 4); + /* Skip ri_pad padding field for mips64. */ + if (re->ec == ELFCLASS64) + re->dw_decode(&p, 4); + for (i = 0; i < 4; i++) + ri_cprmask[i] = re->dw_decode(&p, 4); + if (re->ec == ELFCLASS32) + ri_gp_value = re->dw_decode(&p, 4); + else + ri_gp_value = re->dw_decode(&p, 8); + printf(" %s ", option_kind(ODK_REGINFO)); + printf("ri_gprmask: 0x%08jx\n", (uintmax_t) ri_gprmask); + for (i = 0; i < 4; i++) + printf("%11.11s ri_cprmask[%d]: 0x%08jx\n", "", i, + (uintmax_t) ri_cprmask[i]); + printf("%12.12s", ""); + printf("ri_gp_value: %#jx\n", (uintmax_t) ri_gp_value); + } +} + +static void +dump_arch_specific_info(struct readelf *re) +{ + + dump_liblist(re); + dump_attributes(re); + + switch (re->ehdr.e_machine) { + case EM_MIPS: + case EM_MIPS_RS3_LE: + dump_mips_specific_info(re); + default: + break; + } +} + +static const char * +dwarf_regname(struct readelf *re, unsigned int num) +{ + static char rx[32]; + const char *rn; + + if ((rn = dwarf_reg(re->ehdr.e_machine, num)) != NULL) + return (rn); + + snprintf(rx, sizeof(rx), "r%u", num); + + return (rx); +} + +static void +dump_dwarf_line(struct readelf *re) +{ + struct section *s; + Dwarf_Die die; + Dwarf_Error de; + Dwarf_Half tag, version, pointer_size; + Dwarf_Unsigned offset, endoff, length, hdrlen, dirndx, mtime, fsize; + Dwarf_Small minlen, defstmt, lrange, opbase, oplen; + Elf_Data *d; + char *pn; + uint64_t address, file, line, column, isa, opsize, udelta; + int64_t sdelta; + uint8_t *p, *pe; + int8_t lbase; + int i, is_stmt, dwarf_size, elferr, ret; + + printf("\nDump of debug contents of section .debug_line:\n"); + + s = NULL; + for (i = 0; (size_t) i < re->shnum; i++) { + s = &re->sl[i]; + if (s->name != NULL && !strcmp(s->name, ".debug_line")) + break; + } + if ((size_t) i >= re->shnum) + return; + + (void) elf_errno(); + if ((d = elf_getdata(s->scn, NULL)) == NULL) { + elferr = elf_errno(); + if (elferr != 0) + warnx("elf_getdata failed: %s", elf_errmsg(-1)); + return; + } + if (d->d_size <= 0) + return; + + while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, NULL, NULL, + NULL, &de)) == DW_DLV_OK) { + die = NULL; + while (dwarf_siblingof(re->dbg, die, &die, &de) == DW_DLV_OK) { + if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) { + warnx("dwarf_tag failed: %s", + dwarf_errmsg(de)); + return; + } + /* XXX: What about DW_TAG_partial_unit? */ + if (tag == DW_TAG_compile_unit) + break; + } + if (die == NULL) { + warnx("could not find DW_TAG_compile_unit die"); + return; + } + if (dwarf_attrval_unsigned(die, DW_AT_stmt_list, &offset, + &de) != DW_DLV_OK) + continue; + + length = re->dw_read(d, &offset, 4); + if (length == 0xffffffff) { + dwarf_size = 8; + length = re->dw_read(d, &offset, 8); + } else + dwarf_size = 4; + + if (length > d->d_size - offset) { + warnx("invalid .dwarf_line section"); + continue; + } + + endoff = offset + length; + version = re->dw_read(d, &offset, 2); + hdrlen = re->dw_read(d, &offset, dwarf_size); + minlen = re->dw_read(d, &offset, 1); + defstmt = re->dw_read(d, &offset, 1); + lbase = re->dw_read(d, &offset, 1); + lrange = re->dw_read(d, &offset, 1); + opbase = re->dw_read(d, &offset, 1); + + printf("\n"); + printf(" Length:\t\t\t%ju\n", (uintmax_t) length); + printf(" DWARF version:\t\t%u\n", version); + printf(" Prologue Length:\t\t%ju\n", (uintmax_t) hdrlen); + printf(" Minimum Instruction Length:\t%u\n", minlen); + printf(" Initial value of 'is_stmt':\t%u\n", defstmt); + printf(" Line Base:\t\t\t%d\n", lbase); + printf(" Line Range:\t\t\t%u\n", lrange); + printf(" Opcode Base:\t\t\t%u\n", opbase); + (void) dwarf_get_address_size(re->dbg, &pointer_size, &de); + printf(" (Pointer size:\t\t%u)\n", pointer_size); + + printf("\n"); + printf(" Opcodes:\n"); + for (i = 1; i < opbase; i++) { + oplen = re->dw_read(d, &offset, 1); + printf(" Opcode %d has %u args\n", i, oplen); + } + + printf("\n"); + printf(" The Directory Table:\n"); + p = (uint8_t *) d->d_buf + offset; + while (*p != '\0') { + printf(" %s\n", (char *) p); + p += strlen((char *) p) + 1; + } + + p++; + printf("\n"); + printf(" The File Name Table:\n"); + printf(" Entry\tDir\tTime\tSize\tName\n"); + i = 0; + while (*p != '\0') { + i++; + pn = (char *) p; + p += strlen(pn) + 1; + dirndx = _decode_uleb128(&p); + mtime = _decode_uleb128(&p); + fsize = _decode_uleb128(&p); + printf(" %d\t%ju\t%ju\t%ju\t%s\n", i, + (uintmax_t) dirndx, (uintmax_t) mtime, + (uintmax_t) fsize, pn); + } + +#define RESET_REGISTERS \ + do { \ + address = 0; \ + file = 1; \ + line = 1; \ + column = 0; \ + is_stmt = defstmt; \ + } while(0) + +#define LINE(x) (lbase + (((x) - opbase) % lrange)) +#define ADDRESS(x) ((((x) - opbase) / lrange) * minlen) + + p++; + pe = (uint8_t *) d->d_buf + endoff; + printf("\n"); + printf(" Line Number Statements:\n"); + + RESET_REGISTERS; + + while (p < pe) { + + if (*p == 0) { + /* + * Extended Opcodes. + */ + p++; + opsize = _decode_uleb128(&p); + printf(" Extended opcode %u: ", *p); + switch (*p) { + case DW_LNE_end_sequence: + p++; + RESET_REGISTERS; + printf("End of Sequence\n"); + break; + case DW_LNE_set_address: + p++; + address = re->dw_decode(&p, + pointer_size); + printf("set Address to %#jx\n", + (uintmax_t) address); + break; + case DW_LNE_define_file: + p++; + pn = (char *) p; + p += strlen(pn) + 1; + dirndx = _decode_uleb128(&p); + mtime = _decode_uleb128(&p); + fsize = _decode_uleb128(&p); + printf("define new file: %s\n", pn); + break; + default: + /* Unrecognized extened opcodes. */ + p += opsize; + printf("unknown opcode\n"); + } + } else if (*p > 0 && *p < opbase) { + /* + * Standard Opcodes. + */ + switch(*p++) { + case DW_LNS_copy: + printf(" Copy\n"); + break; + case DW_LNS_advance_pc: + udelta = _decode_uleb128(&p) * + minlen; + address += udelta; + printf(" Advance PC by %ju to %#jx\n", + (uintmax_t) udelta, + (uintmax_t) address); + break; + case DW_LNS_advance_line: + sdelta = _decode_sleb128(&p); + 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); + printf(" Set File to %ju\n", + (uintmax_t) file); + break; + case DW_LNS_set_column: + column = _decode_uleb128(&p); + printf(" Set Column to %ju\n", + (uintmax_t) column); + break; + case DW_LNS_negate_stmt: + is_stmt = !is_stmt; + printf(" Set is_stmt to %d\n", is_stmt); + break; + case DW_LNS_set_basic_block: + printf(" Set basic block flag\n"); + break; + case DW_LNS_const_add_pc: + address += ADDRESS(255); + printf(" Advance PC by constant %ju" + " to %#jx\n", + (uintmax_t) ADDRESS(255), + (uintmax_t) address); + break; + case DW_LNS_fixed_advance_pc: + udelta = re->dw_decode(&p, 2); + address += udelta; + printf(" Advance PC by fixed value " + "%ju to %#jx\n", + (uintmax_t) udelta, + (uintmax_t) address); + break; + case DW_LNS_set_prologue_end: + printf(" Set prologue end flag\n"); + break; + case DW_LNS_set_epilogue_begin: + printf(" Set epilogue begin flag\n"); + break; + case DW_LNS_set_isa: + isa = _decode_uleb128(&p); + printf(" Set isa to %ju\n", isa); + break; + default: + /* Unrecognized extended opcodes. */ + printf(" Unknown extended opcode %u\n", + *(p - 1)); + break; + } + + } else { + /* + * Special Opcodes. + */ + line += LINE(*p); + address += ADDRESS(*p); + printf(" Special opcode %u: advance Address " + "by %ju to %#jx and Line by %jd to %ju\n", + *p - opbase, (uintmax_t) ADDRESS(*p), + (uintmax_t) address, (intmax_t) LINE(*p), + (uintmax_t) line); + p++; + } + + + } + } + if (ret == DW_DLV_ERROR) + warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de)); + +#undef RESET_REGISTERS +#undef LINE +#undef ADDRESS +} + +static void +dump_dwarf_line_decoded(struct readelf *re) +{ + Dwarf_Die die; + Dwarf_Line *linebuf, ln; + Dwarf_Addr lineaddr; + Dwarf_Signed linecount, srccount; + Dwarf_Unsigned lineno, fn; + Dwarf_Error de; + const char *dir, *file; + char **srcfiles; + int i, ret; + + printf("Decoded dump of debug contents of section .debug_line:\n\n"); + while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, NULL, NULL, + NULL, &de)) == DW_DLV_OK) { + if (dwarf_siblingof(re->dbg, NULL, &die, &de) != DW_DLV_OK) + continue; + if (dwarf_attrval_string(die, DW_AT_name, &file, &de) != + DW_DLV_OK) + file = NULL; + if (dwarf_attrval_string(die, DW_AT_comp_dir, &dir, &de) != + DW_DLV_OK) + dir = NULL; + printf("CU: "); + if (dir && file) + printf("%s/", dir); + if (file) + printf("%s", file); + putchar('\n'); + printf("%-37s %11s %s\n", "Filename", "Line Number", + "Starting Address"); + if (dwarf_srclines(die, &linebuf, &linecount, &de) != DW_DLV_OK) + continue; + if (dwarf_srcfiles(die, &srcfiles, &srccount, &de) != DW_DLV_OK) + continue; + for (i = 0; i < linecount; i++) { + ln = linebuf[i]; + if (dwarf_line_srcfileno(ln, &fn, &de) != DW_DLV_OK) + continue; + if (dwarf_lineno(ln, &lineno, &de) != DW_DLV_OK) + continue; + if (dwarf_lineaddr(ln, &lineaddr, &de) != DW_DLV_OK) + continue; + printf("%-37s %11ju %#18jx\n", + basename(srcfiles[fn - 1]), (uintmax_t) lineno, + (uintmax_t) lineaddr); + } + putchar('\n'); + } +} + +static void +dump_dwarf_die(struct readelf *re, Dwarf_Die die, int level) +{ + Dwarf_Attribute *attr_list; + Dwarf_Die ret_die; + Dwarf_Off dieoff, cuoff, culen, attroff; + Dwarf_Unsigned ate, lang, v_udata, v_sig; + Dwarf_Signed attr_count, v_sdata; + Dwarf_Off v_off; + Dwarf_Addr v_addr; + Dwarf_Half tag, attr, form; + Dwarf_Block *v_block; + Dwarf_Bool v_bool, is_info; + Dwarf_Sig8 v_sig8; + Dwarf_Error de; + Dwarf_Ptr v_expr; + const char *tag_str, *attr_str, *ate_str, *lang_str; + char unk_tag[32], unk_attr[32]; + char *v_str; + uint8_t *b, *p; + int i, j, abc, ret; + + if (dwarf_dieoffset(die, &dieoff, &de) != DW_DLV_OK) { + warnx("dwarf_dieoffset failed: %s", dwarf_errmsg(de)); + goto cont_search; + } + + printf(" <%d><%jx>: ", level, (uintmax_t) dieoff); + + if (dwarf_die_CU_offset_range(die, &cuoff, &culen, &de) != DW_DLV_OK) { + warnx("dwarf_die_CU_offset_range failed: %s", + dwarf_errmsg(de)); + cuoff = 0; + } + + abc = dwarf_die_abbrev_code(die); + if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) { + warnx("dwarf_tag failed: %s", dwarf_errmsg(de)); + goto cont_search; + } + if (dwarf_get_TAG_name(tag, &tag_str) != DW_DLV_OK) { + snprintf(unk_tag, sizeof(unk_tag), "[Unknown Tag: %#x]", tag); + tag_str = unk_tag; + } + + printf("Abbrev Number: %d (%s)\n", abc, tag_str); + + if ((ret = dwarf_attrlist(die, &attr_list, &attr_count, &de)) != + DW_DLV_OK) { + if (ret == DW_DLV_ERROR) + warnx("dwarf_attrlist failed: %s", dwarf_errmsg(de)); + goto cont_search; + } + + for (i = 0; i < attr_count; i++) { + if (dwarf_whatform(attr_list[i], &form, &de) != DW_DLV_OK) { + warnx("dwarf_whatform failed: %s", dwarf_errmsg(de)); + continue; + } + if (dwarf_whatattr(attr_list[i], &attr, &de) != DW_DLV_OK) { + warnx("dwarf_whatattr failed: %s", dwarf_errmsg(de)); + continue; + } + if (dwarf_get_AT_name(attr, &attr_str) != DW_DLV_OK) { + snprintf(unk_attr, sizeof(unk_attr), + "[Unknown AT: %#x]", attr); + attr_str = unk_attr; + } + if (dwarf_attroffset(attr_list[i], &attroff, &de) != + DW_DLV_OK) { + warnx("dwarf_attroffset failed: %s", dwarf_errmsg(de)); + attroff = 0; + } + printf(" <%jx> %-18s: ", (uintmax_t) attroff, attr_str); + switch (form) { + case DW_FORM_ref_addr: + case DW_FORM_sec_offset: + if (dwarf_global_formref(attr_list[i], &v_off, &de) != + DW_DLV_OK) { + warnx("dwarf_global_formref failed: %s", + dwarf_errmsg(de)); + continue; + } + if (form == DW_FORM_ref_addr) + printf("<0x%jx>", (uintmax_t) v_off); + else + printf("0x%jx", (uintmax_t) v_off); + break; + + case DW_FORM_ref1: + case DW_FORM_ref2: + case DW_FORM_ref4: + case DW_FORM_ref8: + case DW_FORM_ref_udata: + if (dwarf_formref(attr_list[i], &v_off, &de) != + DW_DLV_OK) { + warnx("dwarf_formref failed: %s", + dwarf_errmsg(de)); + continue; + } + v_off += cuoff; + printf("<0x%jx>", (uintmax_t) v_off); + break; + + case DW_FORM_addr: + if (dwarf_formaddr(attr_list[i], &v_addr, &de) != + DW_DLV_OK) { + warnx("dwarf_formaddr failed: %s", + dwarf_errmsg(de)); + continue; + } + printf("%#jx", (uintmax_t) v_addr); + break; + + case DW_FORM_data1: + case DW_FORM_data2: + case DW_FORM_data4: + case DW_FORM_data8: + case DW_FORM_udata: + if (dwarf_formudata(attr_list[i], &v_udata, &de) != + DW_DLV_OK) { + warnx("dwarf_formudata failed: %s", + dwarf_errmsg(de)); + continue; + } + if (attr == DW_AT_high_pc) + printf("0x%jx", (uintmax_t) v_udata); + else + printf("%ju", (uintmax_t) v_udata); + break; + + case DW_FORM_sdata: + if (dwarf_formsdata(attr_list[i], &v_sdata, &de) != + DW_DLV_OK) { + warnx("dwarf_formudata failed: %s", + dwarf_errmsg(de)); + continue; + } + printf("%jd", (intmax_t) v_sdata); + break; + + case DW_FORM_flag: + if (dwarf_formflag(attr_list[i], &v_bool, &de) != + DW_DLV_OK) { + warnx("dwarf_formflag failed: %s", + dwarf_errmsg(de)); + continue; + } + printf("%jd", (intmax_t) v_bool); + break; + + case DW_FORM_flag_present: + putchar('1'); + break; + + case DW_FORM_string: + case DW_FORM_strp: + if (dwarf_formstring(attr_list[i], &v_str, &de) != + DW_DLV_OK) { + warnx("dwarf_formstring failed: %s", + dwarf_errmsg(de)); + continue; + } + if (form == DW_FORM_string) + printf("%s", v_str); + else + printf("(indirect string) %s", v_str); + break; + + case DW_FORM_block: + case DW_FORM_block1: + case DW_FORM_block2: + case DW_FORM_block4: + if (dwarf_formblock(attr_list[i], &v_block, &de) != + DW_DLV_OK) { + warnx("dwarf_formblock failed: %s", + dwarf_errmsg(de)); + continue; + } + printf("%ju byte block:", (uintmax_t) v_block->bl_len); + b = v_block->bl_data; + for (j = 0; (Dwarf_Unsigned) j < v_block->bl_len; j++) + printf(" %x", b[j]); + printf("\t("); + dump_dwarf_block(re, v_block->bl_data, v_block->bl_len); + putchar(')'); + break; + + case DW_FORM_exprloc: + if (dwarf_formexprloc(attr_list[i], &v_udata, &v_expr, + &de) != DW_DLV_OK) { + warnx("dwarf_formexprloc failed: %s", + dwarf_errmsg(de)); + continue; + } + printf("%ju byte block:", (uintmax_t) v_udata); + b = v_expr; + for (j = 0; (Dwarf_Unsigned) j < v_udata; j++) + printf(" %x", b[j]); + printf("\t("); + dump_dwarf_block(re, v_expr, v_udata); + putchar(')'); + break; + + case DW_FORM_ref_sig8: + if (dwarf_formsig8(attr_list[i], &v_sig8, &de) != + DW_DLV_OK) { + warnx("dwarf_formsig8 failed: %s", + dwarf_errmsg(de)); + continue; + } + p = (uint8_t *)(uintptr_t) &v_sig8.signature[0]; + v_sig = re->dw_decode(&p, 8); + printf("signature: 0x%jx", (uintmax_t) v_sig); + } + switch (attr) { + case DW_AT_encoding: + if (dwarf_attrval_unsigned(die, attr, &ate, &de) != + DW_DLV_OK) + break; + if (dwarf_get_ATE_name(ate, &ate_str) != DW_DLV_OK) + ate_str = "DW_ATE_UNKNOWN"; + printf("\t(%s)", &ate_str[strlen("DW_ATE_")]); + break; + + case DW_AT_language: + if (dwarf_attrval_unsigned(die, attr, &lang, &de) != + DW_DLV_OK) + break; + if (dwarf_get_LANG_name(lang, &lang_str) != DW_DLV_OK) + break; + printf("\t(%s)", &lang_str[strlen("DW_LANG_")]); + break; + + case DW_AT_location: + case DW_AT_string_length: + case DW_AT_return_addr: + case DW_AT_data_member_location: + case DW_AT_frame_base: + case DW_AT_segment: + case DW_AT_static_link: + case DW_AT_use_location: + case DW_AT_vtable_elem_location: + switch (form) { + case DW_FORM_data4: + case DW_FORM_data8: + case DW_FORM_sec_offset: + printf("\t(location list)"); + break; + default: + break; + } + + default: + break; + } + putchar('\n'); + } + + +cont_search: + /* Search children. */ + ret = dwarf_child(die, &ret_die, &de); + if (ret == DW_DLV_ERROR) + warnx("dwarf_child: %s", dwarf_errmsg(de)); + else if (ret == DW_DLV_OK) + dump_dwarf_die(re, ret_die, level + 1); + + /* Search sibling. */ + is_info = dwarf_get_die_infotypes_flag(die); + ret = dwarf_siblingof_b(re->dbg, die, &ret_die, is_info, &de); + if (ret == DW_DLV_ERROR) + warnx("dwarf_siblingof: %s", dwarf_errmsg(de)); + else if (ret == DW_DLV_OK) + dump_dwarf_die(re, ret_die, level); + + dwarf_dealloc(re->dbg, die, DW_DLA_DIE); +} + +static void +set_cu_context(struct readelf *re, Dwarf_Half psize, Dwarf_Half osize, + Dwarf_Half ver) +{ + + re->cu_psize = psize; + re->cu_osize = osize; + re->cu_ver = ver; +} + +static void +dump_dwarf_info(struct readelf *re, Dwarf_Bool is_info) +{ + struct section *s; + Dwarf_Die die; + Dwarf_Error de; + Dwarf_Half tag, version, pointer_size, off_size; + Dwarf_Off cu_offset, cu_length; + Dwarf_Off aboff; + Dwarf_Unsigned typeoff; + Dwarf_Sig8 sig8; + Dwarf_Unsigned sig; + uint8_t *p; + const char *sn; + int i, ret; + + sn = is_info ? ".debug_info" : ".debug_types"; + + s = NULL; + for (i = 0; (size_t) i < re->shnum; i++) { + s = &re->sl[i]; + if (s->name != NULL && !strcmp(s->name, sn)) + break; + } + if ((size_t) i >= re->shnum) + return; + + do { + printf("\nDump of debug contents of section %s:\n", sn); + + while ((ret = dwarf_next_cu_header_c(re->dbg, is_info, NULL, + &version, &aboff, &pointer_size, &off_size, NULL, &sig8, + &typeoff, NULL, &de)) == DW_DLV_OK) { + set_cu_context(re, pointer_size, off_size, version); + die = NULL; + while (dwarf_siblingof_b(re->dbg, die, &die, is_info, + &de) == DW_DLV_OK) { + if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) { + warnx("dwarf_tag failed: %s", + dwarf_errmsg(de)); + continue; + } + /* XXX: What about DW_TAG_partial_unit? */ + if ((is_info && tag == DW_TAG_compile_unit) || + (!is_info && tag == DW_TAG_type_unit)) + break; + } + if (die == NULL && is_info) { + warnx("could not find DW_TAG_compile_unit " + "die"); + continue; + } else if (die == NULL && !is_info) { + warnx("could not find DW_TAG_type_unit die"); + continue; + } + + if (dwarf_die_CU_offset_range(die, &cu_offset, + &cu_length, &de) != DW_DLV_OK) { + warnx("dwarf_die_CU_offset failed: %s", + dwarf_errmsg(de)); + continue; + } + + cu_length -= off_size == 4 ? 4 : 12; + + sig = 0; + if (!is_info) { + p = (uint8_t *)(uintptr_t) &sig8.signature[0]; + sig = re->dw_decode(&p, 8); + } + + printf("\n Type Unit @ offset 0x%jx:\n", + (uintmax_t) cu_offset); + printf(" Length:\t\t%#jx (%d-bit)\n", + (uintmax_t) cu_length, off_size == 4 ? 32 : 64); + printf(" Version:\t\t%u\n", version); + printf(" Abbrev Offset:\t0x%jx\n", + (uintmax_t) aboff); + printf(" Pointer Size:\t%u\n", pointer_size); + if (!is_info) { + printf(" Signature:\t\t0x%016jx\n", + (uintmax_t) sig); + printf(" Type Offset:\t0x%jx\n", + (uintmax_t) typeoff); + } + + dump_dwarf_die(re, die, 0); + } + if (ret == DW_DLV_ERROR) + warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de)); + if (is_info) + break; + } while (dwarf_next_types_section(re->dbg, &de) == DW_DLV_OK); +} + +static void +dump_dwarf_abbrev(struct readelf *re) +{ + Dwarf_Abbrev ab; + Dwarf_Off aboff, atoff; + Dwarf_Unsigned length, attr_count; + Dwarf_Signed flag, form; + Dwarf_Half tag, attr; + Dwarf_Error de; + const char *tag_str, *attr_str, *form_str; + char unk_tag[32], unk_attr[32], unk_form[32]; + int i, j, ret; + + printf("\nContents of section .debug_abbrev:\n\n"); + + while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, &aboff, + NULL, NULL, &de)) == DW_DLV_OK) { + printf(" Number TAG\n"); + i = 0; + while ((ret = dwarf_get_abbrev(re->dbg, aboff, &ab, &length, + &attr_count, &de)) == DW_DLV_OK) { + if (length == 1) { + dwarf_dealloc(re->dbg, ab, DW_DLA_ABBREV); + break; + } + aboff += length; + printf("%4d", ++i); + if (dwarf_get_abbrev_tag(ab, &tag, &de) != DW_DLV_OK) { + warnx("dwarf_get_abbrev_tag failed: %s", + dwarf_errmsg(de)); + goto next_abbrev; + } + if (dwarf_get_TAG_name(tag, &tag_str) != DW_DLV_OK) { + snprintf(unk_tag, sizeof(unk_tag), + "[Unknown Tag: %#x]", tag); + tag_str = unk_tag; + } + if (dwarf_get_abbrev_children_flag(ab, &flag, &de) != + DW_DLV_OK) { + warnx("dwarf_get_abbrev_children_flag failed:" + " %s", dwarf_errmsg(de)); + goto next_abbrev; + } + printf(" %s %s\n", tag_str, + flag ? "[has children]" : "[no children]"); + for (j = 0; (Dwarf_Unsigned) j < attr_count; j++) { + if (dwarf_get_abbrev_entry(ab, (Dwarf_Signed) j, + &attr, &form, &atoff, &de) != DW_DLV_OK) { + warnx("dwarf_get_abbrev_entry failed:" + " %s", dwarf_errmsg(de)); + continue; + } + if (dwarf_get_AT_name(attr, &attr_str) != + DW_DLV_OK) { + snprintf(unk_attr, sizeof(unk_attr), + "[Unknown AT: %#x]", attr); + attr_str = unk_attr; + } + if (dwarf_get_FORM_name(form, &form_str) != + DW_DLV_OK) { + snprintf(unk_form, sizeof(unk_form), + "[Unknown Form: %#x]", + (Dwarf_Half) form); + form_str = unk_form; + } + printf(" %-18s %s\n", attr_str, form_str); + } + next_abbrev: + dwarf_dealloc(re->dbg, ab, DW_DLA_ABBREV); + } + if (ret != DW_DLV_OK) + warnx("dwarf_get_abbrev: %s", dwarf_errmsg(de)); + } + if (ret == DW_DLV_ERROR) + warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de)); +} + +static void +dump_dwarf_pubnames(struct readelf *re) +{ + struct section *s; + Dwarf_Off die_off; + Dwarf_Unsigned offset, length, nt_cu_offset, nt_cu_length; + Dwarf_Signed cnt; + Dwarf_Global *globs; + Dwarf_Half nt_version; + Dwarf_Error de; + Elf_Data *d; + char *glob_name; + int i, dwarf_size, elferr; + + printf("\nContents of the .debug_pubnames section:\n"); + + s = NULL; + for (i = 0; (size_t) i < re->shnum; i++) { + s = &re->sl[i]; + if (s->name != NULL && !strcmp(s->name, ".debug_pubnames")) + break; + } + if ((size_t) i >= re->shnum) + return; + + (void) elf_errno(); + if ((d = elf_getdata(s->scn, NULL)) == NULL) { + elferr = elf_errno(); + if (elferr != 0) + warnx("elf_getdata failed: %s", elf_errmsg(-1)); + return; + } + if (d->d_size <= 0) + return; + + /* Read in .debug_pubnames section table header. */ + offset = 0; + length = re->dw_read(d, &offset, 4); + if (length == 0xffffffff) { + dwarf_size = 8; + length = re->dw_read(d, &offset, 8); + } else + dwarf_size = 4; + + if (length > d->d_size - offset) { + warnx("invalid .dwarf_pubnames section"); + return; + } + + nt_version = re->dw_read(d, &offset, 2); + nt_cu_offset = re->dw_read(d, &offset, dwarf_size); + nt_cu_length = re->dw_read(d, &offset, dwarf_size); + printf(" Length:\t\t\t\t%ju\n", (uintmax_t) length); + printf(" Version:\t\t\t\t%u\n", nt_version); + printf(" Offset into .debug_info section:\t%ju\n", + (uintmax_t) nt_cu_offset); + printf(" Size of area in .debug_info section:\t%ju\n", + (uintmax_t) nt_cu_length); + + if (dwarf_get_globals(re->dbg, &globs, &cnt, &de) != DW_DLV_OK) { + warnx("dwarf_get_globals failed: %s", dwarf_errmsg(de)); + return; + } + + printf("\n Offset Name\n"); + for (i = 0; i < cnt; i++) { + if (dwarf_globname(globs[i], &glob_name, &de) != DW_DLV_OK) { + warnx("dwarf_globname failed: %s", dwarf_errmsg(de)); + continue; + } + if (dwarf_global_die_offset(globs[i], &die_off, &de) != + DW_DLV_OK) { + warnx("dwarf_global_die_offset failed: %s", + dwarf_errmsg(de)); + continue; + } + printf(" %-11ju %s\n", (uintmax_t) die_off, glob_name); + } +} + +static void +dump_dwarf_aranges(struct readelf *re) +{ + struct section *s; + Dwarf_Arange *aranges; + Dwarf_Addr start; + Dwarf_Unsigned offset, length, as_cu_offset; + Dwarf_Off die_off; + Dwarf_Signed cnt; + Dwarf_Half as_version, as_addrsz, as_segsz; + Dwarf_Error de; + Elf_Data *d; + int i, dwarf_size, elferr; + + printf("\nContents of section .debug_aranges:\n"); + + s = NULL; + for (i = 0; (size_t) i < re->shnum; i++) { + s = &re->sl[i]; + if (s->name != NULL && !strcmp(s->name, ".debug_aranges")) + break; + } + if ((size_t) i >= re->shnum) + return; + + (void) elf_errno(); + if ((d = elf_getdata(s->scn, NULL)) == NULL) { + elferr = elf_errno(); + if (elferr != 0) + warnx("elf_getdata failed: %s", elf_errmsg(-1)); + return; + } + if (d->d_size <= 0) + return; + + /* Read in the .debug_aranges section table header. */ + offset = 0; + length = re->dw_read(d, &offset, 4); + if (length == 0xffffffff) { + dwarf_size = 8; + length = re->dw_read(d, &offset, 8); + } else + dwarf_size = 4; + + if (length > d->d_size - offset) { + warnx("invalid .dwarf_aranges section"); + return; + } + + as_version = re->dw_read(d, &offset, 2); + as_cu_offset = re->dw_read(d, &offset, dwarf_size); + as_addrsz = re->dw_read(d, &offset, 1); + as_segsz = re->dw_read(d, &offset, 1); + + printf(" Length:\t\t\t%ju\n", (uintmax_t) length); + printf(" Version:\t\t\t%u\n", as_version); + printf(" Offset into .debug_info:\t%ju\n", (uintmax_t) as_cu_offset); + printf(" Pointer Size:\t\t\t%u\n", as_addrsz); + printf(" Segment Size:\t\t\t%u\n", as_segsz); + + if (dwarf_get_aranges(re->dbg, &aranges, &cnt, &de) != DW_DLV_OK) { + warnx("dwarf_get_aranges failed: %s", dwarf_errmsg(de)); + return; + } + + printf("\n Address Length\n"); + for (i = 0; i < cnt; i++) { + if (dwarf_get_arange_info(aranges[i], &start, &length, + &die_off, &de) != DW_DLV_OK) { + warnx("dwarf_get_arange_info failed: %s", + dwarf_errmsg(de)); + continue; + } + printf(" %08jx %ju\n", (uintmax_t) start, + (uintmax_t) length); + } +} + +static void +dump_dwarf_ranges_foreach(struct readelf *re, Dwarf_Die die, Dwarf_Addr base) +{ + Dwarf_Attribute *attr_list; + Dwarf_Ranges *ranges; + Dwarf_Die ret_die; + Dwarf_Error de; + Dwarf_Addr base0; + Dwarf_Half attr; + Dwarf_Signed attr_count, cnt; + Dwarf_Unsigned off, bytecnt; + int i, j, ret; + + if ((ret = dwarf_attrlist(die, &attr_list, &attr_count, &de)) != + DW_DLV_OK) { + if (ret == DW_DLV_ERROR) + warnx("dwarf_attrlist failed: %s", dwarf_errmsg(de)); + goto cont_search; + } + + for (i = 0; i < attr_count; i++) { + if (dwarf_whatattr(attr_list[i], &attr, &de) != DW_DLV_OK) { + warnx("dwarf_whatattr failed: %s", dwarf_errmsg(de)); + continue; + } + if (attr != DW_AT_ranges) + continue; + if (dwarf_formudata(attr_list[i], &off, &de) != DW_DLV_OK) { + warnx("dwarf_formudata failed: %s", dwarf_errmsg(de)); + continue; + } + if (dwarf_get_ranges(re->dbg, (Dwarf_Off) off, &ranges, &cnt, + &bytecnt, &de) != DW_DLV_OK) + continue; + base0 = base; + for (j = 0; j < cnt; j++) { + printf(" %08jx ", (uintmax_t) off); + if (ranges[j].dwr_type == DW_RANGES_END) { + printf("%s\n", "<End of list>"); + continue; + } else if (ranges[j].dwr_type == + DW_RANGES_ADDRESS_SELECTION) { + base0 = ranges[j].dwr_addr2; + continue; + } + if (re->ec == ELFCLASS32) + printf("%08jx %08jx\n", + ranges[j].dwr_addr1 + base0, + ranges[j].dwr_addr2 + base0); + else + printf("%016jx %016jx\n", + ranges[j].dwr_addr1 + base0, + ranges[j].dwr_addr2 + base0); + } + } + +cont_search: + /* Search children. */ + ret = dwarf_child(die, &ret_die, &de); + if (ret == DW_DLV_ERROR) + warnx("dwarf_child: %s", dwarf_errmsg(de)); + else if (ret == DW_DLV_OK) + dump_dwarf_ranges_foreach(re, ret_die, base); + + /* Search sibling. */ + ret = dwarf_siblingof(re->dbg, die, &ret_die, &de); + if (ret == DW_DLV_ERROR) + warnx("dwarf_siblingof: %s", dwarf_errmsg(de)); + else if (ret == DW_DLV_OK) + dump_dwarf_ranges_foreach(re, ret_die, base); +} + +static void +dump_dwarf_ranges(struct readelf *re) +{ + Dwarf_Ranges *ranges; + Dwarf_Die die; + Dwarf_Signed cnt; + Dwarf_Unsigned bytecnt; + Dwarf_Half tag; + Dwarf_Error de; + Dwarf_Unsigned lowpc; + int ret; + + if (dwarf_get_ranges(re->dbg, 0, &ranges, &cnt, &bytecnt, &de) != + DW_DLV_OK) + return; + + printf("Contents of the .debug_ranges section:\n\n"); + if (re->ec == ELFCLASS32) + printf(" %-8s %-8s %s\n", "Offset", "Begin", "End"); + else + printf(" %-8s %-16s %s\n", "Offset", "Begin", "End"); + + while ((ret = dwarf_next_cu_header(re->dbg, NULL, NULL, NULL, NULL, + NULL, &de)) == DW_DLV_OK) { + die = NULL; + if (dwarf_siblingof(re->dbg, die, &die, &de) != DW_DLV_OK) + continue; + if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) { + warnx("dwarf_tag failed: %s", dwarf_errmsg(de)); + continue; + } + /* XXX: What about DW_TAG_partial_unit? */ + lowpc = 0; + if (tag == DW_TAG_compile_unit) { + if (dwarf_attrval_unsigned(die, DW_AT_low_pc, &lowpc, + &de) != DW_DLV_OK) + lowpc = 0; + } + + dump_dwarf_ranges_foreach(re, die, (Dwarf_Addr) lowpc); + } + putchar('\n'); +} + +static void +dump_dwarf_macinfo(struct readelf *re) +{ + Dwarf_Unsigned offset; + Dwarf_Signed cnt; + Dwarf_Macro_Details *md; + Dwarf_Error de; + const char *mi_str; + char unk_mi[32]; + int i; + +#define _MAX_MACINFO_ENTRY 65535 + + printf("\nContents of section .debug_macinfo:\n\n"); + + offset = 0; + while (dwarf_get_macro_details(re->dbg, offset, _MAX_MACINFO_ENTRY, + &cnt, &md, &de) == DW_DLV_OK) { + for (i = 0; i < cnt; i++) { + offset = md[i].dmd_offset + 1; + if (md[i].dmd_type == 0) + break; + if (dwarf_get_MACINFO_name(md[i].dmd_type, &mi_str) != + DW_DLV_OK) { + snprintf(unk_mi, sizeof(unk_mi), + "[Unknown MACINFO: %#x]", md[i].dmd_type); + mi_str = unk_mi; + } + printf(" %s", mi_str); + switch (md[i].dmd_type) { + case DW_MACINFO_define: + case DW_MACINFO_undef: + printf(" - lineno : %jd macro : %s\n", + (intmax_t) md[i].dmd_lineno, + md[i].dmd_macro); + break; + case DW_MACINFO_start_file: + printf(" - lineno : %jd filenum : %jd\n", + (intmax_t) md[i].dmd_lineno, + (intmax_t) md[i].dmd_fileindex); + break; + default: + putchar('\n'); + break; + } + } + } + +#undef _MAX_MACINFO_ENTRY +} + +static void +dump_dwarf_frame_inst(struct readelf *re, Dwarf_Cie cie, uint8_t *insts, + Dwarf_Unsigned len, Dwarf_Unsigned caf, Dwarf_Signed daf, Dwarf_Addr pc, + Dwarf_Debug dbg) +{ + Dwarf_Frame_Op *oplist; + Dwarf_Signed opcnt, delta; + Dwarf_Small op; + Dwarf_Error de; + const char *op_str; + char unk_op[32]; + int i; + + if (dwarf_expand_frame_instructions(cie, insts, len, &oplist, + &opcnt, &de) != DW_DLV_OK) { + warnx("dwarf_expand_frame_instructions failed: %s", + dwarf_errmsg(de)); + return; + } + + for (i = 0; i < opcnt; i++) { + if (oplist[i].fp_base_op != 0) + op = oplist[i].fp_base_op << 6; + else + op = oplist[i].fp_extended_op; + if (dwarf_get_CFA_name(op, &op_str) != DW_DLV_OK) { + snprintf(unk_op, sizeof(unk_op), "[Unknown CFA: %#x]", + op); + op_str = unk_op; + } + printf(" %s", op_str); + switch (op) { + case DW_CFA_advance_loc: + delta = oplist[i].fp_offset * caf; + pc += delta; + printf(": %ju to %08jx", (uintmax_t) delta, + (uintmax_t) pc); + break; + case DW_CFA_offset: + case DW_CFA_offset_extended: + case DW_CFA_offset_extended_sf: + delta = oplist[i].fp_offset * daf; + printf(": r%u (%s) at cfa%+jd", oplist[i].fp_register, + dwarf_regname(re, oplist[i].fp_register), + (intmax_t) delta); + break; + case DW_CFA_restore: + printf(": r%u (%s)", oplist[i].fp_register, + dwarf_regname(re, oplist[i].fp_register)); + break; + case DW_CFA_set_loc: + pc = oplist[i].fp_offset; + printf(": to %08jx", (uintmax_t) pc); + break; + case DW_CFA_advance_loc1: + case DW_CFA_advance_loc2: + case DW_CFA_advance_loc4: + pc += oplist[i].fp_offset; + printf(": %jd to %08jx", (intmax_t) oplist[i].fp_offset, + (uintmax_t) pc); + break; + case DW_CFA_def_cfa: + printf(": r%u (%s) ofs %ju", oplist[i].fp_register, + dwarf_regname(re, oplist[i].fp_register), + (uintmax_t) oplist[i].fp_offset); + break; + case DW_CFA_def_cfa_sf: + printf(": r%u (%s) ofs %jd", oplist[i].fp_register, + dwarf_regname(re, oplist[i].fp_register), + (intmax_t) (oplist[i].fp_offset * daf)); + break; + case DW_CFA_def_cfa_register: + printf(": r%u (%s)", oplist[i].fp_register, + dwarf_regname(re, oplist[i].fp_register)); + break; + case DW_CFA_def_cfa_offset: + printf(": %ju", (uintmax_t) oplist[i].fp_offset); + break; + case DW_CFA_def_cfa_offset_sf: + printf(": %jd", (intmax_t) (oplist[i].fp_offset * daf)); + break; + default: + break; + } + putchar('\n'); + } + + dwarf_dealloc(dbg, oplist, DW_DLA_FRAME_BLOCK); +} + +static char * +get_regoff_str(struct readelf *re, Dwarf_Half reg, Dwarf_Addr off) +{ + static char rs[16]; + + if (reg == DW_FRAME_UNDEFINED_VAL || reg == DW_FRAME_REG_INITIAL_VALUE) + snprintf(rs, sizeof(rs), "%c", 'u'); + else if (reg == DW_FRAME_CFA_COL) + snprintf(rs, sizeof(rs), "c%+jd", (intmax_t) off); + else + snprintf(rs, sizeof(rs), "%s%+jd", dwarf_regname(re, reg), + (intmax_t) off); + + return (rs); +} + +static int +dump_dwarf_frame_regtable(struct readelf *re, Dwarf_Fde fde, Dwarf_Addr pc, + Dwarf_Unsigned func_len, Dwarf_Half cie_ra) +{ + Dwarf_Regtable rt; + Dwarf_Addr row_pc, end_pc, pre_pc, cur_pc; + Dwarf_Error de; + char *vec; + int i; + +#define BIT_SET(v, n) (v[(n)>>3] |= 1U << ((n) & 7)) +#define BIT_CLR(v, n) (v[(n)>>3] &= ~(1U << ((n) & 7))) +#define BIT_ISSET(v, n) (v[(n)>>3] & (1U << ((n) & 7))) +#define RT(x) rt.rules[(x)] + + vec = calloc((DW_REG_TABLE_SIZE + 7) / 8, 1); + if (vec == NULL) + err(EXIT_FAILURE, "calloc failed"); + + pre_pc = ~((Dwarf_Addr) 0); + cur_pc = pc; + end_pc = pc + func_len; + for (; cur_pc < end_pc; cur_pc++) { + if (dwarf_get_fde_info_for_all_regs(fde, cur_pc, &rt, &row_pc, + &de) != DW_DLV_OK) { + warnx("dwarf_get_fde_info_for_all_regs failed: %s\n", + dwarf_errmsg(de)); + return (-1); + } + if (row_pc == pre_pc) + continue; + pre_pc = row_pc; + for (i = 1; i < DW_REG_TABLE_SIZE; i++) { + if (rt.rules[i].dw_regnum != DW_FRAME_REG_INITIAL_VALUE) + BIT_SET(vec, i); + } + } + + printf(" LOC CFA "); + for (i = 1; i < DW_REG_TABLE_SIZE; i++) { + if (BIT_ISSET(vec, i)) { + if ((Dwarf_Half) i == cie_ra) + printf("ra "); + else + printf("%-5s", + dwarf_regname(re, (unsigned int) i)); + } + } + putchar('\n'); + + pre_pc = ~((Dwarf_Addr) 0); + cur_pc = pc; + end_pc = pc + func_len; + for (; cur_pc < end_pc; cur_pc++) { + if (dwarf_get_fde_info_for_all_regs(fde, cur_pc, &rt, &row_pc, + &de) != DW_DLV_OK) { + warnx("dwarf_get_fde_info_for_all_regs failed: %s\n", + dwarf_errmsg(de)); + return (-1); + } + if (row_pc == pre_pc) + continue; + pre_pc = row_pc; + printf("%08jx ", (uintmax_t) row_pc); + printf("%-8s ", get_regoff_str(re, RT(0).dw_regnum, + RT(0).dw_offset)); + for (i = 1; i < DW_REG_TABLE_SIZE; i++) { + if (BIT_ISSET(vec, i)) { + printf("%-5s", get_regoff_str(re, + RT(i).dw_regnum, RT(i).dw_offset)); + } + } + putchar('\n'); + } + + free(vec); + + return (0); + +#undef BIT_SET +#undef BIT_CLR +#undef BIT_ISSET +#undef RT +} + +static void +dump_dwarf_frame_section(struct readelf *re, struct section *s, int alt) +{ + Dwarf_Cie *cie_list, cie, pre_cie; + Dwarf_Fde *fde_list, fde; + Dwarf_Off cie_offset, fde_offset; + Dwarf_Unsigned cie_length, fde_instlen; + Dwarf_Unsigned cie_caf, cie_daf, cie_instlen, func_len, fde_length; + Dwarf_Signed cie_count, fde_count, cie_index; + Dwarf_Addr low_pc; + Dwarf_Half cie_ra; + Dwarf_Small cie_version; + Dwarf_Ptr fde_addr, fde_inst, cie_inst; + char *cie_aug, c; + int i, eh_frame; + Dwarf_Error de; + + printf("\nThe section %s contains:\n\n", s->name); + + if (!strcmp(s->name, ".debug_frame")) { + eh_frame = 0; + if (dwarf_get_fde_list(re->dbg, &cie_list, &cie_count, + &fde_list, &fde_count, &de) != DW_DLV_OK) { + warnx("dwarf_get_fde_list failed: %s", + dwarf_errmsg(de)); + return; + } + } else if (!strcmp(s->name, ".eh_frame")) { + eh_frame = 1; + if (dwarf_get_fde_list_eh(re->dbg, &cie_list, &cie_count, + &fde_list, &fde_count, &de) != DW_DLV_OK) { + warnx("dwarf_get_fde_list_eh failed: %s", + dwarf_errmsg(de)); + return; + } + } else + return; + + pre_cie = NULL; + for (i = 0; i < fde_count; i++) { + if (dwarf_get_fde_n(fde_list, i, &fde, &de) != DW_DLV_OK) { + warnx("dwarf_get_fde_n failed: %s", dwarf_errmsg(de)); + continue; + } + if (dwarf_get_cie_of_fde(fde, &cie, &de) != DW_DLV_OK) { + warnx("dwarf_get_fde_n failed: %s", dwarf_errmsg(de)); + continue; + } + if (dwarf_get_fde_range(fde, &low_pc, &func_len, &fde_addr, + &fde_length, &cie_offset, &cie_index, &fde_offset, + &de) != DW_DLV_OK) { + warnx("dwarf_get_fde_range failed: %s", + dwarf_errmsg(de)); + continue; + } + if (dwarf_get_fde_instr_bytes(fde, &fde_inst, &fde_instlen, + &de) != DW_DLV_OK) { + warnx("dwarf_get_fde_instr_bytes failed: %s", + dwarf_errmsg(de)); + continue; + } + if (pre_cie == NULL || cie != pre_cie) { + pre_cie = cie; + if (dwarf_get_cie_info(cie, &cie_length, &cie_version, + &cie_aug, &cie_caf, &cie_daf, &cie_ra, + &cie_inst, &cie_instlen, &de) != DW_DLV_OK) { + warnx("dwarf_get_cie_info failed: %s", + dwarf_errmsg(de)); + continue; + } + printf("%08jx %08jx %8.8jx CIE", + (uintmax_t) cie_offset, + (uintmax_t) cie_length, + (uintmax_t) (eh_frame ? 0 : ~0U)); + if (!alt) { + putchar('\n'); + printf(" Version:\t\t\t%u\n", cie_version); + printf(" Augmentation:\t\t\t\""); + while ((c = *cie_aug++) != '\0') + putchar(c); + printf("\"\n"); + printf(" Code alignment factor:\t%ju\n", + (uintmax_t) cie_caf); + printf(" Data alignment factor:\t%jd\n", + (intmax_t) cie_daf); + printf(" Return address column:\t%ju\n", + (uintmax_t) cie_ra); + putchar('\n'); + dump_dwarf_frame_inst(re, cie, cie_inst, + cie_instlen, cie_caf, cie_daf, 0, + re->dbg); + putchar('\n'); + } else { + printf(" \""); + while ((c = *cie_aug++) != '\0') + putchar(c); + putchar('"'); + printf(" cf=%ju df=%jd ra=%ju\n", + (uintmax_t) cie_caf, + (uintmax_t) cie_daf, + (uintmax_t) cie_ra); + dump_dwarf_frame_regtable(re, fde, low_pc, 1, + cie_ra); + putchar('\n'); + } + } + printf("%08jx %08jx %08jx FDE cie=%08jx pc=%08jx..%08jx\n", + (uintmax_t) fde_offset, (uintmax_t) fde_length, + (uintmax_t) cie_offset, + (uintmax_t) (eh_frame ? fde_offset + 4 - cie_offset : + cie_offset), + (uintmax_t) low_pc, (uintmax_t) (low_pc + func_len)); + if (!alt) + dump_dwarf_frame_inst(re, cie, fde_inst, fde_instlen, + cie_caf, cie_daf, low_pc, re->dbg); + else + dump_dwarf_frame_regtable(re, fde, low_pc, func_len, + cie_ra); + putchar('\n'); + } +} + +static void +dump_dwarf_frame(struct readelf *re, int alt) +{ + struct section *s; + int i; + + (void) dwarf_set_frame_cfa_value(re->dbg, DW_FRAME_CFA_COL); + + for (i = 0; (size_t) i < re->shnum; i++) { + s = &re->sl[i]; + if (s->name != NULL && (!strcmp(s->name, ".debug_frame") || + !strcmp(s->name, ".eh_frame"))) + dump_dwarf_frame_section(re, s, alt); + } +} + +static void +dump_dwarf_str(struct readelf *re) +{ + struct section *s; + Elf_Data *d; + unsigned char *p; + int elferr, end, i, j; + + printf("\nContents of section .debug_str:\n"); + + s = NULL; + for (i = 0; (size_t) i < re->shnum; i++) { + s = &re->sl[i]; + if (s->name != NULL && !strcmp(s->name, ".debug_str")) + break; + } + if ((size_t) i >= re->shnum) + return; + + (void) elf_errno(); + if ((d = elf_getdata(s->scn, NULL)) == NULL) { + elferr = elf_errno(); + if (elferr != 0) + warnx("elf_getdata failed: %s", elf_errmsg(-1)); + return; + } + if (d->d_size <= 0) + return; + + for (i = 0, p = d->d_buf; (size_t) i < d->d_size; i += 16) { + printf(" 0x%08x", (unsigned int) i); + if ((size_t) i + 16 > d->d_size) + end = d->d_size; + else + end = i + 16; + for (j = i; j < i + 16; j++) { + if ((j - i) % 4 == 0) + putchar(' '); + if (j >= end) { + printf(" "); + continue; + } + printf("%02x", (uint8_t) p[j]); + } + putchar(' '); + for (j = i; j < end; j++) { + if (isprint(p[j])) + putchar(p[j]); + else if (p[j] == 0) + putchar('.'); + else + putchar(' '); + } + putchar('\n'); + } +} + +struct loc_at { + Dwarf_Attribute la_at; + Dwarf_Unsigned la_off; + Dwarf_Unsigned la_lowpc; + Dwarf_Half la_cu_psize; + Dwarf_Half la_cu_osize; + Dwarf_Half la_cu_ver; + TAILQ_ENTRY(loc_at) la_next; +}; + +static TAILQ_HEAD(, loc_at) lalist = TAILQ_HEAD_INITIALIZER(lalist); + +static void +search_loclist_at(struct readelf *re, Dwarf_Die die, Dwarf_Unsigned lowpc) +{ + Dwarf_Attribute *attr_list; + Dwarf_Die ret_die; + Dwarf_Unsigned off; + Dwarf_Off ref; + Dwarf_Signed attr_count; + Dwarf_Half attr, form; + Dwarf_Bool is_info; + Dwarf_Error de; + struct loc_at *la, *nla; + int i, ret; + + is_info = dwarf_get_die_infotypes_flag(die); + + if ((ret = dwarf_attrlist(die, &attr_list, &attr_count, &de)) != + DW_DLV_OK) { + if (ret == DW_DLV_ERROR) + warnx("dwarf_attrlist failed: %s", dwarf_errmsg(de)); + goto cont_search; + } + for (i = 0; i < attr_count; i++) { + if (dwarf_whatattr(attr_list[i], &attr, &de) != DW_DLV_OK) { + warnx("dwarf_whatattr failed: %s", dwarf_errmsg(de)); + continue; + } + if (attr != DW_AT_location && + attr != DW_AT_string_length && + attr != DW_AT_return_addr && + attr != DW_AT_data_member_location && + attr != DW_AT_frame_base && + attr != DW_AT_segment && + attr != DW_AT_static_link && + attr != DW_AT_use_location && + attr != DW_AT_vtable_elem_location) + continue; + if (dwarf_whatform(attr_list[i], &form, &de) != DW_DLV_OK) { + warnx("dwarf_whatform failed: %s", dwarf_errmsg(de)); + continue; + } + if (form == DW_FORM_data4 || form == DW_FORM_data8) { + if (dwarf_formudata(attr_list[i], &off, &de) != + DW_DLV_OK) { + warnx("dwarf_formudata failed: %s", + dwarf_errmsg(de)); + continue; + } + } else if (form == DW_FORM_sec_offset) { + if (dwarf_global_formref(attr_list[i], &ref, &de) != + DW_DLV_OK) { + warnx("dwarf_global_formref failed: %s", + dwarf_errmsg(de)); + continue; + } + off = ref; + } else + continue; + + TAILQ_FOREACH(la, &lalist, la_next) { + if (off == la->la_off) + break; + if (off < la->la_off) { + if ((nla = malloc(sizeof(*nla))) == NULL) + err(EXIT_FAILURE, "malloc failed"); + nla->la_at = attr_list[i]; + nla->la_off = off; + nla->la_lowpc = lowpc; + nla->la_cu_psize = re->cu_psize; + nla->la_cu_osize = re->cu_osize; + nla->la_cu_ver = re->cu_ver; + TAILQ_INSERT_BEFORE(la, nla, la_next); + break; + } + } + if (la == NULL) { + if ((nla = malloc(sizeof(*nla))) == NULL) + err(EXIT_FAILURE, "malloc failed"); + nla->la_at = attr_list[i]; + nla->la_off = off; + nla->la_lowpc = lowpc; + nla->la_cu_psize = re->cu_psize; + nla->la_cu_osize = re->cu_osize; + nla->la_cu_ver = re->cu_ver; + TAILQ_INSERT_TAIL(&lalist, nla, la_next); + } + } + +cont_search: + /* Search children. */ + ret = dwarf_child(die, &ret_die, &de); + if (ret == DW_DLV_ERROR) + warnx("dwarf_child: %s", dwarf_errmsg(de)); + else if (ret == DW_DLV_OK) + search_loclist_at(re, ret_die, lowpc); + + /* Search sibling. */ + ret = dwarf_siblingof_b(re->dbg, die, &ret_die, is_info, &de); + if (ret == DW_DLV_ERROR) + warnx("dwarf_siblingof: %s", dwarf_errmsg(de)); + else if (ret == DW_DLV_OK) + search_loclist_at(re, ret_die, lowpc); +} + +static void +dump_dwarf_loc(struct readelf *re, Dwarf_Loc *lr) +{ + const char *op_str; + char unk_op[32]; + uint8_t *b, n; + int i; + + if (dwarf_get_OP_name(lr->lr_atom, &op_str) != + DW_DLV_OK) { + snprintf(unk_op, sizeof(unk_op), + "[Unknown OP: %#x]", lr->lr_atom); + op_str = unk_op; + } + + printf("%s", op_str); + + switch (lr->lr_atom) { + case DW_OP_reg0: + case DW_OP_reg1: + case DW_OP_reg2: + case DW_OP_reg3: + case DW_OP_reg4: + case DW_OP_reg5: + case DW_OP_reg6: + case DW_OP_reg7: + case DW_OP_reg8: + case DW_OP_reg9: + case DW_OP_reg10: + case DW_OP_reg11: + case DW_OP_reg12: + case DW_OP_reg13: + case DW_OP_reg14: + case DW_OP_reg15: + case DW_OP_reg16: + case DW_OP_reg17: + case DW_OP_reg18: + case DW_OP_reg19: + case DW_OP_reg20: + case DW_OP_reg21: + case DW_OP_reg22: + case DW_OP_reg23: + case DW_OP_reg24: + case DW_OP_reg25: + case DW_OP_reg26: + case DW_OP_reg27: + case DW_OP_reg28: + case DW_OP_reg29: + case DW_OP_reg30: + case DW_OP_reg31: + printf(" (%s)", dwarf_regname(re, lr->lr_atom - DW_OP_reg0)); + break; + + case DW_OP_deref: + case DW_OP_lit0: + case DW_OP_lit1: + case DW_OP_lit2: + case DW_OP_lit3: + case DW_OP_lit4: + case DW_OP_lit5: + case DW_OP_lit6: + case DW_OP_lit7: + case DW_OP_lit8: + case DW_OP_lit9: + case DW_OP_lit10: + case DW_OP_lit11: + case DW_OP_lit12: + case DW_OP_lit13: + case DW_OP_lit14: + case DW_OP_lit15: + case DW_OP_lit16: + case DW_OP_lit17: + case DW_OP_lit18: + case DW_OP_lit19: + case DW_OP_lit20: + case DW_OP_lit21: + case DW_OP_lit22: + case DW_OP_lit23: + case DW_OP_lit24: + case DW_OP_lit25: + case DW_OP_lit26: + case DW_OP_lit27: + case DW_OP_lit28: + case DW_OP_lit29: + case DW_OP_lit30: + case DW_OP_lit31: + case DW_OP_dup: + case DW_OP_drop: + case DW_OP_over: + case DW_OP_swap: + case DW_OP_rot: + case DW_OP_xderef: + case DW_OP_abs: + case DW_OP_and: + case DW_OP_div: + case DW_OP_minus: + case DW_OP_mod: + case DW_OP_mul: + case DW_OP_neg: + case DW_OP_not: + case DW_OP_or: + case DW_OP_plus: + case DW_OP_shl: + case DW_OP_shr: + case DW_OP_shra: + case DW_OP_xor: + case DW_OP_eq: + case DW_OP_ge: + case DW_OP_gt: + case DW_OP_le: + case DW_OP_lt: + case DW_OP_ne: + case DW_OP_nop: + case DW_OP_push_object_address: + case DW_OP_form_tls_address: + case DW_OP_call_frame_cfa: + case DW_OP_stack_value: + case DW_OP_GNU_push_tls_address: + case DW_OP_GNU_uninit: + break; + + case DW_OP_const1u: + case DW_OP_pick: + case DW_OP_deref_size: + case DW_OP_xderef_size: + case DW_OP_const2u: + case DW_OP_bra: + case DW_OP_skip: + case DW_OP_const4u: + case DW_OP_const8u: + case DW_OP_constu: + case DW_OP_plus_uconst: + case DW_OP_regx: + case DW_OP_piece: + printf(": %ju", (uintmax_t) + lr->lr_number); + break; + + case DW_OP_const1s: + case DW_OP_const2s: + case DW_OP_const4s: + case DW_OP_const8s: + case DW_OP_consts: + printf(": %jd", (intmax_t) + lr->lr_number); + break; + + case DW_OP_breg0: + case DW_OP_breg1: + case DW_OP_breg2: + case DW_OP_breg3: + case DW_OP_breg4: + case DW_OP_breg5: + case DW_OP_breg6: + case DW_OP_breg7: + case DW_OP_breg8: + case DW_OP_breg9: + case DW_OP_breg10: + case DW_OP_breg11: + case DW_OP_breg12: + case DW_OP_breg13: + case DW_OP_breg14: + case DW_OP_breg15: + case DW_OP_breg16: + case DW_OP_breg17: + case DW_OP_breg18: + case DW_OP_breg19: + case DW_OP_breg20: + case DW_OP_breg21: + case DW_OP_breg22: + case DW_OP_breg23: + case DW_OP_breg24: + case DW_OP_breg25: + case DW_OP_breg26: + case DW_OP_breg27: + case DW_OP_breg28: + case DW_OP_breg29: + case DW_OP_breg30: + case DW_OP_breg31: + printf(" (%s): %jd", + dwarf_regname(re, lr->lr_atom - DW_OP_breg0), + (intmax_t) lr->lr_number); + break; + + case DW_OP_fbreg: + printf(": %jd", (intmax_t) + lr->lr_number); + break; + + case DW_OP_bregx: + printf(": %ju (%s) %jd", + (uintmax_t) lr->lr_number, + dwarf_regname(re, (unsigned int) lr->lr_number), + (intmax_t) lr->lr_number2); + break; + + case DW_OP_addr: + case DW_OP_GNU_encoded_addr: + printf(": %#jx", (uintmax_t) + lr->lr_number); + break; + + case DW_OP_GNU_implicit_pointer: + printf(": <0x%jx> %jd", (uintmax_t) lr->lr_number, + (intmax_t) lr->lr_number2); + break; + + case DW_OP_implicit_value: + printf(": %ju byte block:", (uintmax_t) lr->lr_number); + b = (uint8_t *)(uintptr_t) lr->lr_number2; + for (i = 0; (Dwarf_Unsigned) i < lr->lr_number; i++) + printf(" %x", b[i]); + break; + + case DW_OP_GNU_entry_value: + printf(": ("); + dump_dwarf_block(re, (uint8_t *)(uintptr_t) lr->lr_number2, + lr->lr_number); + putchar(')'); + break; + + case DW_OP_GNU_const_type: + printf(": <0x%jx> ", (uintmax_t) lr->lr_number); + b = (uint8_t *)(uintptr_t) lr->lr_number2; + n = *b; + for (i = 1; (uint8_t) i < n; i++) + printf(" %x", b[i]); + break; + + case DW_OP_GNU_regval_type: + printf(": %ju (%s) <0x%jx>", (uintmax_t) lr->lr_number, + dwarf_regname(re, (unsigned int) lr->lr_number), + (uintmax_t) lr->lr_number2); + break; + + case DW_OP_GNU_convert: + case DW_OP_GNU_deref_type: + case DW_OP_GNU_parameter_ref: + case DW_OP_GNU_reinterpret: + printf(": <0x%jx>", (uintmax_t) lr->lr_number); + break; + + default: + break; + } +} + +static void +dump_dwarf_block(struct readelf *re, uint8_t *b, Dwarf_Unsigned len) +{ + Dwarf_Locdesc *llbuf; + Dwarf_Signed lcnt; + Dwarf_Error de; + int i; + + if (dwarf_loclist_from_expr_b(re->dbg, b, len, re->cu_psize, + re->cu_osize, re->cu_ver, &llbuf, &lcnt, &de) != DW_DLV_OK) { + warnx("dwarf_loclist_form_expr_b: %s", dwarf_errmsg(de)); + return; + } + + for (i = 0; (Dwarf_Half) i < llbuf->ld_cents; i++) { + dump_dwarf_loc(re, &llbuf->ld_s[i]); + if (i < llbuf->ld_cents - 1) + printf("; "); + } + + dwarf_dealloc(re->dbg, llbuf->ld_s, DW_DLA_LOC_BLOCK); + dwarf_dealloc(re->dbg, llbuf, DW_DLA_LOCDESC); +} + +static void +dump_dwarf_loclist(struct readelf *re) +{ + Dwarf_Die die; + Dwarf_Locdesc **llbuf; + Dwarf_Unsigned lowpc; + Dwarf_Signed lcnt; + Dwarf_Half tag, version, pointer_size, off_size; + Dwarf_Error de; + struct loc_at *la; + int i, j, ret; + + printf("\nContents of section .debug_loc:\n"); + + /* Search .debug_info section. */ + while ((ret = dwarf_next_cu_header_b(re->dbg, NULL, &version, NULL, + &pointer_size, &off_size, NULL, NULL, &de)) == DW_DLV_OK) { + set_cu_context(re, pointer_size, off_size, version); + die = NULL; + if (dwarf_siblingof(re->dbg, die, &die, &de) != DW_DLV_OK) + continue; + if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) { + warnx("dwarf_tag failed: %s", dwarf_errmsg(de)); + continue; + } + /* XXX: What about DW_TAG_partial_unit? */ + lowpc = 0; + if (tag == DW_TAG_compile_unit) { + if (dwarf_attrval_unsigned(die, DW_AT_low_pc, + &lowpc, &de) != DW_DLV_OK) + lowpc = 0; + } + + /* Search attributes for reference to .debug_loc section. */ + search_loclist_at(re, die, lowpc); + } + if (ret == DW_DLV_ERROR) + warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de)); + + /* Search .debug_types section. */ + do { + while ((ret = dwarf_next_cu_header_c(re->dbg, 0, NULL, + &version, NULL, &pointer_size, &off_size, NULL, NULL, + NULL, NULL, &de)) == DW_DLV_OK) { + set_cu_context(re, pointer_size, off_size, version); + die = NULL; + if (dwarf_siblingof(re->dbg, die, &die, &de) != + DW_DLV_OK) + continue; + if (dwarf_tag(die, &tag, &de) != DW_DLV_OK) { + warnx("dwarf_tag failed: %s", + dwarf_errmsg(de)); + continue; + } + + lowpc = 0; + if (tag == DW_TAG_type_unit) { + if (dwarf_attrval_unsigned(die, DW_AT_low_pc, + &lowpc, &de) != DW_DLV_OK) + lowpc = 0; + } + + /* + * Search attributes for reference to .debug_loc + * section. + */ + search_loclist_at(re, die, lowpc); + } + if (ret == DW_DLV_ERROR) + warnx("dwarf_next_cu_header: %s", dwarf_errmsg(de)); + } while (dwarf_next_types_section(re->dbg, &de) == DW_DLV_OK); + + if (TAILQ_EMPTY(&lalist)) + return; + + printf(" Offset Begin End Expression\n"); + + TAILQ_FOREACH(la, &lalist, la_next) { + if (dwarf_loclist_n(la->la_at, &llbuf, &lcnt, &de) != + DW_DLV_OK) { + warnx("dwarf_loclist_n failed: %s", dwarf_errmsg(de)); + continue; + } + set_cu_context(re, la->la_cu_psize, la->la_cu_osize, + la->la_cu_ver); + for (i = 0; i < lcnt; i++) { + printf(" %8.8jx ", la->la_off); + if (llbuf[i]->ld_lopc == 0 && llbuf[i]->ld_hipc == 0) { + printf("<End of list>\n"); + continue; + } + + /* TODO: handle base selection entry. */ + + printf("%8.8jx %8.8jx ", + (uintmax_t) (la->la_lowpc + llbuf[i]->ld_lopc), + (uintmax_t) (la->la_lowpc + llbuf[i]->ld_hipc)); + + putchar('('); + for (j = 0; (Dwarf_Half) j < llbuf[i]->ld_cents; j++) { + dump_dwarf_loc(re, &llbuf[i]->ld_s[j]); + if (j < llbuf[i]->ld_cents - 1) + printf("; "); + } + putchar(')'); + + if (llbuf[i]->ld_lopc == llbuf[i]->ld_hipc) + printf(" (start == end)"); + putchar('\n'); + } + for (i = 0; i < lcnt; i++) { + dwarf_dealloc(re->dbg, llbuf[i]->ld_s, + DW_DLA_LOC_BLOCK); + dwarf_dealloc(re->dbg, llbuf[i], DW_DLA_LOCDESC); + } + dwarf_dealloc(re->dbg, llbuf, DW_DLA_LIST); + } +} + +/* + * Retrieve a string using string table section index and the string offset. + */ +static const char* +get_string(struct readelf *re, int strtab, size_t off) +{ + const char *name; + + if ((name = elf_strptr(re->elf, strtab, off)) == NULL) + return (""); + + return (name); +} + +/* + * Retrieve the name of a symbol using the section index of the symbol + * table and the index of the symbol within that table. + */ +static const char * +get_symbol_name(struct readelf *re, int symtab, int i) +{ + struct section *s; + const char *name; + GElf_Sym sym; + Elf_Data *data; + int elferr; + + s = &re->sl[symtab]; + if (s->type != SHT_SYMTAB && s->type != SHT_DYNSYM) + return (""); + (void) elf_errno(); + if ((data = elf_getdata(s->scn, NULL)) == NULL) { + elferr = elf_errno(); + if (elferr != 0) + warnx("elf_getdata failed: %s", elf_errmsg(elferr)); + return (""); + } + if (gelf_getsym(data, i, &sym) != &sym) + return (""); + /* Return section name for STT_SECTION symbol. */ + if (GELF_ST_TYPE(sym.st_info) == STT_SECTION && + re->sl[sym.st_shndx].name != NULL) + return (re->sl[sym.st_shndx].name); + if ((name = elf_strptr(re->elf, s->link, sym.st_name)) == NULL) + return (""); + + return (name); +} + +static uint64_t +get_symbol_value(struct readelf *re, int symtab, int i) +{ + struct section *s; + GElf_Sym sym; + Elf_Data *data; + int elferr; + + s = &re->sl[symtab]; + if (s->type != SHT_SYMTAB && s->type != SHT_DYNSYM) + return (0); + (void) elf_errno(); + if ((data = elf_getdata(s->scn, NULL)) == NULL) { + elferr = elf_errno(); + if (elferr != 0) + warnx("elf_getdata failed: %s", elf_errmsg(elferr)); + return (0); + } + if (gelf_getsym(data, i, &sym) != &sym) + return (0); + + return (sym.st_value); +} + +static void +hex_dump(struct readelf *re) +{ + struct section *s; + Elf_Data *d; + uint8_t *buf; + size_t sz, nbytes; + uint64_t addr; + int elferr, i, j; + + for (i = 1; (size_t) i < re->shnum; i++) { + s = &re->sl[i]; + if (find_dumpop(re, (size_t) i, s->name, HEX_DUMP, -1) == NULL) + continue; + (void) elf_errno(); + if ((d = elf_getdata(s->scn, NULL)) == NULL) { + elferr = elf_errno(); + if (elferr != 0) + warnx("elf_getdata failed: %s", + elf_errmsg(elferr)); + continue; + } + if (d->d_size <= 0 || d->d_buf == NULL) { + printf("\nSection '%s' has no data to dump.\n", + s->name); + continue; + } + buf = d->d_buf; + sz = d->d_size; + addr = s->addr; + printf("\nHex dump of section '%s':\n", s->name); + while (sz > 0) { + printf(" 0x%8.8jx ", (uintmax_t)addr); + nbytes = sz > 16? 16 : sz; + for (j = 0; j < 16; j++) { + if ((size_t)j < nbytes) + printf("%2.2x", buf[j]); + else + printf(" "); + if ((j & 3) == 3) + printf(" "); + } + for (j = 0; (size_t)j < nbytes; j++) { + if (isprint(buf[j])) + printf("%c", buf[j]); + else + printf("."); + } + printf("\n"); + buf += nbytes; + addr += nbytes; + sz -= nbytes; + } + } +} + +static void +str_dump(struct readelf *re) +{ + struct section *s; + Elf_Data *d; + unsigned char *start, *end, *buf_end; + unsigned int len; + int i, j, elferr, found; + + for (i = 1; (size_t) i < re->shnum; i++) { + s = &re->sl[i]; + if (find_dumpop(re, (size_t) i, s->name, STR_DUMP, -1) == NULL) + continue; + (void) elf_errno(); + if ((d = elf_getdata(s->scn, NULL)) == NULL) { + elferr = elf_errno(); + if (elferr != 0) + warnx("elf_getdata failed: %s", + elf_errmsg(elferr)); + continue; + } + if (d->d_size <= 0 || d->d_buf == NULL) { + printf("\nSection '%s' has no data to dump.\n", + s->name); + continue; + } + buf_end = (unsigned char *) d->d_buf + d->d_size; + start = (unsigned char *) d->d_buf; + found = 0; + printf("\nString dump of section '%s':\n", s->name); + for (;;) { + while (start < buf_end && !isprint(*start)) + start++; + if (start >= buf_end) + break; + end = start + 1; + while (end < buf_end && isprint(*end)) + end++; + printf(" [%6lx] ", + (long) (start - (unsigned char *) d->d_buf)); + len = end - start; + for (j = 0; (unsigned int) j < len; j++) + putchar(start[j]); + putchar('\n'); + found = 1; + if (end >= buf_end) + break; + start = end + 1; + } + if (!found) + printf(" No strings found in this section."); + putchar('\n'); + } +} + +static void +load_sections(struct readelf *re) +{ + struct section *s; + const char *name; + Elf_Scn *scn; + GElf_Shdr sh; + size_t shstrndx, ndx; + int elferr; + + /* Allocate storage for internal section list. */ + if (!elf_getshnum(re->elf, &re->shnum)) { + warnx("elf_getshnum failed: %s", elf_errmsg(-1)); + return; + } + if (re->sl != NULL) + free(re->sl); + if ((re->sl = calloc(re->shnum, sizeof(*re->sl))) == NULL) + err(EXIT_FAILURE, "calloc failed"); + + /* Get the index of .shstrtab section. */ + if (!elf_getshstrndx(re->elf, &shstrndx)) { + warnx("elf_getshstrndx failed: %s", elf_errmsg(-1)); + return; + } + + if ((scn = elf_getscn(re->elf, 0)) == NULL) + return; + + (void) elf_errno(); + do { + if (gelf_getshdr(scn, &sh) == NULL) { + warnx("gelf_getshdr failed: %s", elf_errmsg(-1)); + (void) elf_errno(); + continue; + } + if ((name = elf_strptr(re->elf, shstrndx, sh.sh_name)) == NULL) { + (void) elf_errno(); + name = "ERROR"; + } + if ((ndx = elf_ndxscn(scn)) == SHN_UNDEF) { + if ((elferr = elf_errno()) != 0) + warnx("elf_ndxscn failed: %s", + elf_errmsg(elferr)); + continue; + } + if (ndx >= re->shnum) { + warnx("section index of '%s' out of range", name); + continue; + } + s = &re->sl[ndx]; + s->name = name; + s->scn = scn; + s->off = sh.sh_offset; + s->sz = sh.sh_size; + s->entsize = sh.sh_entsize; + s->align = sh.sh_addralign; + s->type = sh.sh_type; + s->flags = sh.sh_flags; + s->addr = sh.sh_addr; + s->link = sh.sh_link; + s->info = sh.sh_info; + } while ((scn = elf_nextscn(re->elf, scn)) != NULL); + elferr = elf_errno(); + if (elferr != 0) + warnx("elf_nextscn failed: %s", elf_errmsg(elferr)); +} + +static void +unload_sections(struct readelf *re) +{ + + if (re->sl != NULL) { + free(re->sl); + re->sl = NULL; + } + re->shnum = 0; + re->vd_s = NULL; + re->vn_s = NULL; + re->vs_s = NULL; + re->vs = NULL; + re->vs_sz = 0; + if (re->ver != NULL) { + free(re->ver); + re->ver = NULL; + re->ver_sz = 0; + } +} + +static void +dump_elf(struct readelf *re) +{ + + /* Fetch ELF header. No need to continue if it fails. */ + if (gelf_getehdr(re->elf, &re->ehdr) == NULL) { + warnx("gelf_getehdr failed: %s", elf_errmsg(-1)); + return; + } + if ((re->ec = gelf_getclass(re->elf)) == ELFCLASSNONE) { + warnx("gelf_getclass failed: %s", elf_errmsg(-1)); + return; + } + if (re->ehdr.e_ident[EI_DATA] == ELFDATA2MSB) { + re->dw_read = _read_msb; + re->dw_decode = _decode_msb; + } else { + re->dw_read = _read_lsb; + re->dw_decode = _decode_lsb; + } + + if (re->options & ~RE_H) + load_sections(re); + if ((re->options & RE_VV) || (re->options & RE_S)) + search_ver(re); + if (re->options & RE_H) + dump_ehdr(re); + if (re->options & RE_L) + dump_phdr(re); + if (re->options & RE_SS) + dump_shdr(re); + if (re->options & RE_D) + dump_dynamic(re); + if (re->options & RE_R) + dump_reloc(re); + if (re->options & RE_S) + dump_symtabs(re); + if (re->options & RE_N) + dump_notes(re); + if (re->options & RE_II) + dump_hash(re); + if (re->options & RE_X) + hex_dump(re); + if (re->options & RE_P) + str_dump(re); + if (re->options & RE_VV) + dump_ver(re); + if (re->options & RE_AA) + dump_arch_specific_info(re); + if (re->options & RE_W) + dump_dwarf(re); + if (re->options & ~RE_H) + unload_sections(re); +} + +static void +dump_dwarf(struct readelf *re) +{ + int error; + Dwarf_Error de; + + if (dwarf_elf_init(re->elf, DW_DLC_READ, NULL, NULL, &re->dbg, &de)) { + if ((error = dwarf_errno(de)) != DW_DLE_DEBUG_INFO_NULL) + errx(EXIT_FAILURE, "dwarf_elf_init failed: %s", + dwarf_errmsg(de)); + return; + } + + if (re->dop & DW_A) + dump_dwarf_abbrev(re); + if (re->dop & DW_L) + dump_dwarf_line(re); + if (re->dop & DW_LL) + dump_dwarf_line_decoded(re); + if (re->dop & DW_I) { + dump_dwarf_info(re, 0); + dump_dwarf_info(re, 1); + } + if (re->dop & DW_P) + dump_dwarf_pubnames(re); + if (re->dop & DW_R) + dump_dwarf_aranges(re); + if (re->dop & DW_RR) + dump_dwarf_ranges(re); + if (re->dop & DW_M) + dump_dwarf_macinfo(re); + if (re->dop & DW_F) + dump_dwarf_frame(re, 0); + else if (re->dop & DW_FF) + dump_dwarf_frame(re, 1); + if (re->dop & DW_S) + dump_dwarf_str(re); + if (re->dop & DW_O) + dump_dwarf_loclist(re); + + dwarf_finish(re->dbg, &de); +} + +static void +dump_ar(struct readelf *re, int fd) +{ + Elf_Arsym *arsym; + Elf_Arhdr *arhdr; + Elf_Cmd cmd; + Elf *e; + size_t sz; + off_t off; + int i; + + re->ar = re->elf; + + if (re->options & RE_C) { + if ((arsym = elf_getarsym(re->ar, &sz)) == NULL) { + warnx("elf_getarsym() failed: %s", elf_errmsg(-1)); + goto process_members; + } + printf("Index of archive %s: (%ju entries)\n", re->filename, + (uintmax_t) sz - 1); + off = 0; + for (i = 0; (size_t) i < sz; i++) { + if (arsym[i].as_name == NULL) + break; + if (arsym[i].as_off != off) { + off = arsym[i].as_off; + if (elf_rand(re->ar, off) != off) { + warnx("elf_rand() failed: %s", + elf_errmsg(-1)); + continue; + } + if ((e = elf_begin(fd, ELF_C_READ, re->ar)) == + NULL) { + warnx("elf_begin() failed: %s", + elf_errmsg(-1)); + continue; + } + if ((arhdr = elf_getarhdr(e)) == NULL) { + warnx("elf_getarhdr() failed: %s", + elf_errmsg(-1)); + elf_end(e); + continue; + } + printf("Binary %s(%s) contains:\n", + re->filename, arhdr->ar_name); + } + printf("\t%s\n", arsym[i].as_name); + } + if (elf_rand(re->ar, SARMAG) != SARMAG) { + warnx("elf_rand() failed: %s", elf_errmsg(-1)); + return; + } + } + +process_members: + + if ((re->options & ~RE_C) == 0) + return; + + cmd = ELF_C_READ; + while ((re->elf = elf_begin(fd, cmd, re->ar)) != NULL) { + if ((arhdr = elf_getarhdr(re->elf)) == NULL) { + warnx("elf_getarhdr() failed: %s", elf_errmsg(-1)); + goto next_member; + } + if (strcmp(arhdr->ar_name, "/") == 0 || + strcmp(arhdr->ar_name, "//") == 0 || + strcmp(arhdr->ar_name, "__.SYMDEF") == 0) + goto next_member; + printf("\nFile: %s(%s)\n", re->filename, arhdr->ar_name); + dump_elf(re); + + next_member: + cmd = elf_next(re->elf); + elf_end(re->elf); + } + re->elf = re->ar; +} + +static void +dump_object(struct readelf *re) +{ + int fd; + + if ((fd = open(re->filename, O_RDONLY)) == -1) { + warn("open %s failed", re->filename); + return; + } + + if ((re->flags & DISPLAY_FILENAME) != 0) + printf("\nFile: %s\n", re->filename); + + if ((re->elf = elf_begin(fd, ELF_C_READ, NULL)) == NULL) { + warnx("elf_begin() failed: %s", elf_errmsg(-1)); + return; + } + + switch (elf_kind(re->elf)) { + case ELF_K_NONE: + warnx("Not an ELF file."); + return; + case ELF_K_ELF: + dump_elf(re); + break; + case ELF_K_AR: + dump_ar(re, fd); + break; + default: + warnx("Internal: libelf returned unknown elf kind."); + return; + } + + elf_end(re->elf); +} + +static void +add_dumpop(struct readelf *re, size_t si, const char *sn, int op, int t) +{ + struct dumpop *d; + + if ((d = find_dumpop(re, si, sn, -1, t)) == NULL) { + if ((d = calloc(1, sizeof(*d))) == NULL) + err(EXIT_FAILURE, "calloc failed"); + if (t == DUMP_BY_INDEX) + d->u.si = si; + else + d->u.sn = sn; + d->type = t; + d->op = op; + STAILQ_INSERT_TAIL(&re->v_dumpop, d, dumpop_list); + } else + d->op |= op; +} + +static struct dumpop * +find_dumpop(struct readelf *re, size_t si, const char *sn, int op, int t) +{ + struct dumpop *d; + + STAILQ_FOREACH(d, &re->v_dumpop, dumpop_list) { + if ((op == -1 || op & d->op) && + (t == -1 || (unsigned) t == d->type)) { + if ((d->type == DUMP_BY_INDEX && d->u.si == si) || + (d->type == DUMP_BY_NAME && !strcmp(d->u.sn, sn))) + return (d); + } + } + + return (NULL); +} + +static struct { + const char *ln; + char sn; + int value; +} dwarf_op[] = { + {"rawline", 'l', DW_L}, + {"decodedline", 'L', DW_LL}, + {"info", 'i', DW_I}, + {"abbrev", 'a', DW_A}, + {"pubnames", 'p', DW_P}, + {"aranges", 'r', DW_R}, + {"ranges", 'r', DW_R}, + {"Ranges", 'R', DW_RR}, + {"macro", 'm', DW_M}, + {"frames", 'f', DW_F}, + {"frames-interp", 'F', DW_FF}, + {"str", 's', DW_S}, + {"loc", 'o', DW_O}, + {NULL, 0, 0} +}; + +static void +parse_dwarf_op_short(struct readelf *re, const char *op) +{ + int i; + + if (op == NULL) { + re->dop |= DW_DEFAULT_OPTIONS; + return; + } + + for (; *op != '\0'; op++) { + for (i = 0; dwarf_op[i].ln != NULL; i++) { + if (dwarf_op[i].sn == *op) { + re->dop |= dwarf_op[i].value; + break; + } + } + } +} + +static void +parse_dwarf_op_long(struct readelf *re, const char *op) +{ + char *p, *token, *bp; + int i; + + if (op == NULL) { + re->dop |= DW_DEFAULT_OPTIONS; + return; + } + + if ((p = strdup(op)) == NULL) + err(EXIT_FAILURE, "strdup failed"); + bp = p; + + while ((token = strsep(&p, ",")) != NULL) { + for (i = 0; dwarf_op[i].ln != NULL; i++) { + if (!strcmp(token, dwarf_op[i].ln)) { + re->dop |= dwarf_op[i].value; + break; + } + } + } + + free(bp); +} + +static uint64_t +_read_lsb(Elf_Data *d, uint64_t *offsetp, int bytes_to_read) +{ + uint64_t ret; + uint8_t *src; + + src = (uint8_t *) d->d_buf + *offsetp; + + ret = 0; + switch (bytes_to_read) { + case 8: + ret |= ((uint64_t) src[4]) << 32 | ((uint64_t) src[5]) << 40; + ret |= ((uint64_t) src[6]) << 48 | ((uint64_t) src[7]) << 56; + case 4: + ret |= ((uint64_t) src[2]) << 16 | ((uint64_t) src[3]) << 24; + case 2: + ret |= ((uint64_t) src[1]) << 8; + case 1: + ret |= src[0]; + break; + default: + return (0); + } + + *offsetp += bytes_to_read; + + return (ret); +} + +static uint64_t +_read_msb(Elf_Data *d, uint64_t *offsetp, int bytes_to_read) +{ + uint64_t ret; + uint8_t *src; + + src = (uint8_t *) d->d_buf + *offsetp; + + switch (bytes_to_read) { + case 1: + ret = src[0]; + break; + case 2: + ret = src[1] | ((uint64_t) src[0]) << 8; + break; + case 4: + ret = src[3] | ((uint64_t) src[2]) << 8; + ret |= ((uint64_t) src[1]) << 16 | ((uint64_t) src[0]) << 24; + break; + case 8: + ret = src[7] | ((uint64_t) src[6]) << 8; + ret |= ((uint64_t) src[5]) << 16 | ((uint64_t) src[4]) << 24; + ret |= ((uint64_t) src[3]) << 32 | ((uint64_t) src[2]) << 40; + ret |= ((uint64_t) src[1]) << 48 | ((uint64_t) src[0]) << 56; + break; + default: + return (0); + } + + *offsetp += bytes_to_read; + + return (ret); +} + +static uint64_t +_decode_lsb(uint8_t **data, int bytes_to_read) +{ + uint64_t ret; + uint8_t *src; + + src = *data; + + ret = 0; + switch (bytes_to_read) { + case 8: + ret |= ((uint64_t) src[4]) << 32 | ((uint64_t) src[5]) << 40; + ret |= ((uint64_t) src[6]) << 48 | ((uint64_t) src[7]) << 56; + case 4: + ret |= ((uint64_t) src[2]) << 16 | ((uint64_t) src[3]) << 24; + case 2: + ret |= ((uint64_t) src[1]) << 8; + case 1: + ret |= src[0]; + break; + default: + return (0); + } + + *data += bytes_to_read; + + return (ret); +} + +static uint64_t +_decode_msb(uint8_t **data, int bytes_to_read) +{ + uint64_t ret; + uint8_t *src; + + src = *data; + + ret = 0; + switch (bytes_to_read) { + case 1: + ret = src[0]; + break; + case 2: + ret = src[1] | ((uint64_t) src[0]) << 8; + break; + case 4: + ret = src[3] | ((uint64_t) src[2]) << 8; + ret |= ((uint64_t) src[1]) << 16 | ((uint64_t) src[0]) << 24; + break; + case 8: + ret = src[7] | ((uint64_t) src[6]) << 8; + ret |= ((uint64_t) src[5]) << 16 | ((uint64_t) src[4]) << 24; + ret |= ((uint64_t) src[3]) << 32 | ((uint64_t) src[2]) << 40; + ret |= ((uint64_t) src[1]) << 48 | ((uint64_t) src[0]) << 56; + break; + default: + return (0); + break; + } + + *data += bytes_to_read; + + return (ret); +} + +static int64_t +_decode_sleb128(uint8_t **dp) +{ + int64_t ret = 0; + uint8_t b; + int shift = 0; + + uint8_t *src = *dp; + + do { + b = *src++; + ret |= ((b & 0x7f) << shift); + shift += 7; + } while ((b & 0x80) != 0); + + if (shift < 32 && (b & 0x40) != 0) + ret |= (-1 << shift); + + *dp = src; + + return (ret); +} + +static uint64_t +_decode_uleb128(uint8_t **dp) +{ + uint64_t ret = 0; + uint8_t b; + int shift = 0; + + uint8_t *src = *dp; + + do { + b = *src++; + ret |= ((b & 0x7f) << shift); + shift += 7; + } while ((b & 0x80) != 0); + + *dp = src; + + return (ret); +} + +static void +readelf_version(void) +{ + (void) printf("%s (%s)\n", ELFTC_GETPROGNAME(), + elftc_version()); + exit(EXIT_SUCCESS); +} + +#define USAGE_MESSAGE "\ +Usage: %s [options] file...\n\ + Display information about ELF objects and ar(1) archives.\n\n\ + Options:\n\ + -a | --all Equivalent to specifying options '-dhIlrsASV'.\n\ + -c | --archive-index Print the archive symbol table for archives.\n\ + -d | --dynamic Print the contents of SHT_DYNAMIC sections.\n\ + -e | --headers Print all headers in the object.\n\ + -g | --section-groups (accepted, but ignored)\n\ + -h | --file-header Print the file header for the object.\n\ + -l | --program-headers Print the PHDR table for the object.\n\ + -n | --notes Print the contents of SHT_NOTE sections.\n\ + -p INDEX | --string-dump=INDEX\n\ + Print the contents of section at index INDEX.\n\ + -r | --relocs Print relocation information.\n\ + -s | --syms | --symbols Print symbol tables.\n\ + -t | --section-details Print additional information about sections.\n\ + -v | --version Print a version identifier and exit.\n\ + -x INDEX | --hex-dump=INDEX\n\ + Display contents of a section as hexadecimal.\n\ + -A | --arch-specific (accepted, but ignored)\n\ + -D | --use-dynamic Print the symbol table specified by the DT_SYMTAB\n\ + entry in the \".dynamic\" section.\n\ + -H | --help Print a help message.\n\ + -I | --histogram Print information on bucket list lengths for \n\ + hash sections.\n\ + -N | --full-section-name (accepted, but ignored)\n\ + -S | --sections | --section-headers\n\ + Print information about section headers.\n\ + -V | --version-info Print symbol versoning information.\n\ + -W | --wide Print information without wrapping long lines.\n" + + +static void +readelf_usage(void) +{ + fprintf(stderr, USAGE_MESSAGE, ELFTC_GETPROGNAME()); + exit(EXIT_FAILURE); +} + +int +main(int argc, char **argv) +{ + struct readelf *re, re_storage; + unsigned long si; + int opt, i; + char *ep; + + re = &re_storage; + memset(re, 0, sizeof(*re)); + STAILQ_INIT(&re->v_dumpop); + + while ((opt = getopt_long(argc, argv, "AacDdegHhIi:lNnp:rSstuVvWw::x:", + longopts, NULL)) != -1) { + switch(opt) { + case '?': + readelf_usage(); + break; + case 'A': + re->options |= RE_AA; + break; + case 'a': + re->options |= RE_AA | RE_D | RE_H | RE_II | RE_L | + RE_R | RE_SS | RE_S | RE_VV; + break; + case 'c': + re->options |= RE_C; + break; + case 'D': + re->options |= RE_DD; + break; + case 'd': + re->options |= RE_D; + break; + case 'e': + re->options |= RE_H | RE_L | RE_SS; + break; + case 'g': + re->options |= RE_G; + break; + case 'H': + readelf_usage(); + break; + case 'h': + re->options |= RE_H; + break; + case 'I': + re->options |= RE_II; + break; + case 'i': + /* Not implemented yet. */ + break; + case 'l': + re->options |= RE_L; + break; + case 'N': + re->options |= RE_NN; + break; + case 'n': + re->options |= RE_N; + break; + case 'p': + re->options |= RE_P; + si = strtoul(optarg, &ep, 10); + if (*ep == '\0') + add_dumpop(re, (size_t) si, NULL, STR_DUMP, + DUMP_BY_INDEX); + else + add_dumpop(re, 0, optarg, STR_DUMP, + DUMP_BY_NAME); + break; + case 'r': + re->options |= RE_R; + break; + case 'S': + re->options |= RE_SS; + break; + case 's': + re->options |= RE_S; + break; + case 't': + re->options |= RE_T; + break; + case 'u': + re->options |= RE_U; + break; + case 'V': + re->options |= RE_VV; + break; + case 'v': + readelf_version(); + break; + case 'W': + re->options |= RE_WW; + break; + case 'w': + re->options |= RE_W; + parse_dwarf_op_short(re, optarg); + break; + case 'x': + re->options |= RE_X; + si = strtoul(optarg, &ep, 10); + if (*ep == '\0') + add_dumpop(re, (size_t) si, NULL, HEX_DUMP, + DUMP_BY_INDEX); + else + add_dumpop(re, 0, optarg, HEX_DUMP, + DUMP_BY_NAME); + break; + case OPTION_DEBUG_DUMP: + re->options |= RE_W; + parse_dwarf_op_long(re, optarg); + } + } + + argv += optind; + argc -= optind; + + if (argc == 0 || re->options == 0) + readelf_usage(); + + if (argc > 1) + re->flags |= DISPLAY_FILENAME; + + if (elf_version(EV_CURRENT) == EV_NONE) + errx(EXIT_FAILURE, "ELF library initialization failed: %s", + elf_errmsg(-1)); + + for (i = 0; i < argc; i++) { + re->filename = argv[i]; + dump_object(re); + } + + exit(EXIT_SUCCESS); +} diff --git a/contrib/elftoolchain/size/Makefile b/contrib/elftoolchain/size/Makefile new file mode 100644 index 000000000000..6d46d634d462 --- /dev/null +++ b/contrib/elftoolchain/size/Makefile @@ -0,0 +1,11 @@ +# $Id: Makefile 2043 2011-10-23 14:49:16Z jkoshy $ + +TOP= .. + +PROG= size +WARNS?= 6 +LDADD= -lelftc -lelf +DPADD= ${LIBELFTC} ${LIBELF} + +.include "${TOP}/mk/elftoolchain.prog.mk" + diff --git a/contrib/elftoolchain/size/size.1 b/contrib/elftoolchain/size/size.1 new file mode 100644 index 000000000000..97f76c250d86 --- /dev/null +++ b/contrib/elftoolchain/size/size.1 @@ -0,0 +1,257 @@ +.\" Copyright (c) 2007 S.Sam Arun Raj +.\" Copyright (c) 2008,2011 Joseph Koshy +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (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: size.1 3195 2015-05-12 17:22:19Z emaste $ +.\" +.Dd August 25, 2011 +.Dt SIZE 1 +.Os +.Sh NAME +.Nm size +.Nd "display section sizes and total size of ELF objects" +.Sh SYNOPSIS +.Nm +.Op Fl -format= Ns Ar format +.Op Fl -help +.Op Fl -radix= Ns Ar radix +.Op Fl -totals +.Op Fl -version +.Op Fl ABVdhotx +.Op Ar +.Sh DESCRIPTION +The +.Nm +utility +lists the sizes of ELF sections, and optionally the total size, for +each input +.Ar file +specified on the command line. +The +.Nm +utility can operate on ELF objects, on +.Xr ar 1 +archives containing ELF objects, and on core dumps. +If no file name is specified on the command-line, +.Pa a.out +is assumed. +.Pp +The +.Nm +utility recognized the following options: +.Bl -tag -width indent +.It Fl -format= Ns Ar format +Display output using the format specified by argument +.Ar format . +Supported values for this argument are: +.Sq berkeley +and +.Sq sysv . +The default output format is +.Sq berkeley . +See +.Sx Display Formats +below for more information. +.It Fl -help +Display a help message and exit. +.It Fl -radix= Ns Ar radix +Display numeric values using the radix specified by argument +.Ar radix . +Supported values for +.Ar radix +are 8, 10 and 16. +The default radix is 10. +.It Fl -totals +Shows cumulative totals of section sizes from all objects. +This option is ignored for System V style output. +.It Fl -version +Display a version identifier and exit. +.It Fl A +Equivalent to specifying option +.Fl -format= Ns Ar sysv . +.It Fl B +Equivalent to specifying option +.Fl -format= Ns Ar berkeley . +.It Fl V +Equivalent to specifying option +.Fl -version . +.It Fl d +Equivalent to specifying option +.Fl -radix= Ns Ar 10 . +.It Fl h +Equivalent to specifying option +.Fl -help . +.It Fl o +Equivalent to specifying option +.Fl -radix= Ns Ar 8 . +.It Fl t +Equivalent to specifying option +.Fl -totals . +.It Fl x +Equivalent to specifying option +.Fl -radix= Ns Ar 16 . +.El +.Sh DISPLAY FORMATS +.Ss Berkeley Style Output +If +.Sq berkeley +style output is in effect, an initial header line naming fields will +be output, followed by one line of output for each ELF object specified +on the command line or found in an archive. +.Pp +Each line will contain the following whitespace separated fields +in order: +.Bl -enum -compact +.It +The size of the text segment in the object. +.It +The size of the data segment in the object. +.It +The size of the +.Sq bss +segment in the object. +.It +The total size of the object in either decimal or octal. +Decimal output is used if the specified output radix for numeric values +is 10 or 16. +Octal output is used if the radix being used for numeric values +is 8. +.It +The total size of the object in hexadecimal. +.It +The file name of the object. +.El +.Pp +If option +.Fl -totals +was specified, an additional line in the same format as above will be +output at the end containing the sum of the respective fields. +The file name field for the line will contain the string +.Sq (TOTALS) . +.Ss System V Style Output +If System V style output is selected, +.Nm +will output the following information for each object: +.Bl -enum -compact +.It +The name of the object followed by a colon. +.It +A header line containing the names of fields of subsequent lines. +.It +One line per section present in the object. +Each line has three fields: +.Bl -enum -compact +.It +The name of the section. +.It +Its size, in the selected radix for numeric values. +.It +The address associated with the section, in the selected numeric radix. +.El +.It +A line whose section name field contains the string +.Sq Total +and whose size field contains the sum of all reported section sizes. +.El +.Sh EXIT STATUS +.Ex -std +.Sh EXAMPLES +To display the section sizes for +.Pa /bin/ls +use: +.Bd -literal +$ size /bin/ls +text data bss dec hex filename +20975 540 392 21907 5593 /bin/ls +.Ed +.Pp +To display sizes and total for +.Pa /bin/ls +and +.Pa /bin/dd +in hexadecimal, use: +.Bd -literal +$ size -tx /bin/ls /bin/dd +text data bss dec hex filename +0x51ef 0x21c 0x188 21907 5593 /bin/ls +0x3df5 0x170 0x200 16741 4165 /bin/dd +0x8fe4 0x38c 0x388 38648 96f8 (TOTALS) +.Ed +.Pp +To display section sizes for +.Pa /bin/ls +in System V format use: +.Bd -literal +$ size -A /bin/ls +/bin/ls : +section size addr +\&.interp 21 4194704 +\&.note.ABI-tag 24 4194728 +\&.hash 624 4194752 +\&.dynsym 2088 4195376 +\&.dynstr 810 4197464 +\&.rela.dyn 120 4198280 +\&.rela.plt 1656 4198400 +\&.init 19 4200056 +\&.plt 1120 4200076 +\&.text 15224 4201200 +\&.fini 14 4216424 +\&.rodata 1472 4216448 +\&.data 80 5267456 +\&.eh_frame 1624 5267536 +\&.dynamic 384 5269160 +\&.ctors 16 5269544 +\&.dtors 16 5269560 +\&.jcr 8 5269576 +\&.got 576 5269584 +\&.bss 528 5270176 +\&.comment 686 0 +Total 27110 +.Ed +.Sh SEE ALSO +.Xr ar 1 , +.Xr nm 1 , +.Xr objdump 1 , +.Xr readelf 1 , +.Xr strings 1 , +.Xr elf 3 , +.Xr gelf 3 +.Rs +.%A "AT&T Unix Systems Labs" +.%T "System V Application Binary Interface" +.%O http://www.sco.com/developers/gabi/ +.Re +.Sh HISTORY +The +.Nm +utility first appeared in +.At v6 . +.Sh AUTHORS +.An -nosplit +The +.Nm +utility was re-written by +.An S. Sam Arun Raj Aq Mt samarunraj@gmail.com +This manual page was written by +.An S. Sam Arun Raj Aq Mt samarunraj@gmail.com diff --git a/contrib/elftoolchain/size/size.c b/contrib/elftoolchain/size/size.c new file mode 100644 index 000000000000..40d75f8c43f7 --- /dev/null +++ b/contrib/elftoolchain/size/size.c @@ -0,0 +1,917 @@ +/*- + * Copyright (c) 2007 S.Sam Arun Raj + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <assert.h> +#include <err.h> +#include <fcntl.h> +#include <gelf.h> +#include <getopt.h> +#include <libelftc.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include "_elftc.h" + +ELFTC_VCSID("$Id: size.c 3183 2015-04-10 16:18:42Z emaste $"); + +#define BUF_SIZE 1024 +#define ELF_ALIGN(val,x) (((val)+(x)-1) & ~((x)-1)) +#define SIZE_VERSION_STRING "size 1.0" + +enum return_code { + RETURN_OK, + RETURN_NOINPUT, + RETURN_DATAERR, + RETURN_USAGE +}; + +enum output_style { + STYLE_BERKELEY, + STYLE_SYSV +}; + +enum radix_style { + RADIX_OCTAL, + RADIX_DECIMAL, + RADIX_HEX +}; + +static uint64_t bss_size, data_size, text_size, total_size; +static uint64_t bss_size_total, data_size_total, text_size_total; +static int show_totals; +static int size_option; +static enum radix_style radix = RADIX_DECIMAL; +static enum output_style style = STYLE_BERKELEY; +static const char *default_args[2] = { "a.out", NULL }; + +static struct { + int row; + int col; + int *width; + char ***tbl; +} *tb; + +enum { + OPT_FORMAT, + OPT_RADIX +}; + +static struct option size_longopts[] = { + { "format", required_argument, &size_option, OPT_FORMAT }, + { "help", no_argument, NULL, 'h' }, + { "radix", required_argument, &size_option, OPT_RADIX }, + { "totals", no_argument, NULL, 't' }, + { "version", no_argument, NULL, 'V' }, + { NULL, 0, NULL, 0 } +}; + +static void berkeley_calc(GElf_Shdr *); +static void berkeley_footer(const char *, const char *, const char *); +static void berkeley_header(void); +static void berkeley_totals(void); +static int handle_core(char const *, Elf *elf, GElf_Ehdr *); +static void handle_core_note(Elf *, GElf_Ehdr *, GElf_Phdr *, char **); +static int handle_elf(char const *); +static void handle_phdr(Elf *, GElf_Ehdr *, GElf_Phdr *, uint32_t, + const char *); +static void show_version(void); +static void sysv_header(const char *, Elf_Arhdr *); +static void sysv_footer(void); +static void sysv_calc(Elf *, GElf_Ehdr *, GElf_Shdr *); +static void usage(void); +static void tbl_new(int); +static void tbl_print(const char *, int); +static void tbl_print_num(uint64_t, enum radix_style, int); +static void tbl_append(void); +static void tbl_flush(void); + +/* + * size utility using elf(3) and gelf(3) API to list section sizes and + * total in elf files. Supports only elf files (core dumps in elf + * included) that can be opened by libelf, other formats are not supported. + */ +int +main(int argc, char **argv) +{ + int ch, r, rc; + const char **files, *fn; + + rc = RETURN_OK; + + if (elf_version(EV_CURRENT) == EV_NONE) + errx(EXIT_FAILURE, "ELF library initialization failed: %s", + elf_errmsg(-1)); + + while ((ch = getopt_long(argc, argv, "ABVdhotx", size_longopts, + NULL)) != -1) + switch((char)ch) { + case 'A': + style = STYLE_SYSV; + break; + case 'B': + style = STYLE_BERKELEY; + break; + case 'V': + show_version(); + break; + case 'd': + radix = RADIX_DECIMAL; + break; + case 'o': + radix = RADIX_OCTAL; + break; + case 't': + show_totals = 1; + break; + case 'x': + radix = RADIX_HEX; + break; + case 0: + switch (size_option) { + case OPT_FORMAT: + if (*optarg == 's' || *optarg == 'S') + style = STYLE_SYSV; + else if (*optarg == 'b' || *optarg == 'B') + style = STYLE_BERKELEY; + else { + warnx("unrecognized format \"%s\".", + optarg); + usage(); + } + break; + case OPT_RADIX: + r = strtol(optarg, NULL, 10); + if (r == 8) + radix = RADIX_OCTAL; + else if (r == 10) + radix = RADIX_DECIMAL; + else if (r == 16) + radix = RADIX_HEX; + else { + warnx("unsupported radix \"%s\".", + optarg); + usage(); + } + break; + default: + err(EXIT_FAILURE, "Error in option handling."); + /*NOTREACHED*/ + } + break; + case 'h': + case '?': + default: + usage(); + /* NOTREACHED */ + } + argc -= optind; + argv += optind; + + files = (argc == 0) ? default_args : (void *) argv; + + while ((fn = *files) != NULL) { + rc = handle_elf(fn); + if (rc != RETURN_OK) + warnx(rc == RETURN_NOINPUT ? + "'%s': No such file" : + "%s: File format not recognized", fn); + files++; + } + if (style == STYLE_BERKELEY) { + if (show_totals) + berkeley_totals(); + tbl_flush(); + } + return (rc); +} + +static Elf_Data * +xlatetom(Elf *elf, GElf_Ehdr *elfhdr, void *_src, void *_dst, + Elf_Type type, size_t size) +{ + Elf_Data src, dst; + + src.d_buf = _src; + src.d_type = type; + src.d_version = elfhdr->e_version; + src.d_size = size; + dst.d_buf = _dst; + dst.d_version = elfhdr->e_version; + dst.d_size = size; + return (gelf_xlatetom(elf, &dst, &src, elfhdr->e_ident[EI_DATA])); +} + +#define NOTE_OFFSET_32(nhdr, namesz, offset) \ + ((char *)nhdr + sizeof(Elf32_Nhdr) + \ + ELF_ALIGN((int32_t)namesz, 4) + offset) + +#define NOTE_OFFSET_64(nhdr, namesz, offset) \ + ((char *)nhdr + sizeof(Elf32_Nhdr) + \ + ELF_ALIGN((int32_t)namesz, 8) + offset) + +#define PID32(nhdr, namesz, offset) \ + (pid_t)*((int *)((uintptr_t)NOTE_OFFSET_32(nhdr, \ + namesz, offset))); + +#define PID64(nhdr, namesz, offset) \ + (pid_t)*((int *)((uintptr_t)NOTE_OFFSET_64(nhdr, \ + namesz, offset))); + +#define NEXT_NOTE(elfhdr, descsz, namesz, offset) do { \ + if (elfhdr->e_ident[EI_CLASS] == ELFCLASS32) { \ + offset += ELF_ALIGN((int32_t)descsz, 4) + \ + sizeof(Elf32_Nhdr) + \ + ELF_ALIGN((int32_t)namesz, 4); \ + } else { \ + offset += ELF_ALIGN((int32_t)descsz, 8) + \ + sizeof(Elf32_Nhdr) + \ + ELF_ALIGN((int32_t)namesz, 8); \ + } \ +} while (0) + +/* + * Parse individual note entries inside a PT_NOTE segment. + */ +static void +handle_core_note(Elf *elf, GElf_Ehdr *elfhdr, GElf_Phdr *phdr, + char **cmd_line) +{ + size_t max_size; + uint64_t raw_size; + GElf_Off offset; + static pid_t pid; + uintptr_t ver; + Elf32_Nhdr *nhdr, nhdr_l; + static int reg_pseudo = 0, reg2_pseudo = 0 /*, regxfp_pseudo = 0*/; + char buf[BUF_SIZE], *data, *name; + + if (elf == NULL || elfhdr == NULL || phdr == NULL) + return; + + data = elf_rawfile(elf, &max_size); + offset = phdr->p_offset; + while (data != NULL && offset < phdr->p_offset + phdr->p_filesz) { + nhdr = (Elf32_Nhdr *)(uintptr_t)((char*)data + offset); + memset(&nhdr_l, 0, sizeof(Elf32_Nhdr)); + if (!xlatetom(elf, elfhdr, &nhdr->n_type, &nhdr_l.n_type, + ELF_T_WORD, sizeof(Elf32_Word)) || + !xlatetom(elf, elfhdr, &nhdr->n_descsz, &nhdr_l.n_descsz, + ELF_T_WORD, sizeof(Elf32_Word)) || + !xlatetom(elf, elfhdr, &nhdr->n_namesz, &nhdr_l.n_namesz, + ELF_T_WORD, sizeof(Elf32_Word))) + break; + + name = (char *)((char *)nhdr + sizeof(Elf32_Nhdr)); + switch (nhdr_l.n_type) { + case NT_PRSTATUS: { + raw_size = 0; + if (elfhdr->e_ident[EI_OSABI] == ELFOSABI_FREEBSD && + nhdr_l.n_namesz == 0x8 && + !strcmp(name,"FreeBSD")) { + if (elfhdr->e_ident[EI_CLASS] == ELFCLASS32) { + raw_size = (uint64_t)*((uint32_t *) + (uintptr_t)(name + + ELF_ALIGN((int32_t) + nhdr_l.n_namesz, 4) + 8)); + ver = (uintptr_t)NOTE_OFFSET_32(nhdr, + nhdr_l.n_namesz,0); + if (*((int *)ver) == 1) + pid = PID32(nhdr, + nhdr_l.n_namesz, 24); + } else { + raw_size = *((uint64_t *)(uintptr_t) + (name + ELF_ALIGN((int32_t) + nhdr_l.n_namesz, 8) + 16)); + ver = (uintptr_t)NOTE_OFFSET_64(nhdr, + nhdr_l.n_namesz,0); + if (*((int *)ver) == 1) + pid = PID64(nhdr, + nhdr_l.n_namesz, 40); + } + xlatetom(elf, elfhdr, &raw_size, &raw_size, + ELF_T_WORD, sizeof(uint64_t)); + xlatetom(elf, elfhdr, &pid, &pid, ELF_T_WORD, + sizeof(pid_t)); + } + + if (raw_size != 0 && style == STYLE_SYSV) { + (void) snprintf(buf, BUF_SIZE, "%s/%d", + ".reg", pid); + tbl_append(); + tbl_print(buf, 0); + tbl_print_num(raw_size, radix, 1); + tbl_print_num(0, radix, 2); + if (!reg_pseudo) { + tbl_append(); + tbl_print(".reg", 0); + tbl_print_num(raw_size, radix, 1); + tbl_print_num(0, radix, 2); + reg_pseudo = 1; + text_size_total += raw_size; + } + text_size_total += raw_size; + } + } + break; + case NT_FPREGSET: /* same as NT_PRFPREG */ + if (style == STYLE_SYSV) { + (void) snprintf(buf, BUF_SIZE, + "%s/%d", ".reg2", pid); + tbl_append(); + tbl_print(buf, 0); + tbl_print_num(nhdr_l.n_descsz, radix, 1); + tbl_print_num(0, radix, 2); + if (!reg2_pseudo) { + tbl_append(); + tbl_print(".reg2", 0); + tbl_print_num(nhdr_l.n_descsz, radix, + 1); + tbl_print_num(0, radix, 2); + reg2_pseudo = 1; + text_size_total += nhdr_l.n_descsz; + } + text_size_total += nhdr_l.n_descsz; + } + break; +#if 0 + case NT_AUXV: + if (style == STYLE_SYSV) { + tbl_append(); + tbl_print(".auxv", 0); + tbl_print_num(nhdr_l.n_descsz, radix, 1); + tbl_print_num(0, radix, 2); + text_size_total += nhdr_l.n_descsz; + } + break; + case NT_PRXFPREG: + if (style == STYLE_SYSV) { + (void) snprintf(buf, BUF_SIZE, "%s/%d", + ".reg-xfp", pid); + tbl_append(); + tbl_print(buf, 0); + tbl_print_num(nhdr_l.n_descsz, radix, 1); + tbl_print_num(0, radix, 2); + if (!regxfp_pseudo) { + tbl_append(); + tbl_print(".reg-xfp", 0); + tbl_print_num(nhdr_l.n_descsz, radix, + 1); + tbl_print_num(0, radix, 2); + regxfp_pseudo = 1; + text_size_total += nhdr_l.n_descsz; + } + text_size_total += nhdr_l.n_descsz; + } + break; + case NT_PSINFO: +#endif + case NT_PRPSINFO: { + /* FreeBSD 64-bit */ + if (nhdr_l.n_descsz == 0x78 && + !strcmp(name,"FreeBSD")) { + *cmd_line = strdup(NOTE_OFFSET_64(nhdr, + nhdr_l.n_namesz, 33)); + /* FreeBSD 32-bit */ + } else if (nhdr_l.n_descsz == 0x6c && + !strcmp(name,"FreeBSD")) { + *cmd_line = strdup(NOTE_OFFSET_32(nhdr, + nhdr_l.n_namesz, 25)); + } + /* Strip any trailing spaces */ + if (*cmd_line != NULL) { + char *s; + + s = *cmd_line + strlen(*cmd_line); + while (s > *cmd_line) { + if (*(s-1) != 0x20) break; + s--; + } + *s = 0; + } + break; + } +#if 0 + case NT_PSTATUS: + case NT_LWPSTATUS: +#endif + default: + break; + } + NEXT_NOTE(elfhdr, nhdr_l.n_descsz, nhdr_l.n_namesz, offset); + } +} + +/* + * Handles program headers except for PT_NOTE, when sysv output stlye is + * choosen, prints out the segment name and length. For berkely output + * style only PT_LOAD segments are handled, and text, + * data, bss size is calculated for them. + */ +static void +handle_phdr(Elf *elf, GElf_Ehdr *elfhdr, GElf_Phdr *phdr, + uint32_t idx, const char *name) +{ + uint64_t addr, size; + int split; + char buf[BUF_SIZE]; + + if (elf == NULL || elfhdr == NULL || phdr == NULL) + return; + + size = addr = 0; + split = (phdr->p_memsz > 0) && (phdr->p_filesz > 0) && + (phdr->p_memsz > phdr->p_filesz); + + if (style == STYLE_SYSV) { + (void) snprintf(buf, BUF_SIZE, + "%s%d%s", name, idx, (split ? "a" : "")); + tbl_append(); + tbl_print(buf, 0); + tbl_print_num(phdr->p_filesz, radix, 1); + tbl_print_num(phdr->p_vaddr, radix, 2); + text_size_total += phdr->p_filesz; + if (split) { + size = phdr->p_memsz - phdr->p_filesz; + addr = phdr->p_vaddr + phdr->p_filesz; + (void) snprintf(buf, BUF_SIZE, "%s%d%s", name, + idx, "b"); + text_size_total += phdr->p_memsz - phdr->p_filesz; + tbl_append(); + tbl_print(buf, 0); + tbl_print_num(size, radix, 1); + tbl_print_num(addr, radix, 2); + } + } else { + if (phdr->p_type != PT_LOAD) + return; + if ((phdr->p_flags & PF_W) && !(phdr->p_flags & PF_X)) { + data_size += phdr->p_filesz; + if (split) + data_size += phdr->p_memsz - phdr->p_filesz; + } else { + text_size += phdr->p_filesz; + if (split) + text_size += phdr->p_memsz - phdr->p_filesz; + } + } +} + +/* + * Given a core dump file, this function maps program headers to segments. + */ +static int +handle_core(char const *name, Elf *elf, GElf_Ehdr *elfhdr) +{ + GElf_Phdr phdr; + uint32_t i; + char *core_cmdline; + const char *seg_name; + + if (name == NULL || elf == NULL || elfhdr == NULL) + return (RETURN_DATAERR); + if (elfhdr->e_shnum != 0 || elfhdr->e_type != ET_CORE) + return (RETURN_DATAERR); + + seg_name = core_cmdline = NULL; + if (style == STYLE_SYSV) + sysv_header(name, NULL); + else + berkeley_header(); + + for (i = 0; i < elfhdr->e_phnum; i++) { + if (gelf_getphdr(elf, i, &phdr) != NULL) { + if (phdr.p_type == PT_NOTE) { + handle_phdr(elf, elfhdr, &phdr, i, "note"); + handle_core_note(elf, elfhdr, &phdr, + &core_cmdline); + } else { + switch(phdr.p_type) { + case PT_NULL: + seg_name = "null"; + break; + case PT_LOAD: + seg_name = "load"; + break; + case PT_DYNAMIC: + seg_name = "dynamic"; + break; + case PT_INTERP: + seg_name = "interp"; + break; + case PT_SHLIB: + seg_name = "shlib"; + break; + case PT_PHDR: + seg_name = "phdr"; + break; + case PT_GNU_EH_FRAME: + seg_name = "eh_frame_hdr"; + break; + case PT_GNU_STACK: + seg_name = "stack"; + break; + default: + seg_name = "segment"; + } + handle_phdr(elf, elfhdr, &phdr, i, seg_name); + } + } + } + + if (style == STYLE_BERKELEY) { + if (core_cmdline != NULL) { + berkeley_footer(core_cmdline, name, + "core file invoked as"); + } else { + berkeley_footer(core_cmdline, name, "core file"); + } + } else { + sysv_footer(); + if (core_cmdline != NULL) { + (void) printf(" (core file invoked as %s)\n\n", + core_cmdline); + } else { + (void) printf(" (core file)\n\n"); + } + } + free(core_cmdline); + return (RETURN_OK); +} + +/* + * Given an elf object,ar(1) filename, and based on the output style + * and radix format the various sections and their length will be printed + * or the size of the text, data, bss sections will be printed out. + */ +static int +handle_elf(char const *name) +{ + GElf_Ehdr elfhdr; + GElf_Shdr shdr; + Elf *elf, *elf1; + Elf_Arhdr *arhdr; + Elf_Scn *scn; + Elf_Cmd elf_cmd; + int exit_code, fd; + + if (name == NULL) + return (RETURN_NOINPUT); + + if ((fd = open(name, O_RDONLY, 0)) < 0) + return (RETURN_NOINPUT); + + elf_cmd = ELF_C_READ; + elf1 = elf_begin(fd, elf_cmd, NULL); + while ((elf = elf_begin(fd, elf_cmd, elf1)) != NULL) { + arhdr = elf_getarhdr(elf); + if (elf_kind(elf) == ELF_K_NONE && arhdr == NULL) { + (void) elf_end(elf); + (void) elf_end(elf1); + (void) close(fd); + return (RETURN_DATAERR); + } + if (elf_kind(elf) != ELF_K_ELF || + (gelf_getehdr(elf, &elfhdr) == NULL)) { + elf_cmd = elf_next(elf); + (void) elf_end(elf); + warnx("%s: File format not recognized", + arhdr->ar_name); + continue; + } + /* Core dumps are handled separately */ + if (elfhdr.e_shnum == 0 && elfhdr.e_type == ET_CORE) { + exit_code = handle_core(name, elf, &elfhdr); + (void) elf_end(elf); + (void) elf_end(elf1); + (void) close(fd); + return (exit_code); + } else { + scn = NULL; + if (style == STYLE_BERKELEY) { + berkeley_header(); + while ((scn = elf_nextscn(elf, scn)) != NULL) { + if (gelf_getshdr(scn, &shdr) != NULL) + berkeley_calc(&shdr); + } + } else { + sysv_header(name, arhdr); + scn = NULL; + while ((scn = elf_nextscn(elf, scn)) != NULL) { + if (gelf_getshdr(scn, &shdr) != NULL) + sysv_calc(elf, &elfhdr, &shdr); + } + } + if (style == STYLE_BERKELEY) { + if (arhdr != NULL) { + berkeley_footer(name, arhdr->ar_name, + "ex"); + } else { + berkeley_footer(name, NULL, "ex"); + } + } else { + sysv_footer(); + } + } + elf_cmd = elf_next(elf); + (void) elf_end(elf); + } + (void) elf_end(elf1); + (void) close(fd); + return (RETURN_OK); +} + +/* + * Sysv formatting helper functions. + */ +static void +sysv_header(const char *name, Elf_Arhdr *arhdr) +{ + + text_size_total = 0; + if (arhdr != NULL) + (void) printf("%s (ex %s):\n", arhdr->ar_name, name); + else + (void) printf("%s :\n", name); + tbl_new(3); + tbl_append(); + tbl_print("section", 0); + tbl_print("size", 1); + tbl_print("addr", 2); +} + +static void +sysv_calc(Elf *elf, GElf_Ehdr *elfhdr, GElf_Shdr *shdr) +{ + char *section_name; + + section_name = elf_strptr(elf, elfhdr->e_shstrndx, + (size_t) shdr->sh_name); + if ((shdr->sh_type == SHT_SYMTAB || + shdr->sh_type == SHT_STRTAB || shdr->sh_type == SHT_RELA || + shdr->sh_type == SHT_REL) && shdr->sh_addr == 0) + return; + tbl_append(); + tbl_print(section_name, 0); + tbl_print_num(shdr->sh_size, radix, 1); + tbl_print_num(shdr->sh_addr, radix, 2); + text_size_total += shdr->sh_size; +} + +static void +sysv_footer(void) +{ + tbl_append(); + tbl_print("Total", 0); + tbl_print_num(text_size_total, radix, 1); + tbl_flush(); + putchar('\n'); +} + +/* + * berkeley style output formatting helper functions. + */ +static void +berkeley_header(void) +{ + static int printed; + + text_size = data_size = bss_size = 0; + if (!printed) { + tbl_new(6); + tbl_append(); + tbl_print("text", 0); + tbl_print("data", 1); + tbl_print("bss", 2); + if (radix == RADIX_OCTAL) + tbl_print("oct", 3); + else + tbl_print("dec", 3); + tbl_print("hex", 4); + tbl_print("filename", 5); + printed = 1; + } +} + +static void +berkeley_calc(GElf_Shdr *shdr) +{ + if (shdr != NULL) { + if (!(shdr->sh_flags & SHF_ALLOC)) + return; + if ((shdr->sh_flags & SHF_ALLOC) && + ((shdr->sh_flags & SHF_EXECINSTR) || + !(shdr->sh_flags & SHF_WRITE))) + text_size += shdr->sh_size; + else if ((shdr->sh_flags & SHF_ALLOC) && + (shdr->sh_flags & SHF_WRITE) && + (shdr->sh_type != SHT_NOBITS)) + data_size += shdr->sh_size; + else + bss_size += shdr->sh_size; + } +} + +static void +berkeley_totals(void) +{ + long unsigned int grand_total; + + grand_total = text_size_total + data_size_total + bss_size_total; + tbl_append(); + tbl_print_num(text_size_total, radix, 0); + tbl_print_num(data_size_total, radix, 1); + tbl_print_num(bss_size_total, radix, 2); + if (radix == RADIX_OCTAL) + tbl_print_num(grand_total, RADIX_OCTAL, 3); + else + tbl_print_num(grand_total, RADIX_DECIMAL, 3); + tbl_print_num(grand_total, RADIX_HEX, 4); +} + +static void +berkeley_footer(const char *name, const char *ar_name, const char *msg) +{ + char buf[BUF_SIZE]; + + total_size = text_size + data_size + bss_size; + if (show_totals) { + text_size_total += text_size; + bss_size_total += bss_size; + data_size_total += data_size; + } + + tbl_append(); + tbl_print_num(text_size, radix, 0); + tbl_print_num(data_size, radix, 1); + tbl_print_num(bss_size, radix, 2); + if (radix == RADIX_OCTAL) + tbl_print_num(total_size, RADIX_OCTAL, 3); + else + tbl_print_num(total_size, RADIX_DECIMAL, 3); + tbl_print_num(total_size, RADIX_HEX, 4); + if (ar_name != NULL && name != NULL) + (void) snprintf(buf, BUF_SIZE, "%s (%s %s)", ar_name, msg, + name); + else if (ar_name != NULL && name == NULL) + (void) snprintf(buf, BUF_SIZE, "%s (%s)", ar_name, msg); + else + (void) snprintf(buf, BUF_SIZE, "%s", name); + tbl_print(buf, 5); +} + + +static void +tbl_new(int col) +{ + + assert(tb == NULL); + assert(col > 0); + if ((tb = calloc(1, sizeof(*tb))) == NULL) + err(EXIT_FAILURE, "calloc"); + if ((tb->tbl = calloc(col, sizeof(*tb->tbl))) == NULL) + err(EXIT_FAILURE, "calloc"); + if ((tb->width = calloc(col, sizeof(*tb->width))) == NULL) + err(EXIT_FAILURE, "calloc"); + tb->col = col; + tb->row = 0; +} + +static void +tbl_print(const char *s, int col) +{ + int len; + + assert(tb != NULL && tb->col > 0 && tb->row > 0 && col < tb->col); + assert(s != NULL && tb->tbl[col][tb->row - 1] == NULL); + if ((tb->tbl[col][tb->row - 1] = strdup(s)) == NULL) + err(EXIT_FAILURE, "strdup"); + len = strlen(s); + if (len > tb->width[col]) + tb->width[col] = len; +} + +static void +tbl_print_num(uint64_t num, enum radix_style rad, int col) +{ + char buf[BUF_SIZE]; + + (void) snprintf(buf, BUF_SIZE, (rad == RADIX_DECIMAL ? "%ju" : + ((rad == RADIX_OCTAL) ? "0%jo" : "0x%jx")), (uintmax_t) num); + tbl_print(buf, col); +} + +static void +tbl_append(void) +{ + int i; + + assert(tb != NULL && tb->col > 0); + tb->row++; + for (i = 0; i < tb->col; i++) { + tb->tbl[i] = realloc(tb->tbl[i], sizeof(*tb->tbl[i]) * tb->row); + if (tb->tbl[i] == NULL) + err(EXIT_FAILURE, "realloc"); + tb->tbl[i][tb->row - 1] = NULL; + } +} + +static void +tbl_flush(void) +{ + const char *str; + int i, j; + + if (tb == NULL) + return; + + assert(tb->col > 0); + for (i = 0; i < tb->row; i++) { + if (style == STYLE_BERKELEY) + printf(" "); + for (j = 0; j < tb->col; j++) { + str = (tb->tbl[j][i] != NULL ? tb->tbl[j][i] : ""); + if (style == STYLE_SYSV && j == 0) + printf("%-*s", tb->width[j], str); + else if (style == STYLE_BERKELEY && j == tb->col - 1) + printf("%s", str); + else + printf("%*s", tb->width[j], str); + if (j == tb->col -1) + putchar('\n'); + else + printf(" "); + } + } + + for (i = 0; i < tb->col; i++) { + for (j = 0; j < tb->row; j++) { + if (tb->tbl[i][j]) + free(tb->tbl[i][j]); + } + free(tb->tbl[i]); + } + free(tb->tbl); + free(tb->width); + free(tb); + tb = NULL; +} + +#define USAGE_MESSAGE "\ +Usage: %s [options] file ...\n\ + Display sizes of ELF sections.\n\n\ + Options:\n\ + --format=format Display output in specified format. Supported\n\ + values are `berkeley' and `sysv'.\n\ + --help Display this help message and exit.\n\ + --radix=radix Display numeric values in the specified radix.\n\ + Supported values are: 8, 10 and 16.\n\ + --totals Show cumulative totals of section sizes.\n\ + --version Display a version identifier and exit.\n\ + -A Equivalent to `--format=sysv'.\n\ + -B Equivalent to `--format=berkeley'.\n\ + -V Equivalent to `--version'.\n\ + -d Equivalent to `--radix=10'.\n\ + -h Same as option --help.\n\ + -o Equivalent to `--radix=8'.\n\ + -t Equivalent to option --totals.\n\ + -x Equivalent to `--radix=16'.\n" + +static void +usage(void) +{ + (void) fprintf(stderr, USAGE_MESSAGE, ELFTC_GETPROGNAME()); + exit(EXIT_FAILURE); +} + +static void +show_version(void) +{ + (void) printf("%s (%s)\n", ELFTC_GETPROGNAME(), elftc_version()); + exit(EXIT_SUCCESS); +} diff --git a/contrib/elftoolchain/strings/Makefile b/contrib/elftoolchain/strings/Makefile new file mode 100644 index 000000000000..0bc4cd5d97fe --- /dev/null +++ b/contrib/elftoolchain/strings/Makefile @@ -0,0 +1,11 @@ +# $Id: Makefile 2044 2011-10-23 14:52:59Z jkoshy $ + +TOP= .. + +PROG= strings +WARNS?= 6 +DPADD= ${LIBELFTC} ${LIBELF} +LDADD= -lelftc -lelf + +.include "${TOP}/mk/elftoolchain.prog.mk" + diff --git a/contrib/elftoolchain/strings/strings.1 b/contrib/elftoolchain/strings/strings.1 new file mode 100644 index 000000000000..15ad7a7f1eb3 --- /dev/null +++ b/contrib/elftoolchain/strings/strings.1 @@ -0,0 +1,162 @@ +.\" Copyright (c) 2007 S.Sam Arun Raj +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (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: strings.1 3195 2015-05-12 17:22:19Z emaste $ +.\" +.Dd December 19, 2011 +.Dt STRINGS 1 +.Os +.Sh NAME +.Nm strings +.Nd "print the strings of printable characters in files" +.Sh SYNOPSIS +.Nm +.Op Fl a | Fl -all +.Op Fl e Ar encoding | Fl -encoding= Ns Ar encoding +.Op Fl f | Fl -print-file-name +.Op Fl h | Fl -help +.Op Fl n Ar number | Fl -bytes= Ns Ar number | Fl Ar number +.Op Fl o +.Op Fl t Ar radix | Fl -radix= Ns Ar radix +.Op Fl v | Fl -version +.Op Ar +.Sh DESCRIPTION +For each +.Ar file +specified, the +.Nm +utility prints contiguous sequences of printable +characters that are at least +.Va n +characters long and are followed by an unprintable character. +The default value of +.Va n +is 4. +By default, the +.Nm +utility only scans the initialized and loaded sections of ELF objects; +for other file types, the entire file is scanned. +The +.Nm +utility is mainly used for determining the contents of non-text files. +.Pp +If no file name is specified as an argument, standard input is read. +.Pp +The following options are available: +.Bl -tag -width indent +.It Fl a | Fl -all +For ELF objects, scan the entire file for printable strings. +.It Fl e Ar encoding | Fl -encoding= Ns Ar encoding +Select the character encoding to be used while searching for strings. +Valid values for argument +.Ar encoding +are: +.Bl -tag -width indent -compact +.It Ar s +for single 7-bit-byte characters (ASCII, ISO 8859). +.It Ar S +for single 8-bit-byte characters. +.It Ar l +for 16-bit little-endian. +.It Ar b +for 16-bit big-endian. +.It Ar L +for 32-bit little-endian. +.It Ar B +for 32-bit big-endian. +.El +The default is to assume that characters are encoded using a single +7-bit byte. +.It Fl f | Fl -print-file-name +Print the name of the file before each string. +.It Fl h | Fl -help +Print a usage summary and exit. +.It Xo +.Fl n Ar number | +.Fl -bytes= Ns Ar number | +.Fl Ar number +.Xc +Print the contiguous character sequence of at least +.Ar number +characters long, instead of the default of 4 characters. +.It Fl o +Equivalent to specifying +.Fl t Ar o . +.It Fl t Ar radix | Fl -radix= Ns Ar radix +Print the offset from the start of the file before each string +using the specified radix. +Valid values for argument +.Ar radix +are: +.Bl -tag -width indent -compact +.It Ar d +for decimal +.It Ar o +for octal +.It Ar x +for hexadecimal +.El +.It Fl v | Fl -version +Display a version identifier and exit. +.El +.Sh EXIT STATUS +.Ex -std +.Sh EXAMPLES +To display strings in +.Pa /bin/ls +use: +.Dl "$ strings /bin/ls" +.Pp +To display strings in all sections of +.Pa /bin/ln +use: +.Dl "$ strings -a /bin/ln" +.Pp +To display strings in all sections of +.Pa /bin/cat +prefixed with the filename and the offset within the file use: +.Dl "$ strings -a -f -t x /bin/cat" +.Sh SEE ALSO +.Xr ar 1 , +.Xr nm 1 , +.Xr objdump 1 , +.Xr ranlib , +.Xr readelf 1 , +.Xr size 1 +.Sh HISTORY +The first FreeBSD +.Nm +utility appeared in +.Fx v3. +It was later discontinued in +.Fx v5 , +when i386-only a.out format was dropped in favor of ELF. +.Sh AUTHORS +.An -nosplit +The +.Nm +utility was re-written by +.An S.Sam Arun Raj Aq Mt samarunraj@gmail.com . +This manual page was written by +.An S.Sam Arun Raj Aq Mt samarunraj@gmail.com . diff --git a/contrib/elftoolchain/strings/strings.c b/contrib/elftoolchain/strings/strings.c new file mode 100644 index 000000000000..6eab165e0bf4 --- /dev/null +++ b/contrib/elftoolchain/strings/strings.c @@ -0,0 +1,453 @@ +/*- + * Copyright (c) 2007 S.Sam Arun Raj + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include <sys/stat.h> +#include <sys/types.h> + +#include <ctype.h> +#include <err.h> +#include <errno.h> +#include <fcntl.h> +#include <getopt.h> +#include <inttypes.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include <libelf.h> +#include <libelftc.h> +#include <gelf.h> + +#include "_elftc.h" + +ELFTC_VCSID("$Id: strings.c 3174 2015-03-27 17:13:41Z emaste $"); + +enum return_code { + RETURN_OK, + RETURN_NOINPUT, + RETURN_SOFTWARE +}; + +enum radix_style { + RADIX_DECIMAL, + RADIX_HEX, + RADIX_OCTAL +}; + +enum encoding_style { + ENCODING_7BIT, + ENCODING_8BIT, + ENCODING_16BIT_BIG, + ENCODING_16BIT_LITTLE, + ENCODING_32BIT_BIG, + ENCODING_32BIT_LITTLE +}; + +#define PRINTABLE(c) \ + ((c) >= 0 && (c) <= 255 && \ + ((c) == '\t' || isprint((c)) || \ + (encoding == ENCODING_8BIT && (c) > 127))) + + +static int encoding_size, entire_file, min_len, show_filename, show_loc; +static enum encoding_style encoding; +static enum radix_style radix; + +static struct option strings_longopts[] = { + { "all", no_argument, NULL, 'a'}, + { "bytes", required_argument, NULL, 'n'}, + { "encoding", required_argument, NULL, 'e'}, + { "help", no_argument, NULL, 'h'}, + { "print-file-name", no_argument, NULL, 'f'}, + { "radix", required_argument, NULL, 't'}, + { "version", no_argument, NULL, 'v'}, + { NULL, 0, NULL, 0 } +}; + +long getcharacter(void); +int handle_file(const char *); +int handle_elf(const char *, int); +int handle_binary(const char *, int); +int find_strings(const char *, off_t, off_t); +void show_version(void); +void usage(void); + +/* + * strings(1) extracts text(contiguous printable characters) + * from elf and binary files. + */ +int +main(int argc, char **argv) +{ + int ch, rc; + + rc = RETURN_OK; + min_len = 0; + encoding_size = 1; + if (elf_version(EV_CURRENT) == EV_NONE) + errx(EXIT_FAILURE, "ELF library initialization failed: %s", + elf_errmsg(-1)); + + while ((ch = getopt_long(argc, argv, "1234567890ae:fhn:ot:Vv", + strings_longopts, NULL)) != -1) + switch((char)ch) { + case 'a': + entire_file = 1; + break; + case 'e': + if (*optarg == 's') { + encoding = ENCODING_7BIT; + } else if (*optarg == 'S') { + encoding = ENCODING_8BIT; + } else if (*optarg == 'b') { + encoding = ENCODING_16BIT_BIG; + encoding_size = 2; + } else if (*optarg == 'B') { + encoding = ENCODING_32BIT_BIG; + encoding_size = 4; + } else if (*optarg == 'l') { + encoding = ENCODING_16BIT_LITTLE; + encoding_size = 2; + } else if (*optarg == 'L') { + encoding = ENCODING_32BIT_LITTLE; + encoding_size = 4; + } else + usage(); + /* NOTREACHED */ + break; + case 'f': + show_filename = 1; + break; + case 'n': + min_len = (int)strtoimax(optarg, (char**)NULL, 10); + break; + case 'o': + show_loc = 1; + radix = RADIX_OCTAL; + break; + case 't': + show_loc = 1; + if (*optarg == 'd') + radix = RADIX_DECIMAL; + else if (*optarg == 'o') + radix = RADIX_OCTAL; + else if (*optarg == 'x') + radix = RADIX_HEX; + else + usage(); + /* NOTREACHED */ + break; + case 'v': + case 'V': + show_version(); + /* NOTREACHED */ + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + min_len *= 10; + min_len += ch - '0'; + break; + case 'h': + case '?': + default: + usage(); + /* NOTREACHED */ + } + argc -= optind; + argv += optind; + + if (!min_len) + min_len = 4; + if (!*argv) + rc = handle_file("{standard input}"); + else while (*argv) { + rc = handle_file(*argv); + argv++; + } + return (rc); +} + +int +handle_file(const char *name) +{ + int fd, rt; + + if (name == NULL) + return (RETURN_NOINPUT); + if (strcmp("{standard input}", name) != 0) { + if (freopen(name, "rb", stdin) == NULL) { + warnx("'%s': %s", name, strerror(errno)); + return (RETURN_NOINPUT); + } + } else { + return (find_strings(name, (off_t)0, (off_t)0)); + } + + fd = fileno(stdin); + if (fd < 0) + return (RETURN_NOINPUT); + rt = handle_elf(name, fd); + return (rt); +} + +/* + * Files not understood by handle_elf, will be passed off here and will + * treated as a binary file. This would include text file, core dumps ... + */ +int +handle_binary(const char *name, int fd) +{ + struct stat buf; + + memset(&buf, 0, sizeof(struct stat)); + (void) lseek(fd, (off_t)0, SEEK_SET); + if (!fstat(fd, &buf)) + return (find_strings(name, (off_t)0, buf.st_size)); + return (RETURN_SOFTWARE); +} + +/* + * Will analyse a file to see if it ELF, other files including ar(1), + * core dumps are passed off and treated as flat binary files. Unlike + * GNU size in FreeBSD this routine will not treat ELF object from + * different archs as flat binary files(has to overridden using -a). + */ +int +handle_elf(const char *name, int fd) +{ + GElf_Ehdr elfhdr; + GElf_Shdr shdr; + Elf *elf; + Elf_Scn *scn; + int rc; + + rc = RETURN_OK; + /* If entire file is choosen, treat it as a binary file */ + if (entire_file) + return (handle_binary(name, fd)); + + (void) lseek(fd, (off_t)0, SEEK_SET); + elf = elf_begin(fd, ELF_C_READ, NULL); + if (elf_kind(elf) != ELF_K_ELF) { + (void) elf_end(elf); + return (handle_binary(name, fd)); + } + + if (gelf_getehdr(elf, &elfhdr) == NULL) { + (void) elf_end(elf); + warnx("%s: ELF file could not be processed", name); + return (RETURN_SOFTWARE); + } + + if (elfhdr.e_shnum == 0 && elfhdr.e_type == ET_CORE) { + (void) elf_end(elf); + return (handle_binary(name, fd)); + } else { + scn = NULL; + while ((scn = elf_nextscn(elf, scn)) != NULL) { + if (gelf_getshdr(scn, &shdr) == NULL) + continue; + if (shdr.sh_type != SHT_NOBITS && + (shdr.sh_flags & SHF_ALLOC) != 0) { + rc = find_strings(name, shdr.sh_offset, + shdr.sh_size); + } + } + } + (void) elf_end(elf); + return (rc); +} + +/* + * Retrieves a character from input stream based on the encoding + * type requested. + */ +long +getcharacter(void) +{ + long rt; + int i; + char buf[4], c; + + rt = EOF; + for(i = 0; i < encoding_size; i++) { + c = getc(stdin); + if (feof(stdin)) + return (EOF); + buf[i] = c; + } + + switch(encoding) { + case ENCODING_7BIT: + case ENCODING_8BIT: + rt = buf[0]; + break; + case ENCODING_16BIT_BIG: + rt = (buf[0] << 8) | buf[1]; + break; + case ENCODING_16BIT_LITTLE: + rt = buf[0] | (buf[1] << 8); + break; + case ENCODING_32BIT_BIG: + rt = ((long) buf[0] << 24) | ((long) buf[1] << 16) | + ((long) buf[2] << 8) | buf[3]; + break; + case ENCODING_32BIT_LITTLE: + rt = buf[0] | ((long) buf[1] << 8) | ((long) buf[2] << 16) | + ((long) buf[3] << 24); + break; + } + return (rt); +} + +/* + * Input stream stdin is read until the end of file is reached or until + * the section size is reached in case of ELF files. Contiguous + * characters of >= min_size(default 4) will be displayed. + */ +int +find_strings(const char *name, off_t offset, off_t size) +{ + off_t cur_off, start_off; + char *obuf; + long c; + int i; + + if ((obuf = (char*)calloc(1, min_len + 1)) == NULL) { + (void) fprintf(stderr, "Unable to allocate memory: %s\n", + strerror(errno)); + return (RETURN_SOFTWARE); + } + + (void) fseeko(stdin, offset, SEEK_SET); + cur_off = offset; + start_off = 0; + while(1) { + if ((offset + size) && (cur_off >= offset + size)) + break; + start_off = cur_off; + memset(obuf, 0, min_len+1); + for(i = 0; i < min_len; i++) { + c = getcharacter(); + if (c == EOF && feof(stdin)) + goto _exit1; + if (PRINTABLE(c)) { + obuf[i] = c; + obuf[i+1] = 0; + cur_off += encoding_size; + } else { + if (encoding == ENCODING_8BIT && + (uint8_t)c > 127) { + obuf[i] = c; + obuf[i+1] = 0; + cur_off += encoding_size; + continue; + } + cur_off += encoding_size; + break; + } + } + + if (i >= min_len && ((cur_off <= offset + size) || + !(offset + size))) { + if (show_filename) + printf ("%s: ", name); + if (show_loc) { + switch(radix) { + case RADIX_DECIMAL: + (void) printf("%7ju ", + (uintmax_t)start_off); + break; + case RADIX_HEX: + (void) printf("%7jx ", + (uintmax_t)start_off); + break; + case RADIX_OCTAL: + (void) printf("%7jo ", + (uintmax_t)start_off); + break; + } + } + printf("%s", obuf); + + while(1) { + if ((offset + size) && + (cur_off >= offset + size)) + break; + c = getcharacter(); + cur_off += encoding_size; + if (encoding == ENCODING_8BIT && + (uint8_t)c > 127) { + putchar(c); + continue; + } + if (!PRINTABLE(c) || c == EOF) + break; + putchar(c); + } + putchar('\n'); + } + } +_exit1: + free(obuf); + return (RETURN_OK); +} + +#define USAGE_MESSAGE "\ +Usage: %s [options] [file...]\n\ + Print contiguous sequences of printable characters.\n\n\ + Options:\n\ + -a | --all Scan the entire file for strings.\n\ + -e ENC | --encoding=ENC Select the character encoding to use.\n\ + -f | --print-file-name Print the file name before each string.\n\ + -h | --help Print a help message and exit.\n\ + -n N | --bytes=N | -N Print sequences with 'N' or more characters.\n\ + -o Print offsets in octal.\n\ + -t R | --radix=R Print offsets using the radix named by 'R'.\n\ + -v | --version Print a version identifier and exit.\n" + +void +usage(void) +{ + (void) fprintf(stderr, USAGE_MESSAGE, ELFTC_GETPROGNAME()); + exit(EXIT_FAILURE); +} + +void +show_version(void) +{ + (void) printf("%s (%s)\n", ELFTC_GETPROGNAME(), elftc_version()); + exit(EXIT_SUCCESS); +} |