1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
|
/**************************** elf2mac.cpp *********************************
* Author: Agner Fog
* Date created: 2007-01-10
* Last modified: 2012-05-05
* Project: objconv
* Module: elf2mac.cpp
* Description:
* Module for converting ELF file to Mach-O file
*
* Copyright 2007-2012 GNU General Public License http://www.gnu.org/licenses
*****************************************************************************/
#include "stdafx.h"
template <class TELF_Header, class TELF_SectionHeader, class TELF_Symbol, class TELF_Relocation,
class TMAC_header, class TMAC_segment_command, class TMAC_section, class TMAC_nlist, class MInt>
CELF2MAC<ELFSTRUCTURES,MACSTRUCTURES>::CELF2MAC() {
// Constructor
memset(this, 0, sizeof(*this)); // Reset everything
}
template <class TELF_Header, class TELF_SectionHeader, class TELF_Symbol, class TELF_Relocation,
class TMAC_header, class TMAC_segment_command, class TMAC_section, class TMAC_nlist, class MInt>
void CELF2MAC<ELFSTRUCTURES,MACSTRUCTURES>::Convert() {
// Do the conversion
// Some compilers require this-> for accessing members of template base class,
// according to the so-called two-phase lookup rule.
// Call the subfunctions
ToFile.SetFileType(FILETYPE_MACHO_LE); // Set type of new file
MakeFileHeader(); // Make file header
MakeSectionsIndex(); // Make sections index translation table
FindUnusedSymbols(); // Check if symbols used, remove unused symbols
MakeSymbolTable(); // Make symbol table and string tables
MakeSections(); // Make sections and relocation tables
MakeBinaryFile(); // Put sections together
*this << ToFile; // Take over new file buffer
}
template <class TELF_Header, class TELF_SectionHeader, class TELF_Symbol, class TELF_Relocation,
class TMAC_header, class TMAC_segment_command, class TMAC_section, class TMAC_nlist, class MInt>
void CELF2MAC<ELFSTRUCTURES,MACSTRUCTURES>::MakeFileHeader() {
// Convert subfunction: Make file header and load segment command
TMAC_header NewHeader; // new file header
NewHeader.magic = (this->WordSize == 32) ? MAC_MAGIC_32 : MAC_MAGIC_64; // Mach magic number identifier
NewHeader.cputype = (this->WordSize == 32) ? MAC_CPU_TYPE_I386 : MAC_CPU_TYPE_X86_64;
NewHeader.cpusubtype = MAC_CPU_SUBTYPE_I386_ALL;
NewHeader.filetype = MAC_OBJECT;
NewHeader.ncmds = 3; // Three commands = segment, symbol table, dynsymtab
NewHeader.sizeofcmds = 0; // Set this later
NewHeader.flags = 0; // No flags needed
// put file header in OutFile
ToFile.Push(&NewHeader, sizeof(NewHeader));
}
template <class TELF_Header, class TELF_SectionHeader, class TELF_Symbol, class TELF_Relocation,
class TMAC_header, class TMAC_segment_command, class TMAC_section, class TMAC_nlist, class MInt>
void CELF2MAC<ELFSTRUCTURES,MACSTRUCTURES>::MakeSectionsIndex() {
// Make sections index translation table and section offset table.
// We must make these tables before the sections, because they are needed for the
// symbol tables and relocation tables, and we must make the symbol tables before
// the relocation tables, and we must make the relocation tables together with the
// sections.
uint32_t oldsec; // Section number in old file
uint32_t newsec = 0; // Section number in new file
NewSectIndex. SetNum(this->NSections); // Allocate size for section index table
NewSectIndex. SetZero(); // Initialize
NewSectOffset.SetNum(this->NSections); // Allocate buffer for section offset table
NewSectOffset.SetZero(); // Initialize
MInt NewVirtualAddress = 0; // Virtual address of new section as specified in Mach-O file
// First loop through old sections
for (oldsec = 0; oldsec < this->NSections; oldsec++) {
NewSectIndex[oldsec] = 0;
NewSectOffset[oldsec] = 0;
// Get section name
const char * sname = "";
uint32_t namei = this->SectionHeaders[oldsec].sh_name;
if (namei >= this->SecStringTableLen) {
err.submit(2112);
}
else sname = this->SecStringTable + namei;
if (cmd.DebugInfo == CMDL_DEBUG_STRIP) {
// Check for debug section names
if (strncmp(sname, ".note", 5) == 0
|| strncmp(sname, ".comment", 8) == 0
|| strncmp(sname, ".stab", 5) == 0
|| strncmp(sname, ".debug", 6) == 0) {
// Remove this section
this->SectionHeaders[oldsec].sh_type = SHT_REMOVE_ME;
cmd.CountDebugRemoved();
continue;
}
}
if (cmd.ExeptionInfo == CMDL_EXCEPTION_STRIP) {
// Check for exception section name
if (strncmp(sname, ".eh_frame", 9) == 0) {
// Remove this section
this->SectionHeaders[oldsec].sh_type = SHT_REMOVE_ME;
cmd.CountExceptionRemoved();
continue;
}
}
// Search for program data sections only
if (this->SectionHeaders[oldsec].sh_type != SHT_PROGBITS
&& this->SectionHeaders[oldsec].sh_type != SHT_NOBITS) {
// Has no data. Ignore
continue;
}
if (this->SectionHeaders[oldsec].sh_size == 0) {
// Remove empty section
// The linker has a bug with empty sections
continue;
}
// Section index translation table
NewSectIndex[oldsec] = newsec++;
// Calculate virtual memory address of section. This address does not have
// much to do with the final address, but it is needed in relocation entries.
// Alignment
int NewAlign = FloorLog2((uint32_t)this->SectionHeaders[oldsec].sh_addralign);
if (NewAlign > 12) NewAlign = 12; // What is the limit for highest alignment?
int AlignBy = 1 << NewAlign;
// Align memory address
NewVirtualAddress = (NewVirtualAddress + AlignBy - 1) & -(MInt)AlignBy;
// Virtual memory address of new section
NewSectOffset[oldsec] = NewVirtualAddress;
// Increment memory address
NewVirtualAddress += this->SectionHeaders[oldsec].sh_size;
// Fix v. 2.14: Align end of memory address by 4
NewVirtualAddress = (NewVirtualAddress + 3) & MInt(-4);
}
// Store number of sections in new file
NumSectionsNew = newsec;
// Calculate file offset of first raw data
RawDataOffset = sizeof(TMAC_header)
+ sizeof(TMAC_segment_command)
+ NumSectionsNew * sizeof(TMAC_section)
+ sizeof(MAC_symtab_command)
+ sizeof(MAC_dysymtab_command);
// Align end of memory address by 4
NewVirtualAddress = (NewVirtualAddress + 3) & MInt(-4);
// Make segment command
TMAC_segment_command NewSegment;
memset(&NewSegment, 0, sizeof(NewSegment));
NewSegment.cmd = (this->WordSize == 32) ? MAC_LC_SEGMENT : MAC_LC_SEGMENT_64;
NewSegment.cmdsize = sizeof(TMAC_segment_command) + NumSectionsNew * sizeof(TMAC_section);
NewSegment.fileoff = RawDataOffset;
NewSegment.nsects = NumSectionsNew;
NewSegment.maxprot = NewSegment.initprot = 7; // 1=read, 2=write, 4=execute
NewSegment.vmsize = NewVirtualAddress;
NewSegment.filesize = 0; // Changed later
// put segment command in OutFile
CommandOffset = ToFile.Push(&NewSegment, sizeof(NewSegment));
}
template <class TELF_Header, class TELF_SectionHeader, class TELF_Symbol, class TELF_Relocation,
class TMAC_header, class TMAC_segment_command, class TMAC_section, class TMAC_nlist, class MInt>
void CELF2MAC<ELFSTRUCTURES,MACSTRUCTURES>::MakeSymbolTable() {
// Convert subfunction: Symbol table and string tables
uint32_t oldsec; // Section number in old file
TELF_SectionHeader OldHeader; // Old section header
int FoundSymTab = 0; // Found symbol table
char * strtab; // Old symbol string table
int8_t * symtab; // Old symbol table
uint32_t symtabsize; // Size of old symbol table
int8_t * symtabend; // End of old symbol table
uint32_t entrysize; // Size of each entry in old symbol table
TELF_Symbol OldSym; // Old symbol table record
uint32_t OldSymI; // Symbol index in old symbol table
const char * symname; // Symbol name
int NewSection = 0; // New section index
int NewType; // New symbol type
int NewDesc; // New symbol reference type
MInt Value; // Symbol value
uint32_t Scope; // 0: Local, 1: Public, 2: External
// Loop through old sections to find symbol table
for (oldsec = 0; oldsec < this->NSections; oldsec++) {
// Search for program data sections only
if (this->SectionHeaders[oldsec].sh_type == SHT_SYMTAB
|| this->SectionHeaders[oldsec].sh_type == SHT_DYNSYM) {
FoundSymTab++;
// Copy symbol table header for convenience
OldHeader = this->SectionHeaders[oldsec];
// Find associated string table
if (OldHeader.sh_link >= (uint32_t)(this->NSections)) {
err.submit(2035); OldHeader.sh_link = 0;
}
strtab = (char*)this->Buf() + (uint32_t)this->SectionHeaders[OldHeader.sh_link].sh_offset;
// Find old symbol table
entrysize = (uint32_t)OldHeader.sh_entsize;
if (entrysize < sizeof(TELF_Symbol)) {err.submit(2033); entrysize = sizeof(TELF_Symbol);}
symtab = this->Buf() + (uint32_t)OldHeader.sh_offset;
symtabsize = (uint32_t)OldHeader.sh_size;
symtabend = symtab + symtabsize;
if (NewSymTab[0].GetNumEntries() == 0) {
// make empty symbol record for index 0
NewSymTab[0].AddSymbol(0, "", 0, 0, 0, 0);
}
// Loop through old symbol table
for (OldSymI = 0; symtab < symtabend; symtab += entrysize, OldSymI++) {
if (OldSymI == 0) continue; // First symbol entry in ELF file is unused
// Copy 32 bit symbol table entry or convert 64 bit entry
OldSym = *(TELF_Symbol*)symtab;
// Old symbol type
int type = OldSym.st_type;
// Old symbol storage class = binding
int binding = OldSym.st_bind;
// Get symbol name
if (OldSym.st_name < this->SymbolStringTableSize) {
symname = strtab + OldSym.st_name;
}
else {
err.submit(2112); // String table corrupt
continue; // Ignore
}
if (symname == 0 || *symname == 0) {
// Symbol has no name. Give it a name
// Mac linker messes this up if the symbol doesn't have a unique name.
char tempbuf[80];
sprintf(tempbuf, "?unnamed%i", OldSymI);
int os = UnnamedSymbolsTable.PushString(tempbuf);
symname = (char*)UnnamedSymbolsTable.Buf() + os;
}
NewType = NewDesc = 0; // New symbol type
// Value = address
Value = OldSym.st_value;
// Section
if (OldSym.st_shndx == SHN_UNDEF) {
NewSection = 0; // External
}
else if ((int16_t)(OldSym.st_shndx) == SHN_ABS) {
NewType |= MAC_N_ABS; // Absolute symbol
NewDesc |= MAC_N_NO_DEAD_STRIP;
NewSection = 0;
}
else if ((int16_t)(OldSym.st_shndx) == SHN_COMMON) {
NewType |= MAC_N_ABS; // Common symbol. Translate to abs and make warning
NewDesc |= MAC_N_NO_DEAD_STRIP;
NewSection = 0;
err.submit(1053, symname); // Warning. Common symbol
}
else if (OldSym.st_shndx >= this->NSections) {
err.submit(2036, OldSym.st_shndx); // Special/unknown section index or out of range
}
else {
// Normal section index.
// Look up in section index translation table and add 1 because it is 1-based
NewSection = NewSectIndex[OldSym.st_shndx] + 1;
// Value must be absolute address. Add section address
Value += NewSectOffset[OldSym.st_shndx];
}
// Convert binding/storage class
switch (binding) {
case STB_LOCAL: // Local
Scope = S_LOCAL;
if (!(NewType & MAC_N_ABS)) NewType |= MAC_N_SECT;
break;
case STB_GLOBAL:
if (NewSection || (NewType & MAC_N_ABS)) {
// Public
Scope = S_PUBLIC;
NewType |= MAC_N_EXT;
if (!(NewType & MAC_N_ABS)) NewType |= MAC_N_SECT;
}
else {
// External
Scope = S_EXTERNAL;
NewType |= MAC_N_EXT;
}
NewDesc |= MAC_REF_FLAG_UNDEFINED_NON_LAZY;
break;
case STB_WEAK:
if (NewSection) {
// Weak public
Scope = S_PUBLIC;
NewType |= MAC_N_EXT | MAC_N_SECT;
NewDesc |= MAC_N_WEAK_DEF;
if (this->WordSize == 32) {
err.submit(1051, symname); // Weak public only allowed in coalesced section of MachO-32
}
}
else {
// Weak external
Scope = S_EXTERNAL;
NewType |= MAC_N_EXT;
NewDesc |= MAC_N_WEAK_REF;
}
break;
default:
Scope = S_LOCAL;
err.submit(2037, binding); // Other. Not supported
}
// Make record depending on type
switch (type) {
case STT_OBJECT: case STT_NOTYPE:
// Data object
break;
case STT_GNU_IFUNC:
err.submit(1063); // Warning: Gnu indirect function cannot be converted
// continue in next case:
case STT_FUNC:
// Function
break;
case STT_FILE:
// File name record. Ignore
continue;
case STT_SECTION:
// Section name record. (Has no name)
break;
case STT_COMMON:
default:
err.submit(2038, type); // Symbol type not supported
continue;
}
// Discard unused symbols
if (Scope != S_PUBLIC && !OldSymbolUsed[OldSymI]) continue;
// Store new symbol record in the appropriate table
if (Scope > 2) err.submit(9000);
NewSymTab[Scope].AddSymbol(OldSymI, symname, NewType, NewDesc, NewSection, Value);
// Store scope in OldSymbolScope
if (OldSymI < NumOldSymbols) {
OldSymbolScope[OldSymI] = Scope;
}
} // End OldSymI loop
}
} // End search for symbol table
if (FoundSymTab == 0) err.submit(2034); // Symbol table not found
if (FoundSymTab > 1) err.submit(1032); // More than one symbol table found
}
template <class TELF_Header, class TELF_SectionHeader, class TELF_Symbol, class TELF_Relocation,
class TMAC_header, class TMAC_segment_command, class TMAC_section, class TMAC_nlist, class MInt>
void CELF2MAC<ELFSTRUCTURES,MACSTRUCTURES>::Elf2MacRelocations(Elf32_Shdr & OldRelHeader, MAC_section_32 & NewHeader, uint32_t NewRawDataOffset, uint32_t oldsec) {
// Convert 32-bit relocations from ELF to MAC
// (This function has two template instances, only the 32-bit instance is used)
Elf32_Rela OldRelocation; // Old relocation table entry
MAC_scattered_relocation_info scat; // Scattered relocation entry
memset(&scat, 0, sizeof(scat));
// Get pointer to old relocation table
int8_t * reltab = this->Buf() + OldRelHeader.sh_offset;
int8_t * reltabend = reltab + OldRelHeader.sh_size;
// Get entry size
uint32_t entrysize = (uint32_t)OldRelHeader.sh_entsize;
uint32_t expectedentrysize = (OldRelHeader.sh_type == SHT_REL) ? sizeof(Elf32_Rel) : sizeof(Elf32_Rela);
if (entrysize < expectedentrysize) {err.submit(2033); entrysize = expectedentrysize;}
// File pointer to relocations
NewHeader.reloff = NewRelocationTab.GetNumEntries()*sizeof(MAC_relocation_info); // Offset to first relocation table added later
// Loop through relocation table entries
for (; reltab < reltabend; reltab += entrysize) {
// Copy relocation entry with or without addend
OldRelocation.r_addend = 0;
memcpy(&OldRelocation, reltab, entrysize);
// Find inline addend
uint32_t InlinePosition = (uint32_t)(NewRawDataOffset + OldRelocation.r_offset);
// Check that address is valid
if (InlinePosition >= this->GetDataSize()) {
// Address is invalid
err.submit(2032); break;
}
// Pointer to inline addend
int32_t * piaddend = (int32_t*)(NewRawData.Buf() + InlinePosition);
// Add old addend if any
*piaddend += (int32_t)OldRelocation.r_addend;
// Define relocation parameters
uint32_t r_address = 0; // section-relative offset to relocation source
uint32_t r_symbolnum = 0; // symbol index if r_extern == 1 or section ordinal if r_extern == 0
// uint32_t r_value = 0; // value of relocation target
// int r_scattered = 0; // use scattered relocation
int r_pcrel = 0; // self relative
int r_length = 2; // size of source: 0=byte, 1=2 bytes, 2=4 bytes, 3=8 bytes
int r_extern = 0; // public or external
int r_type = 0; // if not 0, machine specific relocation type
int Scope = 0; // Symbol scope: 0 = local, 1 = public, 2 = external
// source offset
r_address = (uint32_t)OldRelocation.r_offset;
// target scope
if (OldRelocation.r_sym < NumOldSymbols) {
Scope = OldSymbolScope[OldRelocation.r_sym];
}
// Get r_extern: 0 = local target referenced by address,
// 1 = external symbol referenced by symbol table index
switch (Scope) {
case S_LOCAL: // Local target must be referenced by address
r_extern = 0; break;
case S_PUBLIC: // Public target is optionally referenced by index or by address
r_extern = 0;
// r_extern = 1; is not allowed!
break;
case S_EXTERNAL: // External target is always referenced by index
r_extern = 1; break;
}
// Get zero-based index into NewSymTab[Scope]
int newindex = NewSymTab[Scope].TranslateIndex(OldRelocation.r_sym);
if (newindex < 0) {
// Symbol not found or wrong type
err.submit(2031);
break;
}
if (r_extern) {
// r_symbolnum is zero based index into combined symbol tables.
// Add number of entries in preceding NewSymTab tables to index
// into NewSymTab[Scope]
r_symbolnum = newindex + NumSymbols[Scope];
}
else {
// r_extern = 0. r_symbolnum = target section
r_symbolnum = NewSymTab[Scope][newindex].n_sect;
// Absolute address of target stored inline in source
*piaddend += (uint32_t)NewSymTab[Scope][newindex].n_value;
}
// Get relocation type and fix addend
switch(OldRelocation.r_type) {
case R_386_NONE: // Ignored
continue;
case R_386_IRELATIVE:
err.submit(1063); // Warning: Gnu indirect function cannot be converted
// continue in next case?:
case R_386_32: // 32-bit absolute virtual address
r_type = MAC32_RELOC_VANILLA;
break;
case R_386_PC32: // 32-bit self-relative
r_type = MAC32_RELOC_VANILLA;
r_pcrel = 1;
// Mach-O 32 bit format requires that self-relative addresses must have
// self-relative values already before relocation. Therefore
// the source address is subtracted.
// (The PC reference point is the end of the source = start
// of source + 4, but ELF files have the same offset so no further
// correction is needed when converting from ELF file).
// !! ToDo: Self-relative relocations plus offset to local symbol in a different section
// sometimes causes problems in Mac linker, perhaps because it fails to determine
// correctly which section the target is in. Use a relocation with a reference point
// instead. This probably occurs only in assembler-coded self-relative 32-bit code.
// (Use asmlib A_strtoupper and A_strcspn as test cases - they fail if dummy data
// at the end of .data section is removed)
*piaddend -= r_address + (uint32_t)NewHeader.addr;
break;
case R_UNSUPPORTED_IMAGEREL: // 32-bit image-relative
// This occurs only when converting from COFF (via ELF)
// Needs scattered relocation entry
scat.r_address = r_address;
scat.r_length = 2;
scat.r_pcrel = 0;
scat.r_scattered = 1;
scat.r_type = MAC32_RELOC_SECTDIFF;
scat.r_value = r_symbolnum;
// Store first entry of scattered pair
NewRelocationTab.Push(&scat, sizeof(scat));
NewHeader.nreloc++;
// Make subtractor record for image base
scat.r_type = MAC32_RELOC_PAIR;
scat.r_value = GetImagebaseSymbol();
// Store second entry of scattered pair
NewRelocationTab.Push(&scat, sizeof(scat));
NewHeader.nreloc++;
continue;
case R_386_GOT32: case R_386_GLOB_DAT: case R_386_GOTOFF: case R_386_GOTPC:
// Global offset table
err.submit(2042); // cannot convert position-independent code
err.ClearError(2042); // report this error only once
r_type = 0;
break;
case R_386_PLT32: case R_386_JMP_SLOT:
// procedure linkage table
err.submit(2043); // cannot convert import table
err.ClearError(2043); // report this error only once
r_type = 0;
break;
default: // Unknown or unsupported relocation method
err.submit(2030, OldRelocation.r_type);
r_type = 0; break;
}
if (!r_pcrel) {
// Warn for position dependent code.
// This warning is currently turned off in error.cpp.
err.submit(1050, this->SymbolName(OldRelocation.r_sym));
// Write this error only once
err.ClearError(1050);
}
// Make relocation entry
MAC_relocation_info rel;
memset(&rel, 0, sizeof(rel));
// Make non-scattered relocation entry
rel.r_address = r_address;
rel.r_symbolnum = r_symbolnum;
rel.r_pcrel = r_pcrel;
rel.r_length = r_length;
rel.r_extern = r_extern;
rel.r_type = r_type;
// Store relocation entry
NewRelocationTab.Push(&rel, sizeof(rel));
NewHeader.nreloc++;
// Remember that symbol is used
// if (SymbolsUsed && OldRelocation.r_type && NewRelocation.r_symbolnum < ?) {
// SymbolsUsed[NewRelocation.r_symbolnum]++;}
} // End of relocations loop
}
template <class TELF_Header, class TELF_SectionHeader, class TELF_Symbol, class TELF_Relocation,
class TMAC_header, class TMAC_segment_command, class TMAC_section, class TMAC_nlist, class MInt>
void CELF2MAC<ELFSTRUCTURES,MACSTRUCTURES>::Elf2MacRelocations(Elf64_Shdr & OldRelHeader, MAC_section_64 & NewHeader, uint32_t NewRawDataOffset, uint32_t oldsec) {
// Convert 64-bit relocations from ELF to MAC
// (This function has two template instances, only the 64-bit instance is used)
// Make relocation entry for dummy subtractor
MAC_relocation_info relsub;
memset(&relsub, 0, sizeof(relsub));
Elf64_Rela OldRelocation; // Old relocation table entry
// Get pointer to old relocation table
int8_t * reltab = this->Buf() + OldRelHeader.sh_offset;
int8_t * reltabend = reltab + OldRelHeader.sh_size;
// Get entry size
uint32_t entrysize = (uint32_t)OldRelHeader.sh_entsize;
uint32_t expectedentrysize = (OldRelHeader.sh_type == SHT_REL) ? sizeof(Elf64_Rel) : sizeof(Elf64_Rela);
if (entrysize < expectedentrysize) {err.submit(2033); entrysize = expectedentrysize;}
// File pointer to relocations
NewHeader.reloff = NewRelocationTab.GetNumEntries()*sizeof(MAC_relocation_info); // Offset to first relocation table added later
// Loop through relocation table entries
for (; reltab < reltabend; reltab += entrysize) {
// Copy relocation entry with or without addend
OldRelocation.r_addend = 0;
memcpy(&OldRelocation, reltab, entrysize);
// Find inline addend
uint32_t InlinePosition = (uint32_t)(NewRawDataOffset + OldRelocation.r_offset);
// Check that address is valid
if (InlinePosition >= this->GetDataSize()) {
// Address is invalid
err.submit(2032);
break;
}
// Pointer to inline addend
int32_t * piaddend = (int32_t*)(NewRawData.Buf() + InlinePosition);
// Add old addend if any
*piaddend += (uint32_t)OldRelocation.r_addend;
// Define relocation parameters
uint32_t r_address = 0; // section-relative offset to relocation source
uint32_t r_symbolnum = 0; // symbol index if r_extern == 1 or section ordinal if r_extern == 0
// uint32_t r_value = 0; // value of relocation target
// int r_scattered = 0; // scattered relocations not used in 64 bit
int r_pcrel = 0; // self relative
int r_length = 2; // size of source: 0=byte, 1=2 bytes, 2=4 bytes, 3=8 bytes
int r_extern = 0; // public or external
int r_type = 0; // if not 0, machine specific relocation type
int Scope = 0; // Symbol scope: 0 = local, 1 = public, 2 = external
// source offset
r_address = (uint32_t)OldRelocation.r_offset;
// target scope
if (OldRelocation.r_sym < NumOldSymbols) {
Scope = OldSymbolScope[OldRelocation.r_sym];
}
// Get r_extern: 0 = local target referenced by address,
// 1 = public or external symbol referenced by symbol table index
switch (Scope) {
case S_LOCAL: // Local target
// r_extern = 0; // Local target must be referenced by address
// Note: the description in reloc.h says that local targets are addressed
// relative to any preceding public target. If there is no preceding label
// then referenced by address in the segment. However, the Gnu compiler
// uses reference to a local symbol and sets r_extern = 1 to indicate that
// it refers to a symbol record, not to an address. I have chosen to use the
// latter method because it is simpler, though undocumented.
r_extern = 1;
break;
case S_PUBLIC: // Public target is optionally referenced by index or by address
r_extern = 1;
break;
case S_EXTERNAL: // External target is always referenced by index
r_extern = 1;
break;
}
// Get zero-based index into NewSymTab[Scope]
int newindex = NewSymTab[Scope].TranslateIndex(OldRelocation.r_sym);
if (newindex < 0) {
// Symbol not found or wrong type
err.submit(2031);
break;
}
// r_symbolnum is zero based index into combined symbol tables.
// Add number of entries in preceding NewSymTab tables to index
// into NewSymTab[Scope]
r_symbolnum = newindex + NumSymbols[Scope];
// Get relocation type and fix addend, 64 bit
switch(OldRelocation.r_type) {
case R_X86_64_NONE: // Ignored
continue;
case R_X86_64_64:
// 64-bit absolute virtual address
r_type = MAC64_RELOC_UNSIGNED; r_length = 3;
break;
case R_X86_64_IRELATIVE:
err.submit(1063); // Warning: Gnu indirect function cannot be converted
// continue in next case?:
case R_X86_64_32: case R_X86_64_32S: {
// 32-bit absolute virtual address
// Note: The linker doesn't accept a 32-bit absolute address
// Make address relative to the image base, and add the value of the image base to compensate
if (cmd.ImageBase == 0) {
// Default image base if not specified
cmd.ImageBase = 0x400000;
}
// Make subtractor relocation entry for image base
relsub.r_address = r_address;
relsub.r_symbolnum = GetImagebaseSymbol();
relsub.r_length = 2;
relsub.r_extern = 1;
relsub.r_type = MAC64_RELOC_SUBTRACTOR;
NewRelocationTab.Push(&relsub, sizeof(relsub));
NewHeader.nreloc++;
// Add image base to compensate for subtracted image base
*piaddend += cmd.ImageBase;
// Now we can add the address we really want:
r_type = MAC64_RELOC_UNSIGNED;
r_length = 2;
// Warn that image base must be set to the specified value
char ImageBaseHex[32];
sprintf(ImageBaseHex, "%X", cmd.ImageBase); // write value as hexadecimal
err.submit(1300, ImageBaseHex); err.ClearError(1300);
break;}
case R_X86_64_PC32: // 32-bit self-relative
r_type = MAC64_RELOC_BRANCH;
// MAC64_RELOC_SIGNED does the same, but the linker complains if external symbol
r_length = 2;
r_pcrel = 1;
// Difference between EIP-relative and self-relative relocation = size of address field
// Adjust inline addend for different relocation method:
*piaddend += 4;
break;
case R_UNSUPPORTED_IMAGEREL: // 32-bit image-relative
// This occurs only when converting from COFF (via ELF)
// Make subtractor relocation entry for image base
relsub.r_address = r_address;
relsub.r_symbolnum = GetImagebaseSymbol();
relsub.r_length = 2;
relsub.r_extern = 1;
relsub.r_type = MAC64_RELOC_SUBTRACTOR;
NewRelocationTab.Push(&relsub, sizeof(relsub));
NewHeader.nreloc++;
// Second record adds the target address
r_type = MAC64_RELOC_UNSIGNED;
r_length = 2;
break;
case R_X86_64_GLOB_DAT: case R_X86_64_GOTPCREL:
// Create 64-bit GOT entry??
r_type = MAC64_RELOC_GOT; r_length = 2;
break;
case R_X86_64_GOT32:
// 32-bit GOT entry
err.submit(2042); // cannot convert 32-bit GOT
err.ClearError(2042); // report this error only once
r_type = 0;
break;
case R_X86_64_PLT32: case R_X86_64_JUMP_SLOT:
// procedure linkage table
err.submit(2043); // cannot convert import table
err.ClearError(2043); // report this error only once
r_type = 0;
break;
case R_X86_64_COPY: case R_X86_64_RELATIVE:
default: // Unknown or unsupported relocation method
err.submit(2030, OldRelocation.r_type);
r_type = 0; break;
}
// Make relocation entry
MAC_relocation_info rel;
memset(&rel, 0, sizeof(rel));
// Make non-scattered relocation entry
rel.r_address = r_address;
rel.r_symbolnum = r_symbolnum;
rel.r_pcrel = r_pcrel;
rel.r_length = r_length;
rel.r_extern = r_extern;
rel.r_type = r_type;
// Store relocation entry
NewRelocationTab.Push(&rel, sizeof(rel));
NewHeader.nreloc++;
// Remember that symbol is used
// if (SymbolsUsed && OldRelocation.r_type && NewRelocation.r_symbolnum < ?) {
// SymbolsUsed[NewRelocation.r_symbolnum]++;}
} // End of relocations loop
}
template <class TELF_Header, class TELF_SectionHeader, class TELF_Symbol, class TELF_Relocation,
class TMAC_header, class TMAC_segment_command, class TMAC_section, class TMAC_nlist, class MInt>
void CELF2MAC<ELFSTRUCTURES,MACSTRUCTURES>::MakeSections() {
// Convert subfunction: Make sections and relocation tables
uint32_t oldsec; // Section number in old file
uint32_t relsec; // Relocation section in old file
TMAC_section NewHeader; // New section header
TELF_SectionHeader OldHeader; // Old section header
TELF_SectionHeader OldRelHeader;// Old relocation section header
uint32_t NewVirtualAddress = 0; // Virtual address of new section
uint32_t NewRawDataOffset = 0; // Offset into NewRawData of section.
// NewRawDataOffset is different from NewVirtualAddress if alignment of sections in
// the object file is different from alignment of sections in memory
// Count cumulative number of symbols in each scope
NumSymbols[0] = 0;
NumSymbols[1] = NumSymbols[0] + NewSymTab[0].GetNumEntries();
NumSymbols[2] = NumSymbols[1] + NewSymTab[1].GetNumEntries();
NumSymbols[3] = NumSymbols[2] + NewSymTab[2].GetNumEntries();
if (NumSymbols[3] >= 0x1000000) err.submit(2051); // Too many symbols, max = 2^24
NewSectHeadOffset = ToFile.GetDataSize();
// Second loop through old sections
for (oldsec = 0; oldsec < this->NSections; oldsec++) {
// Copy old header for convenience
OldHeader = this->SectionHeaders[oldsec];
if (OldHeader.sh_size == 0) {
// Remove empty section
// The linker has a bug with empty sections
continue;
}
// Search for program data sections only
if (OldHeader.sh_type == SHT_PROGBITS || OldHeader.sh_type == SHT_NOBITS) {
// Reset new section header
memset(&NewHeader, 0, sizeof(NewHeader));
// Section name
const char * sname = "";
uint32_t namei = OldHeader.sh_name;
if (namei >= this->SecStringTableLen) err.submit(2112);
else sname = this->SecStringTable + namei;
// Translate section name and truncate to 16 characters
if (!stricmp(sname,".text") || !stricmp(sname,"_text")) {
strcpy(NewHeader.sectname, "__text");
strcpy(NewHeader.segname, "__TEXT");
}
else if (!stricmp(sname,".data") || !stricmp(sname,"_data")) {
strcpy(NewHeader.sectname, "__data");
strcpy(NewHeader.segname, "__DATA");
}
else if (!strnicmp(sname+1,"bss", 3)) {
strcpy(NewHeader.sectname, "__bss");
strcpy(NewHeader.segname, "__DATA");
}
else if (!strnicmp(sname+1,"const", 5) || !strnicmp(sname+1,"rodata", 6)) {
strcpy(NewHeader.sectname, "__const");
strcpy(NewHeader.segname, "__DATA");
}
else if (!strnicmp(sname, ELF_CONSTRUCTOR_NAME, 5)) {
// Constructors
strcpy(NewHeader.sectname, MAC_CONSTRUCTOR_NAME);
strcpy(NewHeader.segname, "__DATA");
NewHeader.flags = MAC_S_MOD_INIT_FUNC_POINTERS;
}
else if (OldHeader.sh_flags & SHF_EXECINSTR) {
// Other code section
if (strlen(NewHeader.sectname) > 16) err.submit(1040, NewHeader.sectname); // Warning: name truncated
strncpy(NewHeader.sectname, sname, 16);
strcpy(NewHeader.segname, "__TEXT");
}
else {
// Other data section. Truncate name to 16 characters
if (strlen(NewHeader.sectname) > 16) err.submit(1040, NewHeader.sectname); // Warning: name truncated
strncpy(NewHeader.sectname, sname, 16);
strcpy(NewHeader.segname, "__DATA");
}
if (NewHeader.sectname[0] == '.') {
// Make sure name begins with '_'
NewHeader.sectname[0] = '_';
}
// Raw data
NewHeader.size = OldHeader.sh_size; // section size in file
// File to raw data for section
NewHeader.offset = NewRawData.GetDataSize() + RawDataOffset;
if (OldHeader.sh_size && OldHeader.sh_type != SHT_NOBITS) { // Not for .bss segment
// Copy raw data
NewRawDataOffset = NewRawData.Push(this->Buf()+(uint32_t)OldHeader.sh_offset, (uint32_t)OldHeader.sh_size);
NewRawData.Align(4);
}
// Section flags
if (OldHeader.sh_flags & SHF_EXECINSTR) {
NewHeader.flags |= MAC_S_ATTR_PURE_INSTRUCTIONS | MAC_S_ATTR_SOME_INSTRUCTIONS;
}
else if (OldHeader.sh_type == SHT_NOBITS) {
NewHeader.flags |= MAC_S_ZEROFILL; // .bss segment
}
// Alignment
NewHeader.align = FloorLog2((uint32_t)OldHeader.sh_addralign);
if (NewHeader.align > 12) NewHeader.align = 12; // What is the limit for highest alignment?
int AlignBy = 1 << NewHeader.align;
// Align memory address
NewVirtualAddress = (NewVirtualAddress + AlignBy - 1) & -AlignBy;
// Virtual memory address of new section
NewHeader.addr = NewVirtualAddress;
NewVirtualAddress += (uint32_t)OldHeader.sh_size;
// Find relocation table for this section by searching through all sections
for (relsec = 1; relsec < this->NSections; relsec++) {
// Get section header
OldRelHeader = this->SectionHeaders[relsec];
// Check if this is a relocations section referring to oldsec
if ((OldRelHeader.sh_type == SHT_REL || OldRelHeader.sh_type == SHT_RELA) // if section is relocation
&& OldRelHeader.sh_info == oldsec) { // and if section refers to current section
Elf2MacRelocations(OldRelHeader, NewHeader, NewRawDataOffset, oldsec);
}
} // End of search for relocation table
// Align raw data for next section
NewRawData.Align(4);
// Fix v. 2.14: adjust NewVirtualAddress to match above alignment
NewVirtualAddress = (NewVirtualAddress + 3) & MInt(-4);
// Store section header in file
ToFile.Push(&NewHeader, sizeof(NewHeader));
} // End of if section has program data
} // End of loop through old sections
} // End of function MakeSections
template <class TELF_Header, class TELF_SectionHeader, class TELF_Symbol, class TELF_Relocation,
class TMAC_header, class TMAC_segment_command, class TMAC_section, class TMAC_nlist, class MInt>
void CELF2MAC<ELFSTRUCTURES,MACSTRUCTURES>::FindUnusedSymbols() {
// Check if symbols used, remove unused symbols
// Allocate table OldSymbolScope and OldSymbolUsed
NumOldSymbols = this->SymbolTableEntries;
if (NumOldSymbols > 0 && NumOldSymbols < 0x1000000) {
OldSymbolScope.SetNum(NumOldSymbols);
OldSymbolScope.SetZero();
OldSymbolUsed.SetNum(NumOldSymbols);
OldSymbolUsed.SetZero();
}
// Loop through section headers
for (uint32_t sc = 0; sc < this->NSections; sc++) {
uint32_t entrysize = uint32_t(this->SectionHeaders[sc].sh_entsize);
// printf("\n%2i Name: %-18s ", sc, SecStringTable + sheader.sh_name);
if ((this->SectionHeaders[sc].sh_type==SHT_REL || this->SectionHeaders[sc].sh_type==SHT_RELA)) {
// Relocation list
int8_t * reltab = this->Buf() + uint32_t(this->SectionHeaders[sc].sh_offset);
int8_t * reltabend = reltab + uint32_t(this->SectionHeaders[sc].sh_size);
uint32_t expectedentrysize = this->SectionHeaders[sc].sh_type == SHT_RELA ?
sizeof(TELF_Relocation) : // Elf32_Rela, Elf64_Rela
sizeof(TELF_Relocation) - this->WordSize/8; // Elf32_Rel, Elf64_Rel
if (entrysize < expectedentrysize) {err.submit(2033); entrysize = expectedentrysize;}
// Loop through entries
for (; reltab < reltabend; reltab += entrysize) {
int isymbol = ((TELF_Relocation*)reltab)->r_sym;
// printf("\n>SymbolUsed: %i, Name: %s",isymbol,SymbolName(isymbol));
// Remember symbol used
OldSymbolUsed[isymbol]++;
}
}
}
}
template <class TELF_Header, class TELF_SectionHeader, class TELF_Symbol, class TELF_Relocation,
class TMAC_header, class TMAC_segment_command, class TMAC_section, class TMAC_nlist, class MInt>
void CELF2MAC<ELFSTRUCTURES,MACSTRUCTURES>::MakeBinaryFile() {
// Convert subfunction: Putting sections together
// File header, segment command and section headers have been inserted.
int i;
// Update segment header for segment size in file
((TMAC_segment_command*)(ToFile.Buf()+CommandOffset))->filesize = NewRawData.GetDataSize();
// Make symbol table command
MAC_symtab_command symtab;
memset(&symtab, 0, sizeof(symtab));
symtab.cmd = MAC_LC_SYMTAB;
symtab.cmdsize = sizeof(symtab);
// symoff, nsyms, stroff, strsize inserted later
// Store symtab command
NewSymtabOffset = ToFile.Push(&symtab, sizeof(symtab));
// Make MAC_dysymtab_command command
MAC_dysymtab_command dysymtab;
memset(&dysymtab, 0, sizeof(dysymtab));
dysymtab.cmd = MAC_LC_DYSYMTAB;
dysymtab.cmdsize = sizeof(dysymtab);
dysymtab.ilocalsym = 0; // index to local symbols
dysymtab.nlocalsym = NewSymTab[0].GetNumEntries(); // number of local symbols
dysymtab.iextdefsym = dysymtab.nlocalsym; // index to externally defined symbols
dysymtab.nextdefsym = NewSymTab[1].GetNumEntries(); // number of externally defined symbols
dysymtab.iundefsym = dysymtab.iextdefsym + dysymtab.nextdefsym; // index to public symbols
dysymtab.nundefsym = NewSymTab[2].GetNumEntries(); // number of public symbols
// Store MAC_dysymtab_command command
ToFile.Push(&dysymtab, sizeof(dysymtab));
// Store section data
uint32_t Current = ToFile.Push(NewRawData.Buf(), NewRawData.GetDataSize());
if (Current != RawDataOffset) err.submit(9000);
ToFile.Align(4);
// Store relocation tables
uint32_t Reltabs = ToFile.Push(NewRelocationTab.Buf(), NewRelocationTab.GetDataSize());
// Initialize new string table. First string is empty
NewStringTable.Push(0, 1);
// Store symbol tables and make string table
// Tables are not sorted alphabetically yet. This will be done subsequently
// by CMAC2MAC
uint32_t Symtabs = ToFile.GetDataSize();
uint32_t NumSyms = 0;
for (i = 0; i < 3; i++) {
NumSyms += NewSymTab[i].GetNumEntries();
NewSymTab[i].StoreList(&ToFile, &NewStringTable);
}
// Store string table
uint32_t StringTab = ToFile.Push(NewStringTable.Buf(), NewStringTable.GetDataSize());
// Set missing values in file header
((TMAC_header*)ToFile.Buf())->sizeofcmds = RawDataOffset - sizeof(TMAC_header);
// Adjust relocation offsets in section headers
TMAC_section* sectp = (TMAC_section*)(ToFile.Buf() + NewSectHeadOffset);
for (i = 0; i < NumSectionsNew; i++, sectp++) {
sectp->reloff += Reltabs;
}
// Set missing symoff, nsyms, stroff, strsize in symtab command
MAC_symtab_command * symtabp = (MAC_symtab_command*)(ToFile.Buf() + NewSymtabOffset);
symtabp->symoff = Symtabs;
symtabp->nsyms = NumSyms;
symtabp->stroff = StringTab;
symtabp->strsize = NewStringTable.GetDataSize();
}
template <class TELF_Header, class TELF_SectionHeader, class TELF_Symbol, class TELF_Relocation,
class TMAC_header, class TMAC_segment_command, class TMAC_section, class TMAC_nlist, class MInt>
int CELF2MAC<ELFSTRUCTURES,MACSTRUCTURES>::GetImagebaseSymbol() {
// Get symbol table index of __mh_execute_header = image base
// Create this symbol table entry if it doesn't exist
const char * ImageBaseName = "__mh_execute_header";
static int ImagebaseSymbol = -1;
if (ImagebaseSymbol >= 0) {
// Found previously
return ImagebaseSymbol;
}
// Search for name among external symbols
int index2 = NewSymTab[2].Search(ImageBaseName);
if (index2 >= 0) {
// found
ImagebaseSymbol = index2 + NumSymbols[2];
return ImagebaseSymbol;
}
// Not found. Create symbol
NewSymTab[2].AddSymbol(NumOldSymbols, ImageBaseName, MAC_N_EXT, 0, 0, 0);
ImagebaseSymbol = NewSymTab[2].TranslateIndex(NumOldSymbols) + NumSymbols[2];
NumSymbols[3]++;
return ImagebaseSymbol;
}
// Make template instances for 32 and 64 bits
template class CELF2MAC<ELF32STRUCTURES,MAC32STRUCTURES>;
template class CELF2MAC<ELF64STRUCTURES,MAC64STRUCTURES>;
|