- }
-
- // Resolve the addresses of any symbols that were defined in this segment.
- for (int i = 0, e = SymbolNames.size(); i != e; ++i)
- resolveSymbol(SymbolNames[i]);
-
- return false;
-}
-
-
-bool RuntimeDyldMachO::
-loadSegment64(const MachOObject *Obj,
- const MachOObject::LoadCommandInfo *SegmentLCI,
- const InMemoryStruct<macho::SymtabLoadCommand> &SymtabLC) {
- InMemoryStruct<macho::Segment64LoadCommand> Segment64LC;
- Obj->ReadSegment64LoadCommand(*SegmentLCI, Segment64LC);
- if (!Segment64LC)
- return Error("unable to load segment load command");
-
-
- SmallVector<unsigned, 16> SectionMap;
- for (unsigned SectNum = 0; SectNum != Segment64LC->NumSections; ++SectNum) {
- InMemoryStruct<macho::Section64> Sect;
- Obj->ReadSection64(*SegmentLCI, SectNum, Sect);
- if (!Sect)
- return Error("unable to load section: '" + Twine(SectNum) + "'");
-
- // Allocate memory via the MM for the section.
- uint8_t *Buffer;
- uint32_t SectionID = Sections.size();
- unsigned Align = 1 << Sect->Align; // .o file has log2 alignment.
- if (Sect->Flags == 0x80000400)
- Buffer = MemMgr->allocateCodeSection(Sect->Size, Align, SectionID);
- else
- Buffer = MemMgr->allocateDataSection(Sect->Size, Align, SectionID);
-
- DEBUG(dbgs() << "Loading "
- << ((Sect->Flags == 0x80000400) ? "text" : "data")
- << " (ID #" << SectionID << ")"
- << " '" << Sect->SegmentName << ","
- << Sect->Name << "' of size " << Sect->Size
- << " (align " << Align << ")"
- << " to address " << Buffer << ".\n");
-
- // Copy the payload from the object file into the allocated buffer.
- uint8_t *Base = (uint8_t*)Obj->getData(Segment64LC->FileOffset,
- Segment64LC->FileSize).data();
- memcpy(Buffer, Base + Sect->Address, Sect->Size);
-
- // Remember what got allocated for this SectionID.
- Sections.push_back(sys::MemoryBlock(Buffer, Sect->Size));
- SectionLocalMemToID[Buffer] = SectionID;
-
- // By default, the load address of a section is its memory buffer.
- SectionLoadAddress.push_back((uint64_t)Buffer);
-
- // Keep a map of object file section numbers to corresponding SectionIDs
- // while processing the file.
- SectionMap.push_back(SectionID);
- }
-
- // Process the symbol table.
- SmallVector<StringRef, 64> SymbolNames;
- processSymbols64(Obj, SectionMap, SymbolNames, SymtabLC);
-
- // Process the relocations for each section we're loading.
- Relocations.grow(Relocations.size() + Segment64LC->NumSections);
- Referrers.grow(Referrers.size() + Segment64LC->NumSections);
- for (unsigned SectNum = 0; SectNum != Segment64LC->NumSections; ++SectNum) {
- InMemoryStruct<macho::Section64> Sect;
- Obj->ReadSection64(*SegmentLCI, SectNum, Sect);
- if (!Sect)
- return Error("unable to load section: '" + Twine(SectNum) + "'");
- for (unsigned j = 0; j != Sect->NumRelocationTableEntries; ++j) {
- InMemoryStruct<macho::RelocationEntry> RE;
- Obj->ReadRelocationEntry(Sect->RelocationTableOffset, j, RE);
- if (RE->Word0 & macho::RF_Scattered)
- return Error("NOT YET IMPLEMENTED: scattered relocations.");
- // Word0 of the relocation is the offset into the section where the
- // relocation should be applied. We need to translate that into an
- // offset into a function since that's our atom.
- uint32_t Offset = RE->Word0;
- bool isExtern = (RE->Word1 >> 27) & 1;
-
- // FIXME: Get the relocation addend from the target address.
- // FIXME: VERY imporant for internal relocations.
-
- // Figure out the source symbol of the relocation. If isExtern is true,
- // this relocation references the symbol table, otherwise it references
- // a section in the same object, numbered from 1 through NumSections
- // (SectionBases is [0, NumSections-1]).
- uint32_t SourceNum = RE->Word1 & 0xffffff; // 24-bit value
- if (!isExtern) {
- assert(SourceNum > 0 && "Invalid relocation section number!");
- unsigned SectionID = SectionMap[SourceNum - 1];
- unsigned TargetID = SectionMap[SectNum];
- DEBUG(dbgs() << "Internal relocation at Section #"
- << TargetID << " + " << Offset
- << " from Section #"
- << SectionID << " (Word1: "
- << format("0x%x", RE->Word1) << ")\n");
-
- // Store the relocation information. It will get resolved when
- // the section addresses are assigned.
- uint32_t RelocationIndex = Relocations[SectionID].size();
- Relocations[SectionID].push_back(RelocationEntry(TargetID,
- Offset,
- RE->Word1,
- 0 /*Addend*/));
- Referrers[TargetID].push_back(Referrer(SectionID, RelocationIndex));
- } else {
- StringRef SourceName = SymbolNames[SourceNum];
-
- // Now store the relocation information. Associate it with the source
- // symbol. Just add it to the unresolved list and let the general
- // path post-load resolve it if we know where the symbol is.
- UnresolvedRelocations[SourceName].push_back(RelocationEntry(SectNum,
- Offset,
- RE->Word1,
- 0 /*Addend*/));
- DEBUG(dbgs() << "Relocation at Section #" << SectNum << " + " << Offset
- << " from '" << SourceName << "(Word1: "
- << format("0x%x", RE->Word1) << ")\n");
- }