-static bool
-loadSegment32(const MachOObject *Obj,
- sys::MemoryBlock &Data,
- const MachOObject::LoadCommandInfo *SegmentLCI,
- const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC,
- StringMap<void*> &SymbolTable) {
- InMemoryStruct<macho::SegmentLoadCommand> Segment32LC;
- Obj->ReadSegmentLoadCommand(*SegmentLCI, Segment32LC);
- if (!Segment32LC)
- return Error("unable to load segment load command");
-
- // Map the segment into memory.
- std::string ErrorStr;
- Data = sys::Memory::AllocateRWX(Segment32LC->VMSize, 0, &ErrorStr);
- if (!Data.base())
- return Error("unable to allocate memory block: '" + ErrorStr + "'");
- memcpy(Data.base(), Obj->getData(Segment32LC->FileOffset,
- Segment32LC->FileSize).data(),
- Segment32LC->FileSize);
- memset((char*)Data.base() + Segment32LC->FileSize, 0,
- Segment32LC->VMSize - Segment32LC->FileSize);
-
- // Bind the section indices to address.
- void **SectionBases = new void*[Segment32LC->NumSections];
- for (unsigned i = 0; i != Segment32LC->NumSections; ++i) {
- InMemoryStruct<macho::Section> Sect;
- Obj->ReadSection(*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.
- for (unsigned i = 0; i != SymtabLC->NumSymbolTableEntries; ++i) {
- InMemoryStruct<macho::SymbolTableEntry> STE;
- Obj->ReadSymbolTableEntry(SymtabLC->SymbolTableOffset, i, STE);
- if (!STE)
- return Error("unable to read symbol: '" + Twine(i) + "'");
- if (STE->SectionIndex == 0)
- return Error("unexpected undefined symbol!");