Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

Changeset 628

Show
Ignore:
Timestamp:
08/26/10 06:12:38 (14 years ago)
Author:
walter
Message:

bugzilla 4721: compilation slow when compiling unittests on dcollections

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/dmd-1.x/src/backend/elfobj.c

    r589 r628  
    2020#include        <alloca.h> 
    2121#endif 
    2222 
    2323#include        "cc.h" 
    2424#include        "global.h" 
    2525#include        "code.h" 
    2626#include        "type.h" 
    2727#include        "melf.h" 
    2828#include        "outbuf.h" 
    2929#include        "filespec.h" 
    3030#include        "cv4.h" 
    3131#include        "cgcv.h" 
    3232#include        "dt.h" 
    3333 
    3434#include        "aa.h" 
    3535#include        "tinfo.h" 
    3636 
    3737#if ELFOBJ 
    3838 
    3939#include        "dwarf.h" 
     40 
     41#include        "aa.h" 
     42#include        "tinfo.h" 
    4043 
    4144//#define DEBSYM 0x7E 
    4245 
    4346static Outbuffer *fobjbuf; 
    4447 
    4548regm_t BYTEREGS = BYTEREGS_INIT; 
    4649regm_t ALLREGS = ALLREGS_INIT; 
    4750 
    4851static char __file__[] = __FILE__;      // for tassert.h 
    4952#include        "tassert.h" 
    5053 
    5154#define MATCH_SECTION 1 
    5255 
    5356#define DEST_LEN (IDMAX + IDOHD + 1) 
    5457char *obj_mangle2(Symbol *s,char *dest); 
    5558 
    5659#if MARS 
    5760// C++ name mangling is handled by front end 
    5861#define cpp_mangle(s) ((s)->Sident) 
    5962#endif 
     
    106109static IDXSYM elf_addsym(IDXSTR sym, targ_size_t val, unsigned sz, 
    107110                        unsigned typ,unsigned bind,IDXSEC sec); 
    108111static long elf_align(FILE *fd, targ_size_t size, long offset); 
    109112 
    110113// The object file is built is several separate pieces 
    111114 
    112115// Non-repeatable section types have single output buffers 
    113116//      Pre-allocated buffers are defined for: 
    114117//              Section Names string table 
    115118//              Section Headers table 
    116119//              Symbol table 
    117120//              String table 
    118121//              Notes section 
    119122//              Comment data 
    120123 
    121124// Section Names  - String table for section names only 
    122125static Outbuffer *section_names; 
    123126#define SEC_NAMES_INIT  800 
    124127#define SEC_NAMES_INC   400 
    125128 
     129// Hash table for section_names 
     130AArray *section_names_hashtable; 
     131 
     132/* ====================== Cached Strings in section_names ================= */ 
     133 
     134struct TypeInfo_Idxstr : TypeInfo 
     135{ 
     136    const char* toString(); 
     137    hash_t getHash(void *p); 
     138    int equals(void *p1, void *p2); 
     139    int compare(void *p1, void *p2); 
     140    size_t tsize(); 
     141    void swap(void *p1, void *p2); 
     142}; 
     143 
     144TypeInfo_Idxstr ti_idxstr; 
     145 
     146const char* TypeInfo_Idxstr::toString() 
     147{ 
     148    return "IDXSTR"; 
     149} 
     150 
     151hash_t TypeInfo_Idxstr::getHash(void *p) 
     152{ 
     153    IDXSTR a = *(IDXSTR *)p; 
     154    hash_t hash = 0; 
     155    for (const char *s = (char *)(section_names->buf + a); 
     156         *s; 
     157         s++) 
     158    { 
     159        hash = hash * 11 + *s; 
     160    } 
     161    return hash; 
     162} 
     163 
     164int TypeInfo_Idxstr::equals(void *p1, void *p2) 
     165{ 
     166    IDXSTR a1 = *(IDXSTR*)p1; 
     167    IDXSTR a2 = *(IDXSTR*)p2; 
     168    const char *s1 = (char *)(section_names->buf + a1); 
     169    const char *s2 = (char *)(section_names->buf + a2); 
     170 
     171    return strcmp(s1, s2) == 0; 
     172} 
     173 
     174int TypeInfo_Idxstr::compare(void *p1, void *p2) 
     175{ 
     176    IDXSTR a1 = *(IDXSTR*)p1; 
     177    IDXSTR a2 = *(IDXSTR*)p2; 
     178    const char *s1 = (char *)(section_names->buf + a1); 
     179    const char *s2 = (char *)(section_names->buf + a2); 
     180 
     181    return strcmp(s1, s2); 
     182} 
     183 
     184size_t TypeInfo_Idxstr::tsize() 
     185{ 
     186    return sizeof(IDXSTR); 
     187} 
     188 
     189void TypeInfo_Idxstr::swap(void *p1, void *p2) 
     190{ 
     191    assert(0); 
     192} 
     193 
     194 
     195/* ======================================================================== */ 
     196 
    126197// String Table  - String table for all other names 
    127198static Outbuffer *symtab_strings; 
    128199 
    129200 
    130201// Section Headers 
    131202Outbuffer  *SECbuf;             // Buffer to build section table in 
    132203#define SecHdrTab ((Elf32_Shdr *)SECbuf->buf) 
    133204#define GET_SECTION(secidx) (SecHdrTab + secidx) 
    134205#define GET_SECTION_NAME(secidx) (section_names->buf + SecHdrTab[secidx].sh_name) 
    135206 
    136207// The relocation for text and data seems to get lost. 
    137208// Try matching the order gcc output them 
    138209// This means defining the sections and then removing them if they are 
    139210// not used. 
    140211static int section_cnt; // Number of sections in table 
    141212 
    142213#define SHI_TEXT        1 
    143214#define SHI_RELTEXT     2 
    144215#define SHI_DATA        3 
    145216#define SHI_RELDATA     4 
     
    236307IDXSTR elf_addstr(Outbuffer *strtab, const char *str) 
    237308{ 
    238309    //dbg_printf("elf_addstr(strtab = x%x str = '%s')\n",strtab,str); 
    239310    IDXSTR idx = strtab->size();        // remember starting offset 
    240311    strtab->writeString(str); 
    241312    //dbg_printf("\tidx %d, new size %d\n",idx,strtab->size()); 
    242313    return idx; 
    243314} 
    244315 
    245316/******************************* 
    246317 * Find a string in a string table 
    247318 * Input: 
    248319 *      strtab  =       string table for entry 
    249320 *      str     =       string to find 
    250321 * 
    251322 * Returns index into the specified string table or 0. 
    252323 */ 
    253324 
    254325static IDXSTR elf_findstr(Outbuffer *strtab, const char *str, const char *suffix) 
    255326{ 
     327    //printf("elf_findstr(strtab = %p, str = %s, suffix = %s\n", strtab, str ? str : "", suffix ? suffix : ""); 
     328 
     329    size_t len = strlen(str); 
     330 
     331    // Combine str~suffix and have buf point to the combination 
     332#ifdef DEBUG 
     333    char tmpbuf[25];    // to exercise the alloca() code path 
     334#else 
     335    char tmpbuf[1024];  // the alloca() code path is slow 
     336#endif 
     337    const char *buf; 
     338    if (suffix) 
     339    { 
     340        size_t suffixlen = strlen(suffix); 
     341        if (len + suffixlen >= sizeof(tmpbuf)) 
     342        { 
     343             buf = (char *)alloca(len + suffixlen + 1); 
     344             assert(buf); 
     345        } 
     346        else 
     347        { 
     348             buf = tmpbuf; 
     349        } 
     350        memcpy((char *)buf, str, len); 
     351        memcpy((char *)buf + len, suffix, suffixlen + 1); 
     352        len += suffixlen; 
     353    } 
     354    else 
     355        buf = str; 
     356 
     357    // Linear search, slow 
    256358    const char *ent = (char *)strtab->buf+1; 
    257359    const char *pend = ent+strtab->size() - 1; 
    258     const char *s = str; 
    259     const char *sx = suffix; 
    260     int len = strlen(str); 
    261  
    262     if (suffix) 
    263         len += strlen(suffix); 
    264  
    265     while(ent < pend) 
    266     { 
    267         if(*ent == 0)                   // end of table entry 
    268         { 
    269             if(*s == 0 && !sx)          // end of string - found a match 
    270             { 
    271                 return ent - (const char *)strtab->buf - len; 
    272             } 
    273             else                        // table entry too short 
    274             { 
    275                 s = str;                // back to beginning of string 
    276                 sx = suffix; 
    277                 ent++;                  // start of next table entry 
    278             } 
    279         } 
    280         else if (*s == 0 && sx && *sx == *ent) 
    281         {                               // matched first string 
    282             s = sx+1;                   // switch to suffix 
    283             ent++; 
    284             sx = NULL; 
    285         } 
    286         else                            // continue comparing 
    287         { 
    288             if (*ent == *s) 
    289             {                           // Have a match going 
    290                 ent++; 
    291                 s++; 
    292             } 
    293             else                        // no match 
    294             { 
    295                 while(*ent != 0)        // skip to end of entry 
    296                     ent++; 
    297                 ent++;                  // start of next table entry 
    298                 s = str;                // back to beginning of string 
    299                 sx = suffix; 
    300             } 
    301         } 
     360    while (ent + len < pend) 
     361    { 
     362        if (memcmp(buf, ent, len + 1) == 0) 
     363            return ent - (const char *)strtab->buf; 
     364        ent = (const char *)memchr(ent, 0, pend - ent); 
     365        ent += 1; 
    302366    } 
    303367    return 0;                   // never found match 
    304368} 
    305369 
    306370/******************************* 
    307371 * Output a mangled string into the symbol string table 
    308372 * Input: 
    309373 *      str     =       string to add 
    310374 * 
    311375 * Returns index into the table. 
    312376 */ 
    313377 
    314378static IDXSTR elf_addmangled(Symbol *s) 
    315379{ 
    316380    //printf("elf_addmangled(%s)\n", s->Sident); 
    317381    char dest[DEST_LEN]; 
    318382    char *destr; 
    319383    const char *name; 
    320384    int len; 
    321385    IDXSTR namidx; 
     
    450514    sec.sh_flags = flags; 
    451515    sec.sh_addr = addr; 
    452516    sec.sh_offset = offset; 
    453517    sec.sh_size = size; 
    454518    sec.sh_link = link; 
    455519    sec.sh_info = info; 
    456520    sec.sh_addralign = addralign; 
    457521    sec.sh_entsize = entsize; 
    458522 
    459523    if (!SECbuf) 
    460524    {   SECbuf = new Outbuffer(4 * sizeof(Elf32_Shdr)); 
    461525        SECbuf->reserve(16 * sizeof(Elf32_Shdr)); 
    462526    } 
    463527    SECbuf->write((void *)&sec, sizeof(sec)); 
    464528    return section_cnt++; 
    465529} 
    466530 
    467531static IDXSEC elf_newsection(const char *name, const char *suffix, 
    468532        elf_u32_f32 type, elf_u32_f32 flags) 
    469533{ 
    470     Elf32_Shdr sec; 
    471  
    472 //    dbg_printf("elf_newsection(%s,%s,type %d, flags x%x)\n", 
    473 //        name?name:"",suffix?suffix:"",type,flags); 
    474  
    475 #if 1 
    476     int namidx = elf_addstr(section_names,name); 
    477 #else 
    478     int namidx = section_names->size(); 
     534    // dbg_printf("elf_newsection(%s,%s,type %d, flags x%x)\n", 
     535    //        name?name:"",suffix?suffix:"",type,flags); 
     536 
     537    IDXSTR namidx = section_names->size(); 
    479538    section_names->writeString(name); 
    480 #endif 
    481                                         // name in section names table 
    482     if (suffix)                         // suffix - back up over NUL and 
    483     {                                   //          append suffix string 
    484         section_names->setsize(section_names->size()-1); 
     539    if (suffix) 
     540    {   // Append suffix string 
     541        section_names->setsize(section_names->size() - 1);  // back up over terminating 0 
    485542        section_names->writeString(suffix); 
    486     }; 
     543    } 
     544    IDXSTR *pidx = (IDXSTR *)section_names_hashtable->get(&namidx); 
     545    assert(!*pidx);             // must not already exist 
     546    *pidx = namidx; 
     547 
    487548    return elf_newsection2(namidx,type,flags,0,0,0,0,0,0,0); 
    488549} 
    489550 
    490551/************************** 
    491552 * Ouput read only data and generate a symbol for it. 
    492553 * 
    493554 */ 
    494555 
    495556symbol *elf_sym_cdata(tym_t ty,char *p,int len) 
    496557{ 
    497558    symbol *s; 
    498559 
    499560#if 0 
    500561    if (OPT_IS_SET(OPTfwritable_strings)) 
    501562    { 
    502563        alignOffset(DATA, tysize(ty)); 
    503564        s = symboldata(Doffset, ty); 
    504565        SegData[DATA]->SDbuf->write(p,len); 
    505566        s->Sseg = DATA; 
    506567        s->Soffset = Doffset;   // Remember its offset into DATA section 
     
    597658        #define NAMIDX_STRTAB    9       // .strtab 
    598659        #define NAMIDX_SHSTRTAB 17      // .shstrtab 
    599660        #define NAMIDX_TEXT     27      // .text 
    600661        #define NAMIDX_DATA     33      // .data 
    601662        #define NAMIDX_BSS      39      // .bss 
    602663        #define NAMIDX_NOTE     44      // .note 
    603664        #define NAMIDX_COMMENT  50      // .comment 
    604665        #define NAMIDX_RODATA   59      // .rodata 
    605666        #define NAMIDX_RELTEXT  67      // .rel.text and .rela.text 
    606667        #define NAMIDX_RELDATA  77      // .rel.data 
    607668        #define NAMIDX_RELDATA64 78      // .rela.data 
    608669 
    609670        if (section_names) 
    610671            section_names->setsize(sizeof(section_names_init64)); 
    611672        else 
    612673        {   section_names = new Outbuffer(512); 
    613674            section_names->reserve(1024); 
    614675            section_names->writen(section_names_init64, sizeof(section_names_init64)); 
    615676        } 
    616677 
     678        if (section_names_hashtable) 
     679            delete section_names_hashtable; 
     680        section_names_hashtable = new AArray(&ti_idxstr, sizeof(IDXSTR)); 
     681 
    617682        // name,type,flags,addr,offset,size,link,info,addralign,entsize 
    618683        elf_newsection2(0,               SHT_NULL,   0,                 0,0,0,0,0, 0,0); 
    619684        elf_newsection2(NAMIDX_TEXT,SHT_PROGDEF,SHF_ALLOC|SHF_EXECINSTR,0,0,0,0,0, 4,0); 
    620685        elf_newsection2(NAMIDX_RELTEXT,SHT_RELA, 0,0,0,0,SHI_SYMTAB,     SHI_TEXT, 8,8); 
    621686        elf_newsection2(NAMIDX_DATA,SHT_PROGDEF,SHF_ALLOC|SHF_WRITE,    0,0,0,0,0, 8,0); 
    622687        elf_newsection2(NAMIDX_RELDATA64,SHT_RELA, 0,0,0,0,SHI_SYMTAB,   SHI_DATA, 8,8); 
    623688        elf_newsection2(NAMIDX_BSS, SHT_NOBITS,SHF_ALLOC|SHF_WRITE,     0,0,0,0,0, 16,0); 
    624689        elf_newsection2(NAMIDX_RODATA,SHT_PROGDEF,SHF_ALLOC,            0,0,0,0,0, 1,0); 
    625690        elf_newsection2(NAMIDX_STRTAB,SHT_STRTAB, 0,                    0,0,0,0,0, 1,0); 
    626691        elf_newsection2(NAMIDX_SYMTAB,SHT_SYMTAB, 0,                    0,0,0,0,0, 8,0); 
    627692        elf_newsection2(NAMIDX_SHSTRTAB,SHT_STRTAB, 0,                  0,0,0,0,0, 1,0); 
    628693        elf_newsection2(NAMIDX_COMMENT, SHT_PROGDEF,0,                  0,0,0,0,0, 1,0); 
    629694        elf_newsection2(NAMIDX_NOTE,SHT_NOTE,   0,                      0,0,0,0,0, 1,0); 
     695 
     696        IDXSTR namidx; 
     697        namidx = NAMIDX_TEXT;      *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx; 
     698        namidx = NAMIDX_RELTEXT;   *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx; 
     699        namidx = NAMIDX_DATA;      *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx; 
     700        namidx = NAMIDX_RELDATA64; *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx; 
     701        namidx = NAMIDX_BSS;       *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx; 
     702        namidx = NAMIDX_RODATA;    *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx; 
     703        namidx = NAMIDX_STRTAB;    *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx; 
     704        namidx = NAMIDX_SYMTAB;    *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx; 
     705        namidx = NAMIDX_SHSTRTAB;  *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx; 
     706        namidx = NAMIDX_COMMENT;   *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx; 
     707        namidx = NAMIDX_NOTE;      *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx; 
    630708    } 
    631709    else 
    632710    { 
    633711        static char section_names_init[] = 
    634712          "\0.symtab\0.strtab\0.shstrtab\0.text\0.data\0.bss\0.note\0.comment\0.rodata\0.rel.text\0.rel.data"; 
    635713 
    636714        if (section_names) 
    637715            section_names->setsize(sizeof(section_names_init)); 
    638716        else 
    639717        {   section_names = new Outbuffer(512); 
    640             section_names->reserve(1024); 
     718            section_names->reserve(100*1024); 
    641719            section_names->writen(section_names_init, sizeof(section_names_init)); 
    642720        } 
     721 
     722        if (section_names_hashtable) 
     723            delete section_names_hashtable; 
     724        section_names_hashtable = new AArray(&ti_idxstr, sizeof(IDXSTR)); 
    643725 
    644726        // name,type,flags,addr,offset,size,link,info,addralign,entsize 
    645727        elf_newsection2(0,               SHT_NULL,   0,                 0,0,0,0,0, 0,0); 
    646728        elf_newsection2(NAMIDX_TEXT,SHT_PROGDEF,SHF_ALLOC|SHF_EXECINSTR,0,0,0,0,0, 16,0); 
    647729        elf_newsection2(NAMIDX_RELTEXT,SHT_REL, 0,0,0,0,SHI_SYMTAB,      SHI_TEXT, 4,8); 
    648730        elf_newsection2(NAMIDX_DATA,SHT_PROGDEF,SHF_ALLOC|SHF_WRITE,    0,0,0,0,0, 4,0); 
    649731        elf_newsection2(NAMIDX_RELDATA,SHT_REL, 0,0,0,0,SHI_SYMTAB,      SHI_DATA, 4,8); 
    650732        elf_newsection2(NAMIDX_BSS, SHT_NOBITS,SHF_ALLOC|SHF_WRITE,     0,0,0,0,0, 32,0); 
    651733        elf_newsection2(NAMIDX_RODATA,SHT_PROGDEF,SHF_ALLOC,            0,0,0,0,0, 1,0); 
    652734        elf_newsection2(NAMIDX_STRTAB,SHT_STRTAB, 0,                    0,0,0,0,0, 1,0); 
    653735        elf_newsection2(NAMIDX_SYMTAB,SHT_SYMTAB, 0,                    0,0,0,0,0, 4,0); 
    654736        elf_newsection2(NAMIDX_SHSTRTAB,SHT_STRTAB, 0,                  0,0,0,0,0, 1,0); 
    655737        elf_newsection2(NAMIDX_COMMENT, SHT_PROGDEF,0,                  0,0,0,0,0, 1,0); 
    656738        elf_newsection2(NAMIDX_NOTE,SHT_NOTE,   0,                      0,0,0,0,0, 1,0); 
     739 
     740        IDXSTR namidx; 
     741        namidx = NAMIDX_TEXT;      *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx; 
     742        namidx = NAMIDX_RELTEXT;   *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx; 
     743        namidx = NAMIDX_DATA;      *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx; 
     744        namidx = NAMIDX_RELDATA;   *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx; 
     745        namidx = NAMIDX_BSS;       *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx; 
     746        namidx = NAMIDX_RODATA;    *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx; 
     747        namidx = NAMIDX_STRTAB;    *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx; 
     748        namidx = NAMIDX_SYMTAB;    *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx; 
     749        namidx = NAMIDX_SHSTRTAB;  *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx; 
     750        namidx = NAMIDX_COMMENT;   *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx; 
     751        namidx = NAMIDX_NOTE;      *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx; 
    657752    } 
    658753 
    659754    if (SYMbuf) 
    660755        SYMbuf->setsize(0); 
    661756    symbol_idx = 0; 
    662757    local_cnt = 0; 
    663758    // The symbols that every object file has 
    664759    elf_addsym(0, 0, 0, STT_NOTYPE,  STB_LOCAL, 0); 
    665760    elf_addsym(0, 0, 0, STT_FILE,    STB_LOCAL, SHT_ABS);       // STI_FILE 
    666761    elf_addsym(0, 0, 0, STT_SECTION, STB_LOCAL, SHI_TEXT);      // STI_TEXT 
    667762    elf_addsym(0, 0, 0, STT_SECTION, STB_LOCAL, SHI_DATA);      // STI_DATA 
    668763    elf_addsym(0, 0, 0, STT_SECTION, STB_LOCAL, SHI_BSS);       // STI_BSS 
    669764    elf_addsym(0, 0, 0, STT_NOTYPE,  STB_LOCAL, SHI_TEXT);      // STI_GCC 
    670765    elf_addsym(0, 0, 0, STT_SECTION, STB_LOCAL, SHI_RODAT);     // STI_RODAT 
    671766    elf_addsym(0, 0, 0, STT_SECTION, STB_LOCAL, SHI_NOTE);      // STI_NOTE 
    672767    elf_addsym(0, 0, 0, STT_SECTION, STB_LOCAL, SHI_COM);       // STI_COM 
    673768 
    674769    // Initialize output buffers for CODE, DATA and COMMENTS 
    675770    //      (NOTE not supported, BSS not required) 
    676771 
     
    15391634            pseg->SDbuf->reserve(1024); 
    15401635        } 
    15411636    } 
    15421637    if (pseg->SDrel) 
    15431638        pseg->SDrel->setsize(0); 
    15441639    pseg->SDsymidx = symidx; 
    15451640    pseg->SDrelidx = relidx; 
    15461641    pseg->SDrelmaxoff = 0; 
    15471642    pseg->SDrelindex = 0; 
    15481643    pseg->SDrelcnt = 0; 
    15491644    pseg->SDshtidxout = 0; 
    15501645    pseg->SDsym = NULL; 
    15511646    return seg; 
    15521647} 
    15531648 
    15541649int elf_getsegment(const char *name, const char *suffix, int type, int flags, 
    15551650        int align) 
    15561651{ 
    15571652    //printf("elf_getsegment(%s,%s,flags %x, align %d)\n",name,suffix,flags,align); 
    15581653 
    1559     IDXSTR namidx; 
    1560     if (namidx = elf_findstr(section_names,name,suffix)) 
    1561     {                                   // this section name exists 
     1654    // Add name~suffix to the section_names table 
     1655    IDXSTR namidx = section_names->size(); 
     1656    section_names->writeString(name); 
     1657    if (suffix) 
     1658    {   // Append suffix string 
     1659        section_names->setsize(section_names->size() - 1);  // back up over terminating 0 
     1660        section_names->writeString(suffix); 
     1661    } 
     1662    IDXSTR *pidx = (IDXSTR *)section_names_hashtable->get(&namidx); 
     1663    if (*pidx) 
     1664    {   // this section name already exists 
     1665        section_names->setsize(namidx);                 // remove addition 
     1666        namidx = *pidx; 
    15621667        for (int seg = CODE; seg <= seg_count; seg++) 
    15631668        {                               // should be in segment table 
    15641669            if (MAP_SEG2SEC(seg)->sh_name == namidx) 
    15651670            { 
    15661671                return seg;             // found section for segment 
    15671672            } 
    15681673        } 
    15691674        assert(0);      // but it's not a segment 
    15701675        // FIX - should be an error message conflict with section names 
    15711676    } 
     1677    *pidx = namidx; 
    15721678 
    15731679    //dbg_printf("\tNew segment - %d size %d\n", seg,SegData[seg]->SDbuf); 
    1574     IDXSEC shtidx = elf_newsection(name,suffix,type,flags); 
     1680    IDXSEC shtidx = elf_newsection2(namidx,type,flags,0,0,0,0,0,0,0); 
    15751681    SecHdrTab[shtidx].sh_addralign = align; 
    15761682    IDXSYM symidx = elf_addsym(0, 0, 0, STT_SECTION, STB_LOCAL, shtidx); 
    15771683    int seg = elf_getsegment2(shtidx, symidx, 0); 
    15781684    //printf("-elf_getsegment() = %d\n", seg); 
    15791685    return seg; 
    15801686} 
    15811687 
    15821688/******************************** 
    15831689 * Define a new code segment. 
    15841690 * Input: 
    15851691 *      name            name of segment, if NULL then revert to default 
    15861692 *      suffix  0       use name as is 
    15871693 *              1       append "_TEXT" to name 
    15881694 * Output: 
    15891695 *      cseg            segment index of new current code segment 
    15901696 *      Coffset         starting offset in cseg 
    15911697 * Returns: 
    15921698 *      segment index of newly created code segment 
    15931699 */ 
    15941700 
  • trunk/src/backend/elfobj.c

    r589 r628  
    2020#include        <alloca.h> 
    2121#endif 
    2222 
    2323#include        "cc.h" 
    2424#include        "global.h" 
    2525#include        "code.h" 
    2626#include        "type.h" 
    2727#include        "melf.h" 
    2828#include        "outbuf.h" 
    2929#include        "filespec.h" 
    3030#include        "cv4.h" 
    3131#include        "cgcv.h" 
    3232#include        "dt.h" 
    3333 
    3434#include        "aa.h" 
    3535#include        "tinfo.h" 
    3636 
    3737#if ELFOBJ 
    3838 
    3939#include        "dwarf.h" 
     40 
     41#include        "aa.h" 
     42#include        "tinfo.h" 
    4043 
    4144//#define DEBSYM 0x7E 
    4245 
    4346static Outbuffer *fobjbuf; 
    4447 
    4548regm_t BYTEREGS = BYTEREGS_INIT; 
    4649regm_t ALLREGS = ALLREGS_INIT; 
    4750 
    4851static char __file__[] = __FILE__;      // for tassert.h 
    4952#include        "tassert.h" 
    5053 
    5154#define MATCH_SECTION 1 
    5255 
    5356#define DEST_LEN (IDMAX + IDOHD + 1) 
    5457char *obj_mangle2(Symbol *s,char *dest); 
    5558 
    5659#if MARS 
    5760// C++ name mangling is handled by front end 
    5861#define cpp_mangle(s) ((s)->Sident) 
    5962#endif 
     
    106109static IDXSYM elf_addsym(IDXSTR sym, targ_size_t val, unsigned sz, 
    107110                        unsigned typ,unsigned bind,IDXSEC sec); 
    108111static long elf_align(FILE *fd, targ_size_t size, long offset); 
    109112 
    110113// The object file is built is several separate pieces 
    111114 
    112115// Non-repeatable section types have single output buffers 
    113116//      Pre-allocated buffers are defined for: 
    114117//              Section Names string table 
    115118//              Section Headers table 
    116119//              Symbol table 
    117120//              String table 
    118121//              Notes section 
    119122//              Comment data 
    120123 
    121124// Section Names  - String table for section names only 
    122125static Outbuffer *section_names; 
    123126#define SEC_NAMES_INIT  800 
    124127#define SEC_NAMES_INC   400 
    125128 
     129// Hash table for section_names 
     130AArray *section_names_hashtable; 
     131 
     132/* ====================== Cached Strings in section_names ================= */ 
     133 
     134struct TypeInfo_Idxstr : TypeInfo 
     135{ 
     136    const char* toString(); 
     137    hash_t getHash(void *p); 
     138    int equals(void *p1, void *p2); 
     139    int compare(void *p1, void *p2); 
     140    size_t tsize(); 
     141    void swap(void *p1, void *p2); 
     142}; 
     143 
     144TypeInfo_Idxstr ti_idxstr; 
     145 
     146const char* TypeInfo_Idxstr::toString() 
     147{ 
     148    return "IDXSTR"; 
     149} 
     150 
     151hash_t TypeInfo_Idxstr::getHash(void *p) 
     152{ 
     153    IDXSTR a = *(IDXSTR *)p; 
     154    hash_t hash = 0; 
     155    for (const char *s = (char *)(section_names->buf + a); 
     156         *s; 
     157         s++) 
     158    { 
     159        hash = hash * 11 + *s; 
     160    } 
     161    return hash; 
     162} 
     163 
     164int TypeInfo_Idxstr::equals(void *p1, void *p2) 
     165{ 
     166    IDXSTR a1 = *(IDXSTR*)p1; 
     167    IDXSTR a2 = *(IDXSTR*)p2; 
     168    const char *s1 = (char *)(section_names->buf + a1); 
     169    const char *s2 = (char *)(section_names->buf + a2); 
     170 
     171    return strcmp(s1, s2) == 0; 
     172} 
     173 
     174int TypeInfo_Idxstr::compare(void *p1, void *p2) 
     175{ 
     176    IDXSTR a1 = *(IDXSTR*)p1; 
     177    IDXSTR a2 = *(IDXSTR*)p2; 
     178    const char *s1 = (char *)(section_names->buf + a1); 
     179    const char *s2 = (char *)(section_names->buf + a2); 
     180 
     181    return strcmp(s1, s2); 
     182} 
     183 
     184size_t TypeInfo_Idxstr::tsize() 
     185{ 
     186    return sizeof(IDXSTR); 
     187} 
     188 
     189void TypeInfo_Idxstr::swap(void *p1, void *p2) 
     190{ 
     191    assert(0); 
     192} 
     193 
     194 
     195/* ======================================================================== */ 
     196 
    126197// String Table  - String table for all other names 
    127198static Outbuffer *symtab_strings; 
    128199 
    129200 
    130201// Section Headers 
    131202Outbuffer  *SECbuf;             // Buffer to build section table in 
    132203#define SecHdrTab ((Elf32_Shdr *)SECbuf->buf) 
    133204#define GET_SECTION(secidx) (SecHdrTab + secidx) 
    134205#define GET_SECTION_NAME(secidx) (section_names->buf + SecHdrTab[secidx].sh_name) 
    135206 
    136207// The relocation for text and data seems to get lost. 
    137208// Try matching the order gcc output them 
    138209// This means defining the sections and then removing them if they are 
    139210// not used. 
    140211static int section_cnt; // Number of sections in table 
    141212 
    142213#define SHI_TEXT        1 
    143214#define SHI_RELTEXT     2 
    144215#define SHI_DATA        3 
    145216#define SHI_RELDATA     4 
     
    236307IDXSTR elf_addstr(Outbuffer *strtab, const char *str) 
    237308{ 
    238309    //dbg_printf("elf_addstr(strtab = x%x str = '%s')\n",strtab,str); 
    239310    IDXSTR idx = strtab->size();        // remember starting offset 
    240311    strtab->writeString(str); 
    241312    //dbg_printf("\tidx %d, new size %d\n",idx,strtab->size()); 
    242313    return idx; 
    243314} 
    244315 
    245316/******************************* 
    246317 * Find a string in a string table 
    247318 * Input: 
    248319 *      strtab  =       string table for entry 
    249320 *      str     =       string to find 
    250321 * 
    251322 * Returns index into the specified string table or 0. 
    252323 */ 
    253324 
    254325static IDXSTR elf_findstr(Outbuffer *strtab, const char *str, const char *suffix) 
    255326{ 
     327    //printf("elf_findstr(strtab = %p, str = %s, suffix = %s\n", strtab, str ? str : "", suffix ? suffix : ""); 
     328 
     329    size_t len = strlen(str); 
     330 
     331    // Combine str~suffix and have buf point to the combination 
     332#ifdef DEBUG 
     333    char tmpbuf[25];    // to exercise the alloca() code path 
     334#else 
     335    char tmpbuf[1024];  // the alloca() code path is slow 
     336#endif 
     337    const char *buf; 
     338    if (suffix) 
     339    { 
     340        size_t suffixlen = strlen(suffix); 
     341        if (len + suffixlen >= sizeof(tmpbuf)) 
     342        { 
     343             buf = (char *)alloca(len + suffixlen + 1); 
     344             assert(buf); 
     345        } 
     346        else 
     347        { 
     348             buf = tmpbuf; 
     349        } 
     350        memcpy((char *)buf, str, len); 
     351        memcpy((char *)buf + len, suffix, suffixlen + 1); 
     352        len += suffixlen; 
     353    } 
     354    else 
     355        buf = str; 
     356 
     357    // Linear search, slow 
    256358    const char *ent = (char *)strtab->buf+1; 
    257359    const char *pend = ent+strtab->size() - 1; 
    258     const char *s = str; 
    259     const char *sx = suffix; 
    260     int len = strlen(str); 
    261  
    262     if (suffix) 
    263         len += strlen(suffix); 
    264  
    265     while(ent < pend) 
    266     { 
    267         if(*ent == 0)                   // end of table entry 
    268         { 
    269             if(*s == 0 && !sx)          // end of string - found a match 
    270             { 
    271                 return ent - (const char *)strtab->buf - len; 
    272             } 
    273             else                        // table entry too short 
    274             { 
    275                 s = str;                // back to beginning of string 
    276                 sx = suffix; 
    277                 ent++;                  // start of next table entry 
    278             } 
    279         } 
    280         else if (*s == 0 && sx && *sx == *ent) 
    281         {                               // matched first string 
    282             s = sx+1;                   // switch to suffix 
    283             ent++; 
    284             sx = NULL; 
    285         } 
    286         else                            // continue comparing 
    287         { 
    288             if (*ent == *s) 
    289             {                           // Have a match going 
    290                 ent++; 
    291                 s++; 
    292             } 
    293             else                        // no match 
    294             { 
    295                 while(*ent != 0)        // skip to end of entry 
    296                     ent++; 
    297                 ent++;                  // start of next table entry 
    298                 s = str;                // back to beginning of string 
    299                 sx = suffix; 
    300             } 
    301         } 
     360    while (ent + len < pend) 
     361    { 
     362        if (memcmp(buf, ent, len + 1) == 0) 
     363            return ent - (const char *)strtab->buf; 
     364        ent = (const char *)memchr(ent, 0, pend - ent); 
     365        ent += 1; 
    302366    } 
    303367    return 0;                   // never found match 
    304368} 
    305369 
    306370/******************************* 
    307371 * Output a mangled string into the symbol string table 
    308372 * Input: 
    309373 *      str     =       string to add 
    310374 * 
    311375 * Returns index into the table. 
    312376 */ 
    313377 
    314378static IDXSTR elf_addmangled(Symbol *s) 
    315379{ 
    316380    //printf("elf_addmangled(%s)\n", s->Sident); 
    317381    char dest[DEST_LEN]; 
    318382    char *destr; 
    319383    const char *name; 
    320384    int len; 
    321385    IDXSTR namidx; 
     
    450514    sec.sh_flags = flags; 
    451515    sec.sh_addr = addr; 
    452516    sec.sh_offset = offset; 
    453517    sec.sh_size = size; 
    454518    sec.sh_link = link; 
    455519    sec.sh_info = info; 
    456520    sec.sh_addralign = addralign; 
    457521    sec.sh_entsize = entsize; 
    458522 
    459523    if (!SECbuf) 
    460524    {   SECbuf = new Outbuffer(4 * sizeof(Elf32_Shdr)); 
    461525        SECbuf->reserve(16 * sizeof(Elf32_Shdr)); 
    462526    } 
    463527    SECbuf->write((void *)&sec, sizeof(sec)); 
    464528    return section_cnt++; 
    465529} 
    466530 
    467531static IDXSEC elf_newsection(const char *name, const char *suffix, 
    468532        elf_u32_f32 type, elf_u32_f32 flags) 
    469533{ 
    470     Elf32_Shdr sec; 
    471  
    472 //    dbg_printf("elf_newsection(%s,%s,type %d, flags x%x)\n", 
    473 //        name?name:"",suffix?suffix:"",type,flags); 
    474  
    475 #if 1 
    476     int namidx = elf_addstr(section_names,name); 
    477 #else 
    478     int namidx = section_names->size(); 
     534    // dbg_printf("elf_newsection(%s,%s,type %d, flags x%x)\n", 
     535    //        name?name:"",suffix?suffix:"",type,flags); 
     536 
     537    IDXSTR namidx = section_names->size(); 
    479538    section_names->writeString(name); 
    480 #endif 
    481                                         // name in section names table 
    482     if (suffix)                         // suffix - back up over NUL and 
    483     {                                   //          append suffix string 
    484         section_names->setsize(section_names->size()-1); 
     539    if (suffix) 
     540    {   // Append suffix string 
     541        section_names->setsize(section_names->size() - 1);  // back up over terminating 0 
    485542        section_names->writeString(suffix); 
    486     }; 
     543    } 
     544    IDXSTR *pidx = (IDXSTR *)section_names_hashtable->get(&namidx); 
     545    assert(!*pidx);             // must not already exist 
     546    *pidx = namidx; 
     547 
    487548    return elf_newsection2(namidx,type,flags,0,0,0,0,0,0,0); 
    488549} 
    489550 
    490551/************************** 
    491552 * Ouput read only data and generate a symbol for it. 
    492553 * 
    493554 */ 
    494555 
    495556symbol *elf_sym_cdata(tym_t ty,char *p,int len) 
    496557{ 
    497558    symbol *s; 
    498559 
    499560#if 0 
    500561    if (OPT_IS_SET(OPTfwritable_strings)) 
    501562    { 
    502563        alignOffset(DATA, tysize(ty)); 
    503564        s = symboldata(Doffset, ty); 
    504565        SegData[DATA]->SDbuf->write(p,len); 
    505566        s->Sseg = DATA; 
    506567        s->Soffset = Doffset;   // Remember its offset into DATA section 
     
    597658        #define NAMIDX_STRTAB    9       // .strtab 
    598659        #define NAMIDX_SHSTRTAB 17      // .shstrtab 
    599660        #define NAMIDX_TEXT     27      // .text 
    600661        #define NAMIDX_DATA     33      // .data 
    601662        #define NAMIDX_BSS      39      // .bss 
    602663        #define NAMIDX_NOTE     44      // .note 
    603664        #define NAMIDX_COMMENT  50      // .comment 
    604665        #define NAMIDX_RODATA   59      // .rodata 
    605666        #define NAMIDX_RELTEXT  67      // .rel.text and .rela.text 
    606667        #define NAMIDX_RELDATA  77      // .rel.data 
    607668        #define NAMIDX_RELDATA64 78      // .rela.data 
    608669 
    609670        if (section_names) 
    610671            section_names->setsize(sizeof(section_names_init64)); 
    611672        else 
    612673        {   section_names = new Outbuffer(512); 
    613674            section_names->reserve(1024); 
    614675            section_names->writen(section_names_init64, sizeof(section_names_init64)); 
    615676        } 
    616677 
     678        if (section_names_hashtable) 
     679            delete section_names_hashtable; 
     680        section_names_hashtable = new AArray(&ti_idxstr, sizeof(IDXSTR)); 
     681 
    617682        // name,type,flags,addr,offset,size,link,info,addralign,entsize 
    618683        elf_newsection2(0,               SHT_NULL,   0,                 0,0,0,0,0, 0,0); 
    619684        elf_newsection2(NAMIDX_TEXT,SHT_PROGDEF,SHF_ALLOC|SHF_EXECINSTR,0,0,0,0,0, 4,0); 
    620685        elf_newsection2(NAMIDX_RELTEXT,SHT_RELA, 0,0,0,0,SHI_SYMTAB,     SHI_TEXT, 8,8); 
    621686        elf_newsection2(NAMIDX_DATA,SHT_PROGDEF,SHF_ALLOC|SHF_WRITE,    0,0,0,0,0, 8,0); 
    622687        elf_newsection2(NAMIDX_RELDATA64,SHT_RELA, 0,0,0,0,SHI_SYMTAB,   SHI_DATA, 8,8); 
    623688        elf_newsection2(NAMIDX_BSS, SHT_NOBITS,SHF_ALLOC|SHF_WRITE,     0,0,0,0,0, 16,0); 
    624689        elf_newsection2(NAMIDX_RODATA,SHT_PROGDEF,SHF_ALLOC,            0,0,0,0,0, 1,0); 
    625690        elf_newsection2(NAMIDX_STRTAB,SHT_STRTAB, 0,                    0,0,0,0,0, 1,0); 
    626691        elf_newsection2(NAMIDX_SYMTAB,SHT_SYMTAB, 0,                    0,0,0,0,0, 8,0); 
    627692        elf_newsection2(NAMIDX_SHSTRTAB,SHT_STRTAB, 0,                  0,0,0,0,0, 1,0); 
    628693        elf_newsection2(NAMIDX_COMMENT, SHT_PROGDEF,0,                  0,0,0,0,0, 1,0); 
    629694        elf_newsection2(NAMIDX_NOTE,SHT_NOTE,   0,                      0,0,0,0,0, 1,0); 
     695 
     696        IDXSTR namidx; 
     697        namidx = NAMIDX_TEXT;      *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx; 
     698        namidx = NAMIDX_RELTEXT;   *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx; 
     699        namidx = NAMIDX_DATA;      *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx; 
     700        namidx = NAMIDX_RELDATA64; *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx; 
     701        namidx = NAMIDX_BSS;       *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx; 
     702        namidx = NAMIDX_RODATA;    *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx; 
     703        namidx = NAMIDX_STRTAB;    *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx; 
     704        namidx = NAMIDX_SYMTAB;    *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx; 
     705        namidx = NAMIDX_SHSTRTAB;  *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx; 
     706        namidx = NAMIDX_COMMENT;   *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx; 
     707        namidx = NAMIDX_NOTE;      *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx; 
    630708    } 
    631709    else 
    632710    { 
    633711        static char section_names_init[] = 
    634712          "\0.symtab\0.strtab\0.shstrtab\0.text\0.data\0.bss\0.note\0.comment\0.rodata\0.rel.text\0.rel.data"; 
    635713 
    636714        if (section_names) 
    637715            section_names->setsize(sizeof(section_names_init)); 
    638716        else 
    639717        {   section_names = new Outbuffer(512); 
    640             section_names->reserve(1024); 
     718            section_names->reserve(100*1024); 
    641719            section_names->writen(section_names_init, sizeof(section_names_init)); 
    642720        } 
     721 
     722        if (section_names_hashtable) 
     723            delete section_names_hashtable; 
     724        section_names_hashtable = new AArray(&ti_idxstr, sizeof(IDXSTR)); 
    643725 
    644726        // name,type,flags,addr,offset,size,link,info,addralign,entsize 
    645727        elf_newsection2(0,               SHT_NULL,   0,                 0,0,0,0,0, 0,0); 
    646728        elf_newsection2(NAMIDX_TEXT,SHT_PROGDEF,SHF_ALLOC|SHF_EXECINSTR,0,0,0,0,0, 16,0); 
    647729        elf_newsection2(NAMIDX_RELTEXT,SHT_REL, 0,0,0,0,SHI_SYMTAB,      SHI_TEXT, 4,8); 
    648730        elf_newsection2(NAMIDX_DATA,SHT_PROGDEF,SHF_ALLOC|SHF_WRITE,    0,0,0,0,0, 4,0); 
    649731        elf_newsection2(NAMIDX_RELDATA,SHT_REL, 0,0,0,0,SHI_SYMTAB,      SHI_DATA, 4,8); 
    650732        elf_newsection2(NAMIDX_BSS, SHT_NOBITS,SHF_ALLOC|SHF_WRITE,     0,0,0,0,0, 32,0); 
    651733        elf_newsection2(NAMIDX_RODATA,SHT_PROGDEF,SHF_ALLOC,            0,0,0,0,0, 1,0); 
    652734        elf_newsection2(NAMIDX_STRTAB,SHT_STRTAB, 0,                    0,0,0,0,0, 1,0); 
    653735        elf_newsection2(NAMIDX_SYMTAB,SHT_SYMTAB, 0,                    0,0,0,0,0, 4,0); 
    654736        elf_newsection2(NAMIDX_SHSTRTAB,SHT_STRTAB, 0,                  0,0,0,0,0, 1,0); 
    655737        elf_newsection2(NAMIDX_COMMENT, SHT_PROGDEF,0,                  0,0,0,0,0, 1,0); 
    656738        elf_newsection2(NAMIDX_NOTE,SHT_NOTE,   0,                      0,0,0,0,0, 1,0); 
     739 
     740        IDXSTR namidx; 
     741        namidx = NAMIDX_TEXT;      *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx; 
     742        namidx = NAMIDX_RELTEXT;   *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx; 
     743        namidx = NAMIDX_DATA;      *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx; 
     744        namidx = NAMIDX_RELDATA;   *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx; 
     745        namidx = NAMIDX_BSS;       *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx; 
     746        namidx = NAMIDX_RODATA;    *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx; 
     747        namidx = NAMIDX_STRTAB;    *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx; 
     748        namidx = NAMIDX_SYMTAB;    *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx; 
     749        namidx = NAMIDX_SHSTRTAB;  *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx; 
     750        namidx = NAMIDX_COMMENT;   *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx; 
     751        namidx = NAMIDX_NOTE;      *(IDXSTR *)section_names_hashtable->get(&namidx) = namidx; 
    657752    } 
    658753 
    659754    if (SYMbuf) 
    660755        SYMbuf->setsize(0); 
    661756    symbol_idx = 0; 
    662757    local_cnt = 0; 
    663758    // The symbols that every object file has 
    664759    elf_addsym(0, 0, 0, STT_NOTYPE,  STB_LOCAL, 0); 
    665760    elf_addsym(0, 0, 0, STT_FILE,    STB_LOCAL, SHT_ABS);       // STI_FILE 
    666761    elf_addsym(0, 0, 0, STT_SECTION, STB_LOCAL, SHI_TEXT);      // STI_TEXT 
    667762    elf_addsym(0, 0, 0, STT_SECTION, STB_LOCAL, SHI_DATA);      // STI_DATA 
    668763    elf_addsym(0, 0, 0, STT_SECTION, STB_LOCAL, SHI_BSS);       // STI_BSS 
    669764    elf_addsym(0, 0, 0, STT_NOTYPE,  STB_LOCAL, SHI_TEXT);      // STI_GCC 
    670765    elf_addsym(0, 0, 0, STT_SECTION, STB_LOCAL, SHI_RODAT);     // STI_RODAT 
    671766    elf_addsym(0, 0, 0, STT_SECTION, STB_LOCAL, SHI_NOTE);      // STI_NOTE 
    672767    elf_addsym(0, 0, 0, STT_SECTION, STB_LOCAL, SHI_COM);       // STI_COM 
    673768 
    674769    // Initialize output buffers for CODE, DATA and COMMENTS 
    675770    //      (NOTE not supported, BSS not required) 
    676771 
     
    15391634            pseg->SDbuf->reserve(1024); 
    15401635        } 
    15411636    } 
    15421637    if (pseg->SDrel) 
    15431638        pseg->SDrel->setsize(0); 
    15441639    pseg->SDsymidx = symidx; 
    15451640    pseg->SDrelidx = relidx; 
    15461641    pseg->SDrelmaxoff = 0; 
    15471642    pseg->SDrelindex = 0; 
    15481643    pseg->SDrelcnt = 0; 
    15491644    pseg->SDshtidxout = 0; 
    15501645    pseg->SDsym = NULL; 
    15511646    return seg; 
    15521647} 
    15531648 
    15541649int elf_getsegment(const char *name, const char *suffix, int type, int flags, 
    15551650        int align) 
    15561651{ 
    15571652    //printf("elf_getsegment(%s,%s,flags %x, align %d)\n",name,suffix,flags,align); 
    15581653 
    1559     IDXSTR namidx; 
    1560     if (namidx = elf_findstr(section_names,name,suffix)) 
    1561     {                                   // this section name exists 
     1654    // Add name~suffix to the section_names table 
     1655    IDXSTR namidx = section_names->size(); 
     1656    section_names->writeString(name); 
     1657    if (suffix) 
     1658    {   // Append suffix string 
     1659        section_names->setsize(section_names->size() - 1);  // back up over terminating 0 
     1660        section_names->writeString(suffix); 
     1661    } 
     1662    IDXSTR *pidx = (IDXSTR *)section_names_hashtable->get(&namidx); 
     1663    if (*pidx) 
     1664    {   // this section name already exists 
     1665        section_names->setsize(namidx);                 // remove addition 
     1666        namidx = *pidx; 
    15621667        for (int seg = CODE; seg <= seg_count; seg++) 
    15631668        {                               // should be in segment table 
    15641669            if (MAP_SEG2SEC(seg)->sh_name == namidx) 
    15651670            { 
    15661671                return seg;             // found section for segment 
    15671672            } 
    15681673        } 
    15691674        assert(0);      // but it's not a segment 
    15701675        // FIX - should be an error message conflict with section names 
    15711676    } 
     1677    *pidx = namidx; 
    15721678 
    15731679    //dbg_printf("\tNew segment - %d size %d\n", seg,SegData[seg]->SDbuf); 
    1574     IDXSEC shtidx = elf_newsection(name,suffix,type,flags); 
     1680    IDXSEC shtidx = elf_newsection2(namidx,type,flags,0,0,0,0,0,0,0); 
    15751681    SecHdrTab[shtidx].sh_addralign = align; 
    15761682    IDXSYM symidx = elf_addsym(0, 0, 0, STT_SECTION, STB_LOCAL, shtidx); 
    15771683    int seg = elf_getsegment2(shtidx, symidx, 0); 
    15781684    //printf("-elf_getsegment() = %d\n", seg); 
    15791685    return seg; 
    15801686} 
    15811687 
    15821688/******************************** 
    15831689 * Define a new code segment. 
    15841690 * Input: 
    15851691 *      name            name of segment, if NULL then revert to default 
    15861692 *      suffix  0       use name as is 
    15871693 *              1       append "_TEXT" to name 
    15881694 * Output: 
    15891695 *      cseg            segment index of new current code segment 
    15901696 *      Coffset         starting offset in cseg 
    15911697 * Returns: 
    15921698 *      segment index of newly created code segment 
    15931699 */ 
    15941700