1//===- LDFileFormat.h -----------------------------------------------------===//
2//
3//                     The MCLinker Project
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9#ifndef MCLD_ELF_FILE_FORMAT_H
10#define MCLD_ELF_FILE_FORMAT_H
11#include <mcld/LD/LDFileFormat.h>
12#include <mcld/LD/LDSection.h>
13
14namespace mcld {
15
16class ObjectBuilder;
17
18/** \class ELFFileFormat
19 *  \brief ELFFileFormat describes the common file formats in ELF.
20 *  LDFileFormats control the formats of the output file.
21 *
22 *  @ref "Object Files," Ch. 4, in System V Application Binary Interface,
23 *  Fourth Edition.
24 *
25 *  @ref "Object Format," Ch. 10, in ISO/IEC 23360 Part 1:2010(E), Linux
26 *  Standard Base Core Specification 4.1.
27 */
28class ELFFileFormat : public LDFileFormat
29{
30private:
31  /// initObjectFormat - initialize sections that are dependent on object
32  /// formats. (executable, shared objects or relocatable objects).
33  virtual void
34  initObjectFormat(ObjectBuilder& pBuilder, unsigned int pBitClass) = 0;
35
36public:
37  ELFFileFormat();
38
39  void initStdSections(ObjectBuilder& pBuilder, unsigned int pBitClass);
40
41  // -----  capacity  ----- //
42  /// @ref Special Sections, Ch. 4.17, System V ABI, 4th edition.
43  bool hasNULLSection() const
44  { return (NULL != f_pNULLSection) && (0 != f_pNULLSection->size()); }
45
46  bool hasGOT() const
47  { return (NULL != f_pGOT) && (0 != f_pGOT->size()); }
48
49  bool hasPLT() const
50  { return (NULL != f_pPLT) && (0 != f_pPLT->size()); }
51
52  bool hasRelDyn() const
53  { return (NULL != f_pRelDyn) && (0 != f_pRelDyn->size()); }
54
55  bool hasRelPlt() const
56  { return (NULL != f_pRelPlt) && (0 != f_pRelPlt->size()); }
57
58  bool hasRelaDyn() const
59  { return (NULL != f_pRelaDyn) && (0 != f_pRelaDyn->size()); }
60
61  bool hasRelaPlt() const
62  { return (NULL != f_pRelaPlt) && (0 != f_pRelaPlt->size()); }
63
64  /// @ref 10.3.1.1, ISO/IEC 23360, Part 1:2010(E), p. 21.
65  bool hasComment() const
66  { return (NULL != f_pComment) && (0 != f_pComment->size()); }
67
68  bool hasData1() const
69  { return (NULL != f_pData1) && (0 != f_pData1->size()); }
70
71  bool hasDebug() const
72  { return (NULL != f_pDebug) && (0 != f_pDebug->size()); }
73
74  bool hasDynamic() const
75  { return (NULL != f_pDynamic) && (0 != f_pDynamic->size()); }
76
77  bool hasDynStrTab() const
78  { return (NULL != f_pDynStrTab) && (0 != f_pDynStrTab->size()); }
79
80  bool hasDynSymTab() const
81  { return (NULL != f_pDynSymTab) && (0 != f_pDynSymTab->size()); }
82
83  bool hasFini() const
84  { return (NULL != f_pFini) && (0 != f_pFini->size()); }
85
86  bool hasFiniArray() const
87  { return (NULL != f_pFiniArray) && (0 != f_pFiniArray->size()); }
88
89  bool hasHashTab() const
90  { return (NULL != f_pHashTab) && (0 != f_pHashTab->size()); }
91
92  bool hasInit() const
93  { return (NULL != f_pInit) && (0 != f_pInit->size()); }
94
95  bool hasInitArray() const
96  { return (NULL != f_pInitArray) && (0 != f_pInitArray->size()); }
97
98  bool hasInterp() const
99  { return (NULL != f_pInterp) && (0 != f_pInterp->size()); }
100
101  bool hasLine() const
102  { return (NULL != f_pLine) && (0 != f_pLine->size()); }
103
104  bool hasNote() const
105  { return (NULL != f_pNote) && (0 != f_pNote->size()); }
106
107  bool hasPreInitArray() const
108  { return (NULL != f_pPreInitArray) && (0 != f_pPreInitArray->size()); }
109
110  bool hasROData1() const
111  { return (NULL != f_pROData1) && (0 != f_pROData1->size()); }
112
113  bool hasShStrTab() const
114  { return (NULL != f_pShStrTab) && (0 != f_pShStrTab->size()); }
115
116  bool hasStrTab() const
117  { return (NULL != f_pStrTab) && (0 != f_pStrTab->size()); }
118
119  bool hasSymTab() const
120  { return (NULL != f_pSymTab) && (0 != f_pSymTab->size()); }
121
122  bool hasTBSS() const
123  { return (NULL != f_pTBSS) && (0 != f_pTBSS->size()); }
124
125  bool hasTData() const
126  { return (NULL != f_pTData) && (0 != f_pTData->size()); }
127
128  /// @ref 10.3.1.2, ISO/IEC 23360, Part 1:2010(E), p. 24.
129  bool hasCtors() const
130  { return (NULL != f_pCtors) && (0 != f_pCtors->size()); }
131
132  bool hasDataRelRo() const
133  { return (NULL != f_pDataRelRo) && (0 != f_pDataRelRo->size()); }
134
135  bool hasDtors() const
136  { return (NULL != f_pDtors) && (0 != f_pDtors->size()); }
137
138  bool hasEhFrame() const
139  { return (NULL != f_pEhFrame) && (0 != f_pEhFrame->size()); }
140
141  bool hasEhFrameHdr() const
142  { return (NULL != f_pEhFrameHdr) && (0 != f_pEhFrameHdr->size()); }
143
144  bool hasGCCExceptTable() const
145  { return (NULL != f_pGCCExceptTable) && (0 != f_pGCCExceptTable->size()); }
146
147  bool hasGNUVersion() const
148  { return (NULL != f_pGNUVersion) && (0 != f_pGNUVersion->size()); }
149
150  bool hasGNUVersionD() const
151  { return (NULL != f_pGNUVersionD) && (0 != f_pGNUVersionD->size()); }
152
153  bool hasGNUVersionR() const
154  { return (NULL != f_pGNUVersionR) && (0 != f_pGNUVersionR->size()); }
155
156  bool hasGOTPLT() const
157  { return (NULL != f_pGOTPLT) && (0 != f_pGOTPLT->size()); }
158
159  bool hasJCR() const
160  { return (NULL != f_pJCR) && (0 != f_pJCR->size()); }
161
162  bool hasNoteABITag() const
163  { return (NULL != f_pNoteABITag) && (0 != f_pNoteABITag->size()); }
164
165  bool hasStab() const
166  { return (NULL != f_pStab) && (0 != f_pStab->size()); }
167
168  bool hasStabStr() const
169  { return (NULL != f_pStabStr) && (0 != f_pStabStr->size()); }
170
171  bool hasStack() const
172  { return (NULL != f_pStack) && (0 != f_pStack->size()); }
173
174  bool hasStackNote() const
175  { return (NULL != f_pStackNote); }
176
177  bool hasDataRelRoLocal() const
178  { return (NULL != f_pDataRelRoLocal) && (0 != f_pDataRelRoLocal->size()); }
179
180  bool hasGNUHashTab() const
181  { return (NULL != f_pGNUHashTab) && (0 != f_pGNUHashTab->size()); }
182
183  // -----  access functions  ----- //
184  /// @ref Special Sections, Ch. 4.17, System V ABI, 4th edition.
185  LDSection& getNULLSection() {
186    assert(NULL != f_pNULLSection);
187    return *f_pNULLSection;
188  }
189
190  const LDSection& getNULLSection() const {
191    assert(NULL != f_pNULLSection);
192    return *f_pNULLSection;
193  }
194
195  LDSection& getGOT() {
196    assert(NULL != f_pGOT);
197    return *f_pGOT;
198  }
199
200  const LDSection& getGOT() const {
201    assert(NULL != f_pGOT);
202    return *f_pGOT;
203  }
204
205  LDSection& getPLT() {
206    assert(NULL != f_pPLT);
207    return *f_pPLT;
208  }
209
210  const LDSection& getPLT() const {
211    assert(NULL != f_pPLT);
212    return *f_pPLT;
213  }
214
215  LDSection& getRelDyn() {
216    assert(NULL != f_pRelDyn);
217    return *f_pRelDyn;
218  }
219
220  const LDSection& getRelDyn() const {
221    assert(NULL != f_pRelDyn);
222    return *f_pRelDyn;
223  }
224
225  LDSection& getRelPlt() {
226    assert(NULL != f_pRelPlt);
227    return *f_pRelPlt;
228  }
229
230  const LDSection& getRelPlt() const {
231    assert(NULL != f_pRelPlt);
232    return *f_pRelPlt;
233  }
234
235  LDSection& getRelaDyn() {
236    assert(NULL != f_pRelaDyn);
237    return *f_pRelaDyn;
238  }
239
240  const LDSection& getRelaDyn() const {
241    assert(NULL != f_pRelaDyn);
242    return *f_pRelaDyn;
243  }
244
245  LDSection& getRelaPlt() {
246    assert(NULL != f_pRelaPlt);
247    return *f_pRelaPlt;
248  }
249
250  const LDSection& getRelaPlt() const {
251    assert(NULL != f_pRelaPlt);
252    return *f_pRelaPlt;
253  }
254
255  LDSection& getComment() {
256    assert(NULL != f_pComment);
257    return *f_pComment;
258  }
259
260  /// @ref 10.3.1.1, ISO/IEC 23360, Part 1:2010(E), p. 21.
261  const LDSection& getComment() const {
262    assert(NULL != f_pComment);
263    return *f_pComment;
264  }
265
266  LDSection& getData1() {
267    assert(NULL != f_pData1);
268    return *f_pData1;
269  }
270
271  const LDSection& getData1() const {
272    assert(NULL != f_pData1);
273    return *f_pData1;
274  }
275
276  LDSection& getDebug() {
277    assert(NULL != f_pDebug);
278    return *f_pDebug;
279  }
280
281  const LDSection& getDebug() const {
282    assert(NULL != f_pDebug);
283    return *f_pDebug;
284  }
285
286  LDSection& getDynamic() {
287    assert(NULL != f_pDynamic);
288    return *f_pDynamic;
289  }
290
291  const LDSection& getDynamic() const {
292    assert(NULL != f_pDynamic);
293    return *f_pDynamic;
294  }
295
296  LDSection& getDynStrTab() {
297    assert(NULL != f_pDynStrTab);
298    return *f_pDynStrTab;
299  }
300
301  const LDSection& getDynStrTab() const {
302    assert(NULL != f_pDynStrTab);
303    return *f_pDynStrTab;
304  }
305
306  LDSection& getDynSymTab() {
307    assert(NULL != f_pDynSymTab);
308    return *f_pDynSymTab;
309  }
310
311  const LDSection& getDynSymTab() const {
312    assert(NULL != f_pDynSymTab);
313    return *f_pDynSymTab;
314  }
315
316  LDSection& getFini() {
317    assert(NULL != f_pFini);
318    return *f_pFini;
319  }
320
321  const LDSection& getFini() const {
322    assert(NULL != f_pFini);
323    return *f_pFini;
324  }
325
326  LDSection& getFiniArray() {
327    assert(NULL != f_pFiniArray);
328    return *f_pFiniArray;
329  }
330
331  const LDSection& getFiniArray() const {
332    assert(NULL != f_pFiniArray);
333    return *f_pFiniArray;
334  }
335
336  LDSection& getHashTab() {
337    assert(NULL != f_pHashTab);
338    return *f_pHashTab;
339  }
340
341  const LDSection& getHashTab() const {
342    assert(NULL != f_pHashTab);
343    return *f_pHashTab;
344  }
345
346  LDSection& getInit() {
347    assert(NULL != f_pInit);
348    return *f_pInit;
349  }
350
351  const LDSection& getInit() const {
352    assert(NULL != f_pInit);
353    return *f_pInit;
354  }
355
356  LDSection& getInitArray() {
357    assert(NULL != f_pInitArray);
358    return *f_pInitArray;
359  }
360
361  const LDSection& getInitArray() const {
362    assert(NULL != f_pInitArray);
363    return *f_pInitArray;
364  }
365
366  LDSection& getInterp() {
367    assert(NULL != f_pInterp);
368    return *f_pInterp;
369  }
370
371  const LDSection& getInterp() const {
372    assert(NULL != f_pInterp);
373    return *f_pInterp;
374  }
375
376  LDSection& getLine() {
377    assert(NULL != f_pLine);
378    return *f_pLine;
379  }
380
381  const LDSection& getLine() const {
382    assert(NULL != f_pLine);
383    return *f_pLine;
384  }
385
386  LDSection& getNote() {
387    assert(NULL != f_pNote);
388    return *f_pNote;
389  }
390
391  const LDSection& getNote() const {
392    assert(NULL != f_pNote);
393    return *f_pNote;
394  }
395
396  LDSection& getPreInitArray() {
397    assert(NULL != f_pPreInitArray);
398    return *f_pPreInitArray;
399  }
400
401  const LDSection& getPreInitArray() const {
402    assert(NULL != f_pPreInitArray);
403    return *f_pPreInitArray;
404  }
405
406  LDSection& getROData1() {
407    assert(NULL != f_pROData1);
408    return *f_pROData1;
409  }
410
411  const LDSection& getROData1() const {
412    assert(NULL != f_pROData1);
413    return *f_pROData1;
414  }
415
416  LDSection& getShStrTab() {
417    assert(NULL != f_pShStrTab);
418    return *f_pShStrTab;
419  }
420
421  const LDSection& getShStrTab() const {
422    assert(NULL != f_pShStrTab);
423    return *f_pShStrTab;
424  }
425
426  LDSection& getStrTab() {
427    assert(NULL != f_pStrTab);
428    return *f_pStrTab;
429  }
430
431  const LDSection& getStrTab() const {
432    assert(NULL != f_pStrTab);
433    return *f_pStrTab;
434  }
435
436  LDSection& getSymTab() {
437    assert(NULL != f_pSymTab);
438    return *f_pSymTab;
439  }
440
441  const LDSection& getSymTab() const {
442    assert(NULL != f_pSymTab);
443    return *f_pSymTab;
444  }
445
446  LDSection& getTBSS() {
447    assert(NULL != f_pTBSS);
448    return *f_pTBSS;
449  }
450
451  const LDSection& getTBSS() const {
452    assert(NULL != f_pTBSS);
453    return *f_pTBSS;
454  }
455
456  LDSection& getTData() {
457    assert(NULL != f_pTData);
458    return *f_pTData;
459  }
460
461  const LDSection& getTData() const {
462    assert(NULL != f_pTData);
463    return *f_pTData;
464  }
465
466  /// @ref 10.3.1.2, ISO/IEC 23360, Part 1:2010(E), p. 24.
467  LDSection& getCtors() {
468    assert(NULL != f_pCtors);
469    return *f_pCtors;
470  }
471
472  const LDSection& getCtors() const {
473    assert(NULL != f_pCtors);
474    return *f_pCtors;
475  }
476
477  LDSection& getDataRelRo() {
478    assert(NULL != f_pDataRelRo);
479    return *f_pDataRelRo;
480  }
481
482  const LDSection& getDataRelRo() const {
483    assert(NULL != f_pDataRelRo);
484    return *f_pDataRelRo;
485  }
486
487  LDSection& getDtors() {
488    assert(NULL != f_pDtors);
489    return *f_pDtors;
490  }
491
492  const LDSection& getDtors() const {
493    assert(NULL != f_pDtors);
494    return *f_pDtors;
495  }
496
497  LDSection& getEhFrame() {
498    assert(NULL != f_pEhFrame);
499    return *f_pEhFrame;
500  }
501
502  const LDSection& getEhFrame() const {
503    assert(NULL != f_pEhFrame);
504    return *f_pEhFrame;
505  }
506
507  LDSection& getEhFrameHdr() {
508    assert(NULL != f_pEhFrameHdr);
509    return *f_pEhFrameHdr;
510  }
511
512  const LDSection& getEhFrameHdr() const {
513    assert(NULL != f_pEhFrameHdr);
514    return *f_pEhFrameHdr;
515  }
516
517  LDSection& getGCCExceptTable() {
518    assert(NULL != f_pGCCExceptTable);
519    return *f_pGCCExceptTable;
520  }
521
522  const LDSection& getGCCExceptTable() const {
523    assert(NULL != f_pGCCExceptTable);
524    return *f_pGCCExceptTable;
525  }
526
527  LDSection& getGNUVersion() {
528    assert(NULL != f_pGNUVersion);
529    return *f_pGNUVersion;
530  }
531
532  const LDSection& getGNUVersion() const {
533    assert(NULL != f_pGNUVersion);
534    return *f_pGNUVersion;
535  }
536
537  LDSection& getGNUVersionD() {
538    assert(NULL != f_pGNUVersionD);
539    return *f_pGNUVersionD;
540  }
541
542  const LDSection& getGNUVersionD() const {
543    assert(NULL != f_pGNUVersionD);
544    return *f_pGNUVersionD;
545  }
546
547  LDSection& getGNUVersionR() {
548    assert(NULL != f_pGNUVersionR);
549    return *f_pGNUVersionR;
550  }
551
552  const LDSection& getGNUVersionR() const {
553    assert(NULL != f_pGNUVersionR);
554    return *f_pGNUVersionR;
555  }
556
557  LDSection& getGOTPLT() {
558    assert(NULL != f_pGOTPLT);
559    return *f_pGOTPLT;
560  }
561
562  const LDSection& getGOTPLT() const {
563    assert(NULL != f_pGOTPLT);
564    return *f_pGOTPLT;
565  }
566
567  LDSection& getJCR() {
568    assert(NULL != f_pJCR);
569    return *f_pJCR;
570  }
571
572  const LDSection& getJCR() const {
573    assert(NULL != f_pJCR);
574    return *f_pJCR;
575  }
576
577  LDSection& getNoteABITag() {
578    assert(NULL != f_pNoteABITag);
579    return *f_pNoteABITag;
580  }
581
582  const LDSection& getNoteABITag() const {
583    assert(NULL != f_pNoteABITag);
584    return *f_pNoteABITag;
585  }
586
587  LDSection& getStab() {
588    assert(NULL != f_pStab);
589    return *f_pStab;
590  }
591
592  const LDSection& getStab() const {
593    assert(NULL != f_pStab);
594    return *f_pStab;
595  }
596
597  LDSection& getStabStr() {
598    assert(NULL != f_pStabStr);
599    return *f_pStabStr;
600  }
601
602  const LDSection& getStabStr() const {
603    assert(NULL != f_pStabStr);
604    return *f_pStabStr;
605  }
606
607  LDSection& getStack() {
608    assert(NULL != f_pStack);
609    return *f_pStack;
610  }
611
612  const LDSection& getStack() const {
613    assert(NULL != f_pStack);
614    return *f_pStack;
615  }
616
617  LDSection& getStackNote() {
618    assert(NULL != f_pStackNote);
619    return *f_pStackNote;
620  }
621
622  const LDSection& getStackNote() const {
623    assert(NULL != f_pStackNote);
624    return *f_pStackNote;
625  }
626
627  LDSection& getDataRelRoLocal() {
628    assert(NULL != f_pDataRelRoLocal);
629    return *f_pDataRelRoLocal;
630  }
631
632  const LDSection& getDataRelRoLocal() const {
633    assert(NULL != f_pDataRelRoLocal);
634    return *f_pDataRelRoLocal;
635  }
636
637  LDSection& getGNUHashTab() {
638    assert(NULL != f_pGNUHashTab);
639    return *f_pGNUHashTab;
640  }
641
642  const LDSection& getGNUHashTab() const {
643    assert(NULL != f_pGNUHashTab);
644    return *f_pGNUHashTab;
645  }
646
647protected:
648  //         variable name         :  ELF
649  /// @ref Special Sections, Ch. 4.17, System V ABI, 4th edition.
650  LDSection* f_pNULLSection;
651  LDSection* f_pGOT;               // .got
652  LDSection* f_pPLT;               // .plt
653  LDSection* f_pRelDyn;            // .rel.dyn
654  LDSection* f_pRelPlt;            // .rel.plt
655  LDSection* f_pRelaDyn;           // .rela.dyn
656  LDSection* f_pRelaPlt;           // .rela.plt
657
658  /// @ref 10.3.1.1, ISO/IEC 23360, Part 1:2010(E), p. 21.
659  LDSection* f_pComment;           // .comment
660  LDSection* f_pData1;             // .data1
661  LDSection* f_pDebug;             // .debug
662  LDSection* f_pDynamic;           // .dynamic
663  LDSection* f_pDynStrTab;         // .dynstr
664  LDSection* f_pDynSymTab;         // .dynsym
665  LDSection* f_pFini;              // .fini
666  LDSection* f_pFiniArray;         // .fini_array
667  LDSection* f_pHashTab;           // .hash
668  LDSection* f_pInit;              // .init
669  LDSection* f_pInitArray;         // .init_array
670  LDSection* f_pInterp;            // .interp
671  LDSection* f_pLine;              // .line
672  LDSection* f_pNote;              // .note
673  LDSection* f_pPreInitArray;      // .preinit_array
674  LDSection* f_pROData1;           // .rodata1
675  LDSection* f_pShStrTab;          // .shstrtab
676  LDSection* f_pStrTab;            // .strtab
677  LDSection* f_pSymTab;            // .symtab
678  LDSection* f_pTBSS;              // .tbss
679  LDSection* f_pTData;             // .tdata
680
681  /// @ref 10.3.1.2, ISO/IEC 23360, Part 1:2010(E), p. 24.
682  LDSection* f_pCtors;             // .ctors
683  LDSection* f_pDataRelRo;         // .data.rel.ro
684  LDSection* f_pDtors;             // .dtors
685  LDSection* f_pEhFrame;           // .eh_frame
686  LDSection* f_pEhFrameHdr;        // .eh_frame_hdr
687  LDSection* f_pGCCExceptTable;    // .gcc_except_table
688  LDSection* f_pGNUVersion;        // .gnu.version
689  LDSection* f_pGNUVersionD;       // .gnu.version_d
690  LDSection* f_pGNUVersionR;       // .gnu.version_r
691  LDSection* f_pGOTPLT;            // .got.plt
692  LDSection* f_pJCR;               // .jcr
693  LDSection* f_pNoteABITag;        // .note.ABI-tag
694  LDSection* f_pStab;              // .stab
695  LDSection* f_pStabStr;           // .stabstr
696
697  /// practical
698  LDSection* f_pStack;             // .stack
699  LDSection* f_pStackNote;         // .note.GNU-stack
700  LDSection* f_pDataRelRoLocal;    // .data.rel.ro.local
701  LDSection* f_pGNUHashTab;        // .gnu.hash
702};
703
704} // namespace of mcld
705
706#endif
707
708