@@ -73,32 +73,30 @@ struct Symbol {
73
73
};
74
74
75
75
// Wrapper for an MCSectionXCOFF.
76
- struct ControlSection {
77
- const MCSectionXCOFF *const MCCsect;
76
+ // It can be a Csect or debug section or DWARF section and so on.
77
+ struct XCOFFSection {
78
+ const MCSectionXCOFF *const MCSec;
78
79
uint32_t SymbolTableIndex;
79
80
uint32_t Address;
80
81
uint32_t Size ;
81
82
82
83
SmallVector<Symbol, 1 > Syms;
83
84
SmallVector<XCOFFRelocation, 1 > Relocations;
84
- StringRef getSymbolTableName () const { return MCCsect ->getSymbolTableName (); }
85
- ControlSection (const MCSectionXCOFF *MCSec)
86
- : MCCsect (MCSec), SymbolTableIndex(-1 ), Address(-1 ), Size (0 ) {}
85
+ StringRef getSymbolTableName () const { return MCSec ->getSymbolTableName (); }
86
+ XCOFFSection (const MCSectionXCOFF *MCSec)
87
+ : MCSec (MCSec), SymbolTableIndex(-1 ), Address(-1 ), Size (0 ) {}
87
88
};
88
89
89
90
// Type to be used for a container representing a set of csects with
90
91
// (approximately) the same storage mapping class. For example all the csects
91
92
// with a storage mapping class of `xmc_pr` will get placed into the same
92
93
// container.
93
- using CsectGroup = std::deque<ControlSection >;
94
+ using CsectGroup = std::deque<XCOFFSection >;
94
95
using CsectGroups = std::deque<CsectGroup *>;
95
96
96
- // Represents the data related to a section excluding the csects that make up
97
- // the raw data of the section. The csects are stored separately as not all
98
- // sections contain csects, and some sections contain csects which are better
99
- // stored separately, e.g. the .data section containing read-write, descriptor,
100
- // TOCBase and TOC-entry csects.
101
- struct Section {
97
+ // The basic section entry defination. This Section represents a section entry
98
+ // in XCOFF section header table.
99
+ struct SectionEntry {
102
100
char Name[XCOFF::NameSize];
103
101
// The physical/virtual address of the section. For an object file
104
102
// these values are equivalent.
@@ -111,9 +109,6 @@ struct Section {
111
109
112
110
int16_t Index;
113
111
114
- // Virtual sections do not need storage allocated in the object file.
115
- const bool IsVirtual;
116
-
117
112
// XCOFF has special section numbers for symbols:
118
113
// -2 Specifies N_DEBUG, a special symbolic debugging symbol.
119
114
// -1 Specifies N_ABS, an absolute symbol. The symbol has a value but is not
@@ -124,28 +119,53 @@ struct Section {
124
119
static constexpr int16_t UninitializedIndex =
125
120
XCOFF::ReservedSectionNum::N_DEBUG - 1 ;
126
121
127
- CsectGroups Groups;
122
+ SectionEntry (StringRef N, int32_t Flags)
123
+ : Name(), Address(0 ), Size (0 ), FileOffsetToData(0 ),
124
+ FileOffsetToRelocations (0 ), RelocationCount(0 ), Flags(Flags),
125
+ Index(UninitializedIndex) {
126
+ assert (N.size () <= XCOFF::NameSize && " section name too long" );
127
+ memcpy (Name, N.data (), N.size ());
128
+ }
128
129
129
- void reset () {
130
+ virtual void reset () {
130
131
Address = 0 ;
131
132
Size = 0 ;
132
133
FileOffsetToData = 0 ;
133
134
FileOffsetToRelocations = 0 ;
134
135
RelocationCount = 0 ;
135
136
Index = UninitializedIndex;
136
- // Clear any csects we have stored.
137
- for (auto *Group : Groups)
138
- Group->clear ();
139
137
}
140
138
141
- Section (StringRef N, XCOFF::SectionTypeFlags Flags, bool IsVirtual,
142
- CsectGroups Groups)
143
- : Name(), Address(0 ), Size (0 ), FileOffsetToData(0 ),
144
- FileOffsetToRelocations (0 ), RelocationCount(0 ), Flags(Flags),
145
- Index(UninitializedIndex), IsVirtual(IsVirtual), Groups(Groups) {
139
+ virtual ~SectionEntry () {}
140
+ };
141
+
142
+ // Represents the data related to a section excluding the csects that make up
143
+ // the raw data of the section. The csects are stored separately as not all
144
+ // sections contain csects, and some sections contain csects which are better
145
+ // stored separately, e.g. the .data section containing read-write, descriptor,
146
+ // TOCBase and TOC-entry csects.
147
+ struct CsectSectionEntry : public SectionEntry {
148
+ // Virtual sections do not need storage allocated in the object file.
149
+ const bool IsVirtual;
150
+
151
+ // This is a section containing csect groups.
152
+ CsectGroups Groups;
153
+
154
+ CsectSectionEntry (StringRef N, XCOFF::SectionTypeFlags Flags, bool IsVirtual,
155
+ CsectGroups Groups)
156
+ : SectionEntry(N, Flags), IsVirtual(IsVirtual), Groups(Groups) {
146
157
assert (N.size () <= XCOFF::NameSize && " section name too long" );
147
158
memcpy (Name, N.data (), N.size ());
148
159
}
160
+
161
+ void reset () override {
162
+ SectionEntry::reset ();
163
+ // Clear any csects we have stored.
164
+ for (auto *Group : Groups)
165
+ Group->clear ();
166
+ }
167
+
168
+ virtual ~CsectSectionEntry () {}
149
169
};
150
170
151
171
class XCOFFObjectWriter : public MCObjectWriter {
@@ -159,10 +179,10 @@ class XCOFFObjectWriter : public MCObjectWriter {
159
179
std::unique_ptr<MCXCOFFObjectTargetWriter> TargetObjectWriter;
160
180
StringTableBuilder Strings;
161
181
162
- // Maps the MCSection representation to its corresponding ControlSection
163
- // wrapper. Needed for finding the ControlSection to insert an MCSymbol into
182
+ // Maps the MCSection representation to its corresponding XCOFFSection
183
+ // wrapper. Needed for finding the XCOFFSection to insert an MCSymbol into
164
184
// from its containing MCSectionXCOFF.
165
- DenseMap<const MCSectionXCOFF *, ControlSection *> SectionMap;
185
+ DenseMap<const MCSectionXCOFF *, XCOFFSection *> SectionMap;
166
186
167
187
// Maps the MCSymbol representation to its corrresponding symbol table index.
168
188
// Needed for relocation.
@@ -182,15 +202,16 @@ class XCOFFObjectWriter : public MCObjectWriter {
182
202
CsectGroup TBSSCsects;
183
203
184
204
// The Predefined sections.
185
- Section Text;
186
- Section Data;
187
- Section BSS;
188
- Section TData;
189
- Section TBSS;
205
+ CsectSectionEntry Text;
206
+ CsectSectionEntry Data;
207
+ CsectSectionEntry BSS;
208
+ CsectSectionEntry TData;
209
+ CsectSectionEntry TBSS;
190
210
191
211
// All the XCOFF sections, in the order they will appear in the section header
192
212
// table.
193
- std::array<Section *const , 5 > Sections{{&Text, &Data, &BSS, &TData, &TBSS}};
213
+ std::array<CsectSectionEntry *const , 5 > Sections{
214
+ {&Text, &Data, &BSS, &TData, &TBSS}};
194
215
195
216
CsectGroup &getCsectGroup (const MCSectionXCOFF *MCSec);
196
217
@@ -206,16 +227,16 @@ class XCOFFObjectWriter : public MCObjectWriter {
206
227
static bool nameShouldBeInStringTable (const StringRef &);
207
228
void writeSymbolName (const StringRef &);
208
229
void writeSymbolTableEntryForCsectMemberLabel (const Symbol &,
209
- const ControlSection &, int16_t ,
230
+ const XCOFFSection &, int16_t ,
210
231
uint64_t );
211
- void writeSymbolTableEntryForControlSection (const ControlSection &, int16_t ,
232
+ void writeSymbolTableEntryForControlSection (const XCOFFSection &, int16_t ,
212
233
XCOFF::StorageClass);
213
234
void writeFileHeader ();
214
235
void writeSectionHeaderTable ();
215
236
void writeSections (const MCAssembler &Asm, const MCAsmLayout &Layout);
216
237
void writeSymbolTable (const MCAsmLayout &Layout);
217
238
void writeRelocations ();
218
- void writeRelocation (XCOFFRelocation Reloc, const ControlSection &CSection);
239
+ void writeRelocation (XCOFFRelocation Reloc, const XCOFFSection &CSection);
219
240
220
241
// Called after all the csects and symbols have been processed by
221
242
// `executePostLayoutBinding`, this function handles building up the majority
@@ -350,7 +371,7 @@ void XCOFFObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
350
371
for (const auto &S : Asm) {
351
372
const auto *MCSec = cast<const MCSectionXCOFF>(&S);
352
373
assert (SectionMap.find (MCSec) == SectionMap.end () &&
353
- " Cannot add a csect twice." );
374
+ " Cannot add a section twice." );
354
375
assert (XCOFF::XTY_ER != MCSec->getCSectType () &&
355
376
" An undefined csect should not get registered." );
356
377
@@ -392,8 +413,10 @@ void XCOFFObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
392
413
393
414
assert (SectionMap.find (ContainingCsect) != SectionMap.end () &&
394
415
" Expected containing csect to exist in map" );
416
+ XCOFFSection *Csect = SectionMap[ContainingCsect];
395
417
// Lookup the containing csect and add the symbol to it.
396
- SectionMap[ContainingCsect]->Syms .emplace_back (XSym);
418
+ assert (Csect->MCSec ->isCsect () && " only csect is supported now!" );
419
+ Csect->Syms .emplace_back (XSym);
397
420
398
421
// If the name does not fit in the storage provided in the symbol table
399
422
// entry, add it to the string table.
@@ -517,7 +540,8 @@ void XCOFFObjectWriter::writeSections(const MCAssembler &Asm,
517
540
uint32_t CurrentAddressLocation = 0 ;
518
541
for (const auto *Section : Sections) {
519
542
// Nothing to write for this Section.
520
- if (Section->Index == Section::UninitializedIndex || Section->IsVirtual )
543
+ if (Section->Index == SectionEntry::UninitializedIndex ||
544
+ Section->IsVirtual )
521
545
continue ;
522
546
523
547
// There could be a gap (without corresponding zero padding) between
@@ -535,7 +559,7 @@ void XCOFFObjectWriter::writeSections(const MCAssembler &Asm,
535
559
if (uint32_t PaddingSize = Csect.Address - CurrentAddressLocation)
536
560
W.OS .write_zeros (PaddingSize);
537
561
if (Csect.Size )
538
- Asm.writeSectionData (W.OS , Csect.MCCsect , Layout);
562
+ Asm.writeSectionData (W.OS , Csect.MCSec , Layout);
539
563
CurrentAddressLocation = Csect.Address + Csect.Size ;
540
564
}
541
565
}
@@ -594,7 +618,7 @@ void XCOFFObjectWriter::writeSymbolName(const StringRef &SymbolName) {
594
618
}
595
619
596
620
void XCOFFObjectWriter::writeSymbolTableEntryForCsectMemberLabel (
597
- const Symbol &SymbolRef, const ControlSection &CSectionRef,
621
+ const Symbol &SymbolRef, const XCOFFSection &CSectionRef,
598
622
int16_t SectionIndex, uint64_t SymbolOffset) {
599
623
// Name or Zeros and string table offset
600
624
writeSymbolName (SymbolRef.getSymbolTableName ());
@@ -623,15 +647,15 @@ void XCOFFObjectWriter::writeSymbolTableEntryForCsectMemberLabel(
623
647
// Symbol type: Label
624
648
W.write <uint8_t >(XCOFF::XTY_LD);
625
649
// Storage mapping class.
626
- W.write <uint8_t >(CSectionRef.MCCsect ->getMappingClass ());
650
+ W.write <uint8_t >(CSectionRef.MCSec ->getMappingClass ());
627
651
// Reserved (x_stab).
628
652
W.write <uint32_t >(0 );
629
653
// Reserved (x_snstab).
630
654
W.write <uint16_t >(0 );
631
655
}
632
656
633
657
void XCOFFObjectWriter::writeSymbolTableEntryForControlSection (
634
- const ControlSection &CSectionRef, int16_t SectionIndex,
658
+ const XCOFFSection &CSectionRef, int16_t SectionIndex,
635
659
XCOFF::StorageClass StorageClass) {
636
660
// n_name, n_zeros, n_offset
637
661
writeSymbolName (CSectionRef.getSymbolTableName ());
@@ -659,9 +683,9 @@ void XCOFFObjectWriter::writeSymbolTableEntryForControlSection(
659
683
// Typecheck section number. Not supported.
660
684
W.write <uint16_t >(0 );
661
685
// Symbol type.
662
- W.write <uint8_t >(getEncodedType (CSectionRef.MCCsect ));
686
+ W.write <uint8_t >(getEncodedType (CSectionRef.MCSec ));
663
687
// Storage mapping class.
664
- W.write <uint8_t >(CSectionRef.MCCsect ->getMappingClass ());
688
+ W.write <uint8_t >(CSectionRef.MCSec ->getMappingClass ());
665
689
// Reserved (x_stab).
666
690
W.write <uint32_t >(0 );
667
691
// Reserved (x_snstab).
@@ -689,7 +713,7 @@ void XCOFFObjectWriter::writeFileHeader() {
689
713
void XCOFFObjectWriter::writeSectionHeaderTable () {
690
714
for (const auto *Sec : Sections) {
691
715
// Nothing to write for this Section.
692
- if (Sec->Index == Section ::UninitializedIndex)
716
+ if (Sec->Index == SectionEntry ::UninitializedIndex)
693
717
continue ;
694
718
695
719
// Write Name.
@@ -718,7 +742,7 @@ void XCOFFObjectWriter::writeSectionHeaderTable() {
718
742
}
719
743
720
744
void XCOFFObjectWriter::writeRelocation (XCOFFRelocation Reloc,
721
- const ControlSection &CSection) {
745
+ const XCOFFSection &CSection) {
722
746
W.write <uint32_t >(CSection.Address + Reloc.FixupOffsetInCsect );
723
747
W.write <uint32_t >(Reloc.SymbolTableIndex );
724
748
W.write <uint8_t >(Reloc.SignAndSize );
@@ -727,7 +751,7 @@ void XCOFFObjectWriter::writeRelocation(XCOFFRelocation Reloc,
727
751
728
752
void XCOFFObjectWriter::writeRelocations () {
729
753
for (const auto *Section : Sections) {
730
- if (Section->Index == Section ::UninitializedIndex)
754
+ if (Section->Index == SectionEntry ::UninitializedIndex)
731
755
// Nothing to write for this Section.
732
756
continue ;
733
757
@@ -769,12 +793,13 @@ void XCOFFObjectWriter::writeSymbolTable(const MCAsmLayout &Layout) {
769
793
W.write <uint8_t >(0 );
770
794
771
795
for (const auto &Csect : UndefinedCsects) {
772
- writeSymbolTableEntryForControlSection (
773
- Csect, XCOFF::ReservedSectionNum::N_UNDEF, Csect.MCCsect ->getStorageClass ());
796
+ writeSymbolTableEntryForControlSection (Csect,
797
+ XCOFF::ReservedSectionNum::N_UNDEF,
798
+ Csect.MCSec ->getStorageClass ());
774
799
}
775
800
776
801
for (const auto *Section : Sections) {
777
- if (Section->Index == Section ::UninitializedIndex)
802
+ if (Section->Index == SectionEntry ::UninitializedIndex)
778
803
// Nothing to write for this Section.
779
804
continue ;
780
805
@@ -785,8 +810,8 @@ void XCOFFObjectWriter::writeSymbolTable(const MCAsmLayout &Layout) {
785
810
const int16_t SectionIndex = Section->Index ;
786
811
for (const auto &Csect : *Group) {
787
812
// Write out the control section first and then each symbol in it.
788
- writeSymbolTableEntryForControlSection (
789
- Csect, SectionIndex, Csect.MCCsect ->getStorageClass ());
813
+ writeSymbolTableEntryForControlSection (Csect, SectionIndex,
814
+ Csect.MCSec ->getStorageClass ());
790
815
791
816
for (const auto &Sym : Csect.Syms )
792
817
writeSymbolTableEntryForCsectMemberLabel (
@@ -798,7 +823,7 @@ void XCOFFObjectWriter::writeSymbolTable(const MCAsmLayout &Layout) {
798
823
799
824
void XCOFFObjectWriter::finalizeSectionInfo () {
800
825
for (auto *Section : Sections) {
801
- if (Section->Index == Section ::UninitializedIndex)
826
+ if (Section->Index == SectionEntry ::UninitializedIndex)
802
827
// Nothing to record for this Section.
803
828
continue ;
804
829
@@ -822,7 +847,7 @@ void XCOFFObjectWriter::finalizeSectionInfo() {
822
847
// Calculate the file offset to the relocation entries.
823
848
uint64_t RawPointer = RelocationEntryOffset;
824
849
for (auto Sec : Sections) {
825
- if (Sec->Index == Section ::UninitializedIndex || !Sec->RelocationCount )
850
+ if (Sec->Index == SectionEntry ::UninitializedIndex || !Sec->RelocationCount )
826
851
continue ;
827
852
828
853
Sec->FileOffsetToRelocations = RawPointer;
@@ -848,7 +873,7 @@ void XCOFFObjectWriter::assignAddressesAndIndices(const MCAsmLayout &Layout) {
848
873
Csect.Size = 0 ;
849
874
Csect.Address = 0 ;
850
875
Csect.SymbolTableIndex = SymbolTableIndex;
851
- SymbolIndexMap[Csect.MCCsect ->getQualNameSymbol ()] = Csect.SymbolTableIndex ;
876
+ SymbolIndexMap[Csect.MCSec ->getQualNameSymbol ()] = Csect.SymbolTableIndex ;
852
877
// 1 main and 1 auxiliary symbol table entry for each contained symbol.
853
878
SymbolTableIndex += 2 ;
854
879
}
@@ -889,7 +914,7 @@ void XCOFFObjectWriter::assignAddressesAndIndices(const MCAsmLayout &Layout) {
889
914
continue ;
890
915
891
916
for (auto &Csect : *Group) {
892
- const MCSectionXCOFF *MCSec = Csect.MCCsect ;
917
+ const MCSectionXCOFF *MCSec = Csect.MCSec ;
893
918
Csect.Address = alignTo (Address, MCSec->getAlignment ());
894
919
Csect.Size = Layout.getSectionAddressSize (MCSec);
895
920
Address = Csect.Address + Csect.Size ;
@@ -925,7 +950,7 @@ void XCOFFObjectWriter::assignAddressesAndIndices(const MCAsmLayout &Layout) {
925
950
uint64_t RawPointer = XCOFF::FileHeaderSize32 + auxiliaryHeaderSize () +
926
951
SectionCount * XCOFF::SectionHeaderSize32;
927
952
for (auto *Sec : Sections) {
928
- if (Sec->Index == Section ::UninitializedIndex || Sec->IsVirtual )
953
+ if (Sec->Index == SectionEntry ::UninitializedIndex || Sec->IsVirtual )
929
954
continue ;
930
955
931
956
Sec->FileOffsetToData = RawPointer;
0 commit comments