_endOfLoadCommands += sizeof(linkedit_data_command);
_countOfLoadCommands++;
}
- // Accumulate size of each section.
+ // Assign file offsets to each section.
_startOfSectionsContent = _endOfLoadCommands;
- _endOfSectionsContent = _startOfSectionsContent;
unsigned relocCount = 0;
+ uint64_t offset = _startOfSectionsContent;
for (const Section § : file.sections) {
- _sectInfo[§].fileOffset = _endOfSectionsContent;
- _endOfSectionsContent += sect.content.size();
+ if (sect.type != llvm::MachO::S_ZEROFILL) {
+ offset = llvm::RoundUpToAlignment(offset, 1 << sect.alignment);
+ _sectInfo[§].fileOffset = offset;
+ offset += sect.content.size();
+ } else {
+ _sectInfo[§].fileOffset = 0;
+ }
relocCount += sect.relocations.size();
}
+ _endOfSectionsContent = offset;
computeSymbolTableSizes();
computeDataInCodeSize();
uint8_t *next = lc + seg->cmdsize;
memset(seg->segname, 0, 16);
seg->vmaddr = 0;
- seg->vmsize = _endOfSectionsContent - _endOfLoadCommands;
+ seg->vmsize = _file.sections.back().address
+ + _file.sections.back().content.size();
seg->fileoff = _endOfLoadCommands;
seg->filesize = seg->vmsize;
seg->maxprot = VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE;
typename T::section *sout = reinterpret_cast<typename T::section*>
(lc+sizeof(typename T::command));
uint32_t relOffset = _startOfRelocations;
- uint32_t contentOffset = _startOfSectionsContent;
uint32_t indirectSymRunningIndex = 0;
for (const Section &sin : _file.sections) {
setString16(sin.sectionName, sout->sectname);
setString16(sin.segmentName, sout->segname);
sout->addr = sin.address;
sout->size = sin.content.size();
- sout->offset = contentOffset;
+ sout->offset = _sectInfo[&sin].fileOffset;
sout->align = sin.alignment;
sout->reloff = sin.relocations.empty() ? 0 : relOffset;
sout->nreloc = sin.relocations.size();
sout->reserved1 = indirectSymbolIndex(sin, indirectSymRunningIndex);
sout->reserved2 = indirectSymbolElementSize(sin);
relOffset += sin.relocations.size() * sizeof(any_relocation_info);
- contentOffset += sin.content.size();
if (_swap)
swapStruct(*sout);
++sout;
setString16(section->segmentName, sect->segname);
sect->addr = section->address;
sect->size = section->content.size();
- sect->offset = section->address - seg.address + segInfo.fileOffset;
+ if (section->type == llvm::MachO::S_ZEROFILL)
+ sect->offset = 0;
+ else
+ sect->offset = section->address - seg.address + segInfo.fileOffset;
sect->align = section->alignment;
sect->reloff = 0;
sect->nreloc = 0;
};
for (SectionInfo *si : _sectionInfos) {
- if (si->type == llvm::MachO::S_ZEROFILL)
+ Section *normSect = &file.sections[si->normalizedSectionIndex];
+ if (si->type == llvm::MachO::S_ZEROFILL) {
+ const uint8_t *empty = nullptr;
+ normSect->content = llvm::makeArrayRef(empty, si->size);
continue;
+ }
// Copy content from atoms to content buffer for section.
uint8_t *sectionContent = file.ownedAllocations.Allocate<uint8_t>(si->size);
- Section *normSect = &file.sections[si->normalizedSectionIndex];
normSect->content = llvm::makeArrayRef(sectionContent, si->size);
for (AtomInfo &ai : si->atomsAndOffsets) {
uint8_t *atomContent = reinterpret_cast<uint8_t*>