Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/elf2aout A couple of fixes so that NetBSD/cats build...



details:   https://anonhg.NetBSD.org/src/rev/4ca7bf3597c2
branches:  trunk
changeset: 456170:4ca7bf3597c2
user:      skrll <skrll%NetBSD.org@localhost>
date:      Fri Apr 26 07:35:21 2019 +0000

description:
A couple of fixes so that NetBSD/cats builds (on linux), works, and
produces a symbol table that is readable (by 4.4BSD nm(1) at least)

- Change from OMAGIC to ZMAGIC
- Resize the a.out string table if we run out of space.
- output symbol table size as int32_t (the code already assumes no 64bit
  a.out.

diffstat:

 usr.bin/elf2aout/elf2aout.c |  41 +++++++++++++++++++++++++++++------------
 1 files changed, 29 insertions(+), 12 deletions(-)

diffs (105 lines):

diff -r 69f992bf0375 -r 4ca7bf3597c2 usr.bin/elf2aout/elf2aout.c
--- a/usr.bin/elf2aout/elf2aout.c       Fri Apr 26 07:32:40 2019 +0000
+++ b/usr.bin/elf2aout/elf2aout.c       Fri Apr 26 07:35:21 2019 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: elf2aout.c,v 1.19 2016/03/07 22:16:38 martin Exp $     */
+/*     $NetBSD: elf2aout.c,v 1.20 2019/04/26 07:35:21 skrll Exp $      */
 
 /*
  * Copyright (c) 1995
@@ -272,8 +272,8 @@
        default:
                mid = MID_ZERO;
        }
-       aex.a_midmag = (u_long)htonl(((u_long)symflag << 26) 
-           | ((u_long)mid << 16) | OMAGIC);
+       aex.a_midmag = (u_long)htonl(((u_long)symflag << 26)
+           | ((u_long)mid << 16) | ZMAGIC);
 
        aex.a_text = text.len;
        aex.a_data = data.len;
@@ -364,7 +364,8 @@
        ssize_t i, remaining, cur;
        char   *oldstrings;
        char   *newstrings, *nsp;
-       size_t  newstringsize, stringsizebuf;
+       size_t  newstringsize;
+       uint32_t stringsizebuf;
 
        /* Zero the unused fields in the output buffer.. */
        memset(outbuf, 0, sizeof outbuf);
@@ -375,10 +376,10 @@
        /* Suck in the old string table... */
        oldstrings = saveRead(in, stroff, (size_t)strsize, "string table");
 
-       /* Allocate space for the new one.   XXX We make the wild assumption
-        * that no two symbol table entries will point at the same place in
-        * the string table - if that assumption is bad, this could easily
-        * blow up. */
+       /*
+        * Allocate space for the new one.  We will increase the space if
+        * this is too small
+        */
        newstringsize = (size_t)(strsize + remaining);
        newstrings = malloc(newstringsize);
        if (newstrings == NULL)
@@ -390,11 +391,10 @@
        if (lseek(in, symoff, SEEK_SET) < 0)
                err(EXIT_FAILURE, "Can't seek");
        /* Translate and copy symbols... */
-       while (remaining) {
+       for (; remaining; remaining -= cur) {
                cur = remaining;
                if (cur > SYMS_PER_PASS)
                        cur = SYMS_PER_PASS;
-               remaining -= cur;
                if ((i = read(in, inbuf, (size_t)cur * sizeof(Elf32_Sym)))
                    != cur * (ssize_t)sizeof(Elf32_Sym)) {
                        if (i < 0)
@@ -406,6 +406,7 @@
                /* Do the translation... */
                for (i = 0; i < cur; i++) {
                        int     binding, type;
+                       size_t off, len;
 
 #if TARGET_BYTE_ORDER != BYTE_ORDER
                        inbuf[i].st_name  = bswap32(inbuf[i].st_name);
@@ -413,12 +414,28 @@
                        inbuf[i].st_size  = bswap32(inbuf[i].st_size);
                        inbuf[i].st_shndx = bswap16(inbuf[i].st_shndx);
 #endif
+                       off = (size_t)(nsp - newstrings);
+
+                       /* length of this symbol with leading '_' and trailing '\0' */
+                       len = strlen(oldstrings + inbuf[i].st_name) + 1 + 1;
+
+                       /* Does it fit? If not make more space */
+                       if (newstringsize - off < len) {
+                               char *nns;
+
+                               newstringsize += (size_t)(remaining) * len;
+                               nns = realloc(newstrings, newstringsize);
+                               if (nns == NULL)
+                                       err(EXIT_FAILURE, "No memory for new string table!");
+                               newstrings = nns;
+                               nsp = newstrings + off;
+                       }
                        /* Copy the symbol into the new table, but prepend an
                         * underscore. */
                        *nsp = '_';
                        strcpy(nsp + 1, oldstrings + inbuf[i].st_name);
                        outbuf[i].n_un.n_strx = nsp - newstrings + 4;
-                       nsp += strlen(nsp) + 1;
+                       nsp += len;
 
                        type = ELF32_ST_TYPE(inbuf[i].st_info);
                        binding = ELF32_ST_BIND(inbuf[i].st_info);
@@ -475,7 +492,7 @@
        char    ibuf[4096];
        ssize_t remaining, cur, count;
 
-       /* Go to the start of the ELF symbol table... */
+       /* Go to the start of the segment... */
        if (lseek(in, offset, SEEK_SET) < 0)
                err(EXIT_FAILURE, "%s: lseek failed", __func__);
        if (size > SSIZE_MAX)



Home | Main Index | Thread Index | Old Index