aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaniel Dunbar <daniel@zuster.org>2009-08-22 08:28:27 +0000
committerDaniel Dunbar <daniel@zuster.org>2009-08-22 08:28:27 +0000
commit124c5049b1234493b7c0a122a9b8803cc7f0b4d1 (patch)
tree815b09c32b58fe3f55ad7601aa49ba2581c2ee90
parent1f022e2852c1f7e692f0d2990de892f5dd820c0a (diff)
downloadexternal_llvm-124c5049b1234493b7c0a122a9b8803cc7f0b4d1.zip
external_llvm-124c5049b1234493b7c0a122a9b8803cc7f0b4d1.tar.gz
external_llvm-124c5049b1234493b7c0a122a9b8803cc7f0b4d1.tar.bz2
llvm-mc/Mach-O: Move more logic for writing the Mach-O file into the writer
class, and kill off MCSectionData::FileOffset. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@79735 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--include/llvm/MC/MCAssembler.h10
-rw-r--r--lib/MC/MCAssembler.cpp67
2 files changed, 40 insertions, 37 deletions
diff --git a/include/llvm/MC/MCAssembler.h b/include/llvm/MC/MCAssembler.h
index bb2cda9..86ad502 100644
--- a/include/llvm/MC/MCAssembler.h
+++ b/include/llvm/MC/MCAssembler.h
@@ -254,10 +254,6 @@ private:
//
// FIXME: This could all be kept private to the assembler implementation.
- /// FileOffset - The offset of this section in the object file. This is ~0
- /// until initialized.
- uint64_t FileOffset;
-
/// FileSize - The size of this section in the object file. This is ~0 until
/// initialized.
uint64_t FileSize;
@@ -301,12 +297,6 @@ public:
}
void setFileSize(uint64_t Value) { FileSize = Value; }
- uint64_t getFileOffset() const {
- assert(FileOffset != ~UINT64_C(0) && "File offset not set!");
- return FileOffset;
- }
- void setFileOffset(uint64_t Value) { FileOffset = Value; }
-
/// @}
};
diff --git a/lib/MC/MCAssembler.cpp b/lib/MC/MCAssembler.cpp
index 16864f9..f822711 100644
--- a/lib/MC/MCAssembler.cpp
+++ b/lib/MC/MCAssembler.cpp
@@ -102,11 +102,6 @@ public:
/// @}
- static unsigned getPrologSize32(unsigned NumSections) {
- return Header32Size + SegmentLoadCommand32Size +
- NumSections * Section32Size;
- }
-
void WriteHeader32(unsigned NumSections) {
// struct mach_header (28 bytes)
@@ -166,7 +161,7 @@ public:
assert(OS.tell() - Start == SegmentLoadCommand32Size);
}
- void WriteSection32(const MCSectionData &SD) {
+ void WriteSection32(const MCSectionData &SD, uint64_t FileOffset) {
// struct section (68 bytes)
uint64_t Start = OS.tell();
@@ -179,7 +174,7 @@ public:
WriteString(Section.getSegmentName(), 16);
Write32(0); // address
Write32(SD.getFileSize()); // size
- Write32(SD.getFileOffset());
+ Write32(FileOffset);
assert(isPowerOf2_32(SD.getAlignment()) && "Invalid alignment!");
Write32(Log2_32(SD.getAlignment()));
@@ -191,6 +186,39 @@ public:
assert(OS.tell() - Start == Section32Size);
}
+
+ void WriteProlog(MCAssembler &Asm) {
+ unsigned NumSections = Asm.size();
+
+ // Compute the file offsets for all the sections in advance, so that we can
+ // write things out in order.
+ SmallVector<uint64_t, 16> SectionFileOffsets;
+ SectionFileOffsets.resize(NumSections);
+
+ // The section data starts after the header, the segment load command, and
+ // the section headers.
+ uint64_t FileOffset = Header32Size + SegmentLoadCommand32Size +
+ NumSections * Section32Size;
+ uint64_t SectionDataSize = 0;
+ unsigned Index = 0;
+ for (MCAssembler::iterator it = Asm.begin(),
+ ie = Asm.end(); it != ie; ++it, ++Index) {
+ SectionFileOffsets[Index] = FileOffset;
+ FileOffset += it->getFileSize();
+ SectionDataSize += it->getFileSize();
+ }
+
+ // Write the prolog, starting with the header and load command...
+ WriteHeader32(NumSections);
+ WriteSegmentLoadCommand32(NumSections, SectionDataSize);
+
+ // ... and then the section headers.
+ Index = 0;
+ for (MCAssembler::iterator it = Asm.begin(),
+ ie = Asm.end(); it != ie; ++it, ++Index)
+ WriteSection32(*it, SectionFileOffsets[Index]);
+ }
+
};
}
@@ -218,7 +246,6 @@ MCSectionData::MCSectionData() : Section(*(MCSection*)0) {}
MCSectionData::MCSectionData(const MCSection &_Section, MCAssembler *A)
: Section(_Section),
Alignment(1),
- FileOffset(~UINT64_C(0)),
FileSize(~UINT64_C(0))
{
if (A)
@@ -369,31 +396,17 @@ static void WriteFileData(raw_ostream &OS, const MCSectionData &SD,
}
void MCAssembler::Finish() {
- unsigned NumSections = Sections.size();
-
// Layout the sections and fragments.
- uint64_t Offset = MachObjectWriter::getPrologSize32(NumSections);
- uint64_t SectionDataSize = 0;
- for (iterator it = begin(), ie = end(); it != ie; ++it) {
- it->setFileOffset(Offset);
-
+ for (iterator it = begin(), ie = end(); it != ie; ++it)
LayoutSection(*it);
- Offset += it->getFileSize();
- SectionDataSize += it->getFileSize();
- }
-
MachObjectWriter MOW(OS);
- // Write the prolog, starting with the header and load command...
- MOW.WriteHeader32(NumSections);
- MOW.WriteSegmentLoadCommand32(NumSections, SectionDataSize);
-
- // ... and then the section headers.
- for (iterator it = begin(), ie = end(); it != ie; ++it)
- MOW.WriteSection32(*it);
+ // Write the prolog, followed by the data for all the sections & fragments.
+ MOW.WriteProlog(*this);
- // Finally, write the section data.
+ // FIXME: This should move into the Mach-O writer, it should have control over
+ // what goes where.
for (iterator it = begin(), ie = end(); it != ie; ++it)
WriteFileData(OS, *it, MOW);