Fix for bug #170 - ELF output module exports global,defined symbols as undefined
authorggn <ggn.dbug@gmail.com>
Sat, 8 Aug 2020 14:17:56 +0000 (17:17 +0300)
committerShamus Hammons <jlhamm@acm.org>
Sun, 16 Aug 2020 21:42:45 +0000 (16:42 -0500)
Signed-off-by: ggn <ggn.dbug@gmail.com>
object.c
object.h

index 0b7df7ed8ecc77b1d7bb2eb1ad9ad26b6319821b..fe107434f9c835538a4bbe8dce2a4233d5f1a413 100644 (file)
--- a/object.c
+++ b/object.c
@@ -224,7 +224,7 @@ uint8_t * AddELFSymEntry(uint8_t * buf, SYM * sym, int globflag)
        if (w1 & DEFINED)
        {
                if (globflag)           // Export the symbol
-                       st_info |= 16;   //STB_GLOBAL (1<<4)
+                       st_info |= 16;  // STB_GLOBAL (1<<4)
        }
        else if (w1 & (GLOBAL | REFERENCED))
                st_info |= 16;
@@ -232,7 +232,7 @@ uint8_t * AddELFSymEntry(uint8_t * buf, SYM * sym, int globflag)
        D_byte(st_info);
        D_byte(0);                              // st_other
 
-       uint16_t st_shndx = 0xFFF1;     // Assume absolute (equated) number
+       uint16_t st_shndx = SHN_ABS;    // Assume absolute (equated) number
 
        if (w1 & TEXT)
                st_shndx = elfHdrNum[ES_TEXT];
@@ -240,8 +240,13 @@ uint8_t * AddELFSymEntry(uint8_t * buf, SYM * sym, int globflag)
                st_shndx = elfHdrNum[ES_DATA];
        else if (w1 & BSS)
                st_shndx = elfHdrNum[ES_BSS];
-       else if (globflag)
-               st_shndx = 0;           // Global, not absolute
+       else if (globflag && !(w1 & DEFINED) && (w1 & REFERENCED))
+       {
+               st_shndx = SHN_UNDEF;
+       }                                      // If the symbol is global then probably we
+                                    // don't need to do anything (probably)
+                                    // since we set STB_GLOBAL in st_info above.
+                                    // Unless we need to set it to SHN_COMMON?
 
        D_word(st_shndx);
 
index f8714577dfae5fd65a6b906ed2c4ad6f4a640ff9..fe5997c745e76fa3dc31edcab1c4311e7af9a160 100644 (file)
--- a/object.h
+++ b/object.h
@@ -33,6 +33,14 @@ enum ELFSectionNames
        ES_SYMTAB, ES_STRTAB
 };
 
+//
+// ELF special section indices (field st_shndx)
+// Lifted from glibc (https://sourceware.org/git/?p=glibc.git;a=blob;f=elf/elf.h)
+//
+#define SHN_UNDEF       0               /* Undefined section */
+#define SHN_ABS         0xFFF1          /* Associated symbol is absolute */
+#define SHN_COMMON      0xFFF2          /* Associated symbol is common */
+
 // Exported variables.
 extern uint8_t * objImage;
 extern int elfHdrNum[];