- if (!SymtabLCI)
- return Error("no symbol table found in object");
- if (!SegmentLCI)
- return Error("no symbol table found in object");
-
- // Read and register the symbol table data.
- InMemoryStruct<macho::SymtabLoadCommand> SymtabLC;
- Obj->ReadSymtabLoadCommand(*SymtabLCI, SymtabLC);
- if (!SymtabLC)
- return Error("unable to load symbol table load command");
- Obj->RegisterStringTable(*SymtabLC);
-
- // Read the dynamic link-edit information, if present (not present in static
- // objects).
- if (DysymtabLCI) {
- InMemoryStruct<macho::DysymtabLoadCommand> DysymtabLC;
- Obj->ReadDysymtabLoadCommand(*DysymtabLCI, DysymtabLC);
- if (!DysymtabLC)
- return Error("unable to load dynamic link-exit load command");
-
- // FIXME: We don't support anything interesting yet.
- if (DysymtabLC->LocalSymbolsIndex != 0)
- return Error("NOT YET IMPLEMENTED: local symbol entries");
- if (DysymtabLC->ExternalSymbolsIndex != 0)
- return Error("NOT YET IMPLEMENTED: non-external symbol entries");
- if (DysymtabLC->UndefinedSymbolsIndex != SymtabLC->NumSymbolTableEntries)
- return Error("NOT YET IMPLEMENTED: undefined symbol entries");
- }
-
- // Load the segment load command.
- if (SegmentLCI->Command.Type != macho::LCT_Segment64)
- return Error("Segment32 not yet implemented!");
- InMemoryStruct<macho::Segment64LoadCommand> Segment64LC;
- Obj->ReadSegment64LoadCommand(*SegmentLCI, Segment64LC);
- if (!Segment64LC)
- return Error("unable to load segment load command");
-
- // Map the segment into memory.
- sys::MemoryBlock Data = sys::Memory::AllocateRWX(Segment64LC->VMSize,
- 0, &ErrorStr);
- if (!Data.base())
- return Error("unable to allocate memory block: '" + ErrorStr + "'");
- memcpy(Data.base(), Obj->getData(Segment64LC->FileOffset,
- Segment64LC->FileSize).data(),
- Segment64LC->FileSize);
- memset((char*)Data.base() + Segment64LC->FileSize, 0,
- Segment64LC->VMSize - Segment64LC->FileSize);
-
- // Bind the section indices to address.
- void **SectionBases = new void*[Segment64LC->NumSections];
- for (unsigned i = 0; i != Segment64LC->NumSections; ++i) {
- InMemoryStruct<macho::Section64> Sect;
- Obj->ReadSection64(*SegmentLCI, i, Sect);
- if (!Sect)
- return Error("unable to load section: '" + Twine(i) + "'");
-
- // FIXME: We don't support relocations yet.
- if (Sect->NumRelocationTableEntries != 0)
- return Error("not yet implemented: relocations!");
-
- // FIXME: Improve check.
- if (Sect->Flags != 0x80000400)
- return Error("unsupported section type!");
-
- SectionBases[i] = (char*) Data.base() + Sect->Address;
- }
-
- // Bind all the symbols to address.
- StringMap<void*> SymbolTable;
- for (unsigned i = 0; i != SymtabLC->NumSymbolTableEntries; ++i) {
- InMemoryStruct<macho::Symbol64TableEntry> STE;
- Obj->ReadSymbol64TableEntry(SymtabLC->SymbolTableOffset, i, STE);
- if (!STE)
- return Error("unable to read symbol: '" + Twine(i) + "'");
- if (STE->SectionIndex == 0)
- return Error("unexpected undefined symbol!");
-
- unsigned Index = STE->SectionIndex - 1;
- if (Index >= Segment64LC->NumSections)
- return Error("invalid section index for symbol: '" + Twine() + "'");