Changeset 628
- Timestamp:
- 08/26/10 02:12:38 (1 year ago)
- Files:
-
- branches/dmd-1.x/src/backend/elfobj.c (modified) (10 diffs)
- trunk/src/backend/elfobj.c (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/dmd-1.x/src/backend/elfobj.c
r589 r628 38 38 39 39 #include "dwarf.h" 40 41 #include "aa.h" 42 #include "tinfo.h" 40 43 41 44 //#define DEBSYM 0x7E … … 124 127 #define SEC_NAMES_INC 400 125 128 129 // Hash table for section_names 130 AArray *section_names_hashtable; 131 132 /* ====================== Cached Strings in section_names ================= */ 133 134 struct 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 144 TypeInfo_Idxstr ti_idxstr; 145 146 const char* TypeInfo_Idxstr::toString() 147 { 148 return "IDXSTR"; 149 } 150 151 hash_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 164 int 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 174 int 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 184 size_t TypeInfo_Idxstr::tsize() 185 { 186 return sizeof(IDXSTR); 187 } 188 189 void TypeInfo_Idxstr::swap(void *p1, void *p2) 190 { 191 assert(0); 192 } 193 194 195 /* ======================================================================== */ 196 126 197 // String Table - String table for all other names 127 198 static Outbuffer *symtab_strings; … … 254 325 static IDXSTR elf_findstr(Outbuffer *strtab, const char *str, const char *suffix) 255 326 { 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 256 358 const char *ent = (char *)strtab->buf+1; 257 359 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; 302 366 } 303 367 return 0; // never found match … … 468 532 elf_u32_f32 type, elf_u32_f32 flags) 469 533 { 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(); 479 538 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 485 542 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 487 548 return elf_newsection2(namidx,type,flags,0,0,0,0,0,0,0); 488 549 } … … 615 676 } 616 677 678 if (section_names_hashtable) 679 delete section_names_hashtable; 680 section_names_hashtable = new AArray(&ti_idxstr, sizeof(IDXSTR)); 681 617 682 // name,type,flags,addr,offset,size,link,info,addralign,entsize 618 683 elf_newsection2(0, SHT_NULL, 0, 0,0,0,0,0, 0,0); … … 628 693 elf_newsection2(NAMIDX_COMMENT, SHT_PROGDEF,0, 0,0,0,0,0, 1,0); 629 694 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; 630 708 } 631 709 else … … 638 716 else 639 717 { section_names = new Outbuffer(512); 640 section_names->reserve(10 24);718 section_names->reserve(100*1024); 641 719 section_names->writen(section_names_init, sizeof(section_names_init)); 642 720 } 721 722 if (section_names_hashtable) 723 delete section_names_hashtable; 724 section_names_hashtable = new AArray(&ti_idxstr, sizeof(IDXSTR)); 643 725 644 726 // name,type,flags,addr,offset,size,link,info,addralign,entsize … … 655 737 elf_newsection2(NAMIDX_COMMENT, SHT_PROGDEF,0, 0,0,0,0,0, 1,0); 656 738 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; 657 752 } 658 753 … … 1557 1652 //printf("elf_getsegment(%s,%s,flags %x, align %d)\n",name,suffix,flags,align); 1558 1653 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; 1562 1667 for (int seg = CODE; seg <= seg_count; seg++) 1563 1668 { // should be in segment table … … 1570 1675 // FIX - should be an error message conflict with section names 1571 1676 } 1677 *pidx = namidx; 1572 1678 1573 1679 //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); 1575 1681 SecHdrTab[shtidx].sh_addralign = align; 1576 1682 IDXSYM symidx = elf_addsym(0, 0, 0, STT_SECTION, STB_LOCAL, shtidx); trunk/src/backend/elfobj.c
r589 r628 38 38 39 39 #include "dwarf.h" 40 41 #include "aa.h" 42 #include "tinfo.h" 40 43 41 44 //#define DEBSYM 0x7E … … 124 127 #define SEC_NAMES_INC 400 125 128 129 // Hash table for section_names 130 AArray *section_names_hashtable; 131 132 /* ====================== Cached Strings in section_names ================= */ 133 134 struct 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 144 TypeInfo_Idxstr ti_idxstr; 145 146 const char* TypeInfo_Idxstr::toString() 147 { 148 return "IDXSTR"; 149 } 150 151 hash_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 164 int 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 174 int 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 184 size_t TypeInfo_Idxstr::tsize() 185 { 186 return sizeof(IDXSTR); 187 } 188 189 void TypeInfo_Idxstr::swap(void *p1, void *p2) 190 { 191 assert(0); 192 } 193 194 195 /* ======================================================================== */ 196 126 197 // String Table - String table for all other names 127 198 static Outbuffer *symtab_strings; … … 254 325 static IDXSTR elf_findstr(Outbuffer *strtab, const char *str, const char *suffix) 255 326 { 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 256 358 const char *ent = (char *)strtab->buf+1; 257 359 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; 302 366 } 303 367 return 0; // never found match … … 468 532 elf_u32_f32 type, elf_u32_f32 flags) 469 533 { 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(); 479 538 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 485 542 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 487 548 return elf_newsection2(namidx,type,flags,0,0,0,0,0,0,0); 488 549 } … … 615 676 } 616 677 678 if (section_names_hashtable) 679 delete section_names_hashtable; 680 section_names_hashtable = new AArray(&ti_idxstr, sizeof(IDXSTR)); 681 617 682 // name,type,flags,addr,offset,size,link,info,addralign,entsize 618 683 elf_newsection2(0, SHT_NULL, 0, 0,0,0,0,0, 0,0); … … 628 693 elf_newsection2(NAMIDX_COMMENT, SHT_PROGDEF,0, 0,0,0,0,0, 1,0); 629 694 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; 630 708 } 631 709 else … … 638 716 else 639 717 { section_names = new Outbuffer(512); 640 section_names->reserve(10 24);718 section_names->reserve(100*1024); 641 719 section_names->writen(section_names_init, sizeof(section_names_init)); 642 720 } 721 722 if (section_names_hashtable) 723 delete section_names_hashtable; 724 section_names_hashtable = new AArray(&ti_idxstr, sizeof(IDXSTR)); 643 725 644 726 // name,type,flags,addr,offset,size,link,info,addralign,entsize … … 655 737 elf_newsection2(NAMIDX_COMMENT, SHT_PROGDEF,0, 0,0,0,0,0, 1,0); 656 738 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; 657 752 } 658 753 … … 1557 1652 //printf("elf_getsegment(%s,%s,flags %x, align %d)\n",name,suffix,flags,align); 1558 1653 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; 1562 1667 for (int seg = CODE; seg <= seg_count; seg++) 1563 1668 { // should be in segment table … … 1570 1675 // FIX - should be an error message conflict with section names 1571 1676 } 1677 *pidx = namidx; 1572 1678 1573 1679 //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); 1575 1681 SecHdrTab[shtidx].sh_addralign = align; 1576 1682 IDXSYM symidx = elf_addsym(0, 0, 0, STT_SECTION, STB_LOCAL, shtidx);
