blob: c006eae0c04b179fcc6dd2777d5aace53266ced8 [file] [log] [blame]
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001//===- OutputSections.h -----------------------------------------*- C++ -*-===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef LLD_ELF_OUTPUT_SECTIONS_H
11#define LLD_ELF_OUTPUT_SECTIONS_H
12
Davide Italiano85121bb2015-09-25 03:56:11 +000013#include "Config.h"
Rafael Espindola5616adf2017-03-08 22:36:28 +000014#include "InputSection.h"
Rafael Espindola8c022ca2017-07-27 19:22:43 +000015#include "LinkerScript.h"
Simon Atanasyan41325112016-06-19 21:39:37 +000016#include "Relocations.h"
Davide Italiano85121bb2015-09-25 03:56:11 +000017
Rui Ueyama3f851702017-10-02 21:00:41 +000018#include "lld/Common/LLVM.h"
Rui Ueyamaa0752a52016-03-13 20:28:29 +000019#include "llvm/MC/StringTableBuilder.h"
20#include "llvm/Object/ELF.h"
Rafael Espindola5805c4f2015-09-21 21:38:08 +000021
22namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +000023namespace elf {
Rafael Espindola5805c4f2015-09-21 21:38:08 +000024
Rafael Espindola17cb7c02016-12-19 17:01:01 +000025struct PhdrEntry;
Rui Ueyamaf52496e2017-11-03 21:21:47 +000026class Symbol;
Rafael Espindola2deeb602016-07-21 20:18:30 +000027struct EhSectionPiece;
Rafael Espindola5c02b742017-03-06 21:17:18 +000028class EhInputSection;
Rafael Espindola774ea7d2017-02-23 16:49:07 +000029class InputSection;
Rafael Espindolab4c9b812017-02-23 02:28:28 +000030class InputSectionBase;
Rafael Espindola6119b862017-03-06 20:23:56 +000031class MergeInputSection;
Rafael Espindola24e6f362017-02-24 15:07:30 +000032class OutputSection;
Rui Ueyama709fb2bb12017-07-26 22:13:32 +000033template <class ELFT> class ObjFile;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +000034template <class ELFT> class SharedFile;
Rui Ueyama4076fa12017-02-26 23:35:34 +000035class SharedSymbol;
Peter Collingbournee9a9e0a2017-11-06 04:35:31 +000036class Defined;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000037
Rafael Espindola71675852015-09-22 00:16:19 +000038// This represents a section in an output file.
Rafael Espindola24e6f362017-02-24 15:07:30 +000039// It is composed of multiple InputSections.
Rafael Espindola71675852015-09-22 00:16:19 +000040// The writer creates multiple OutputSections and assign them unique,
Rafael Espindola5805c4f2015-09-21 21:38:08 +000041// non-overlapping file offsets and VAs.
Rafael Espindola8c022ca2017-07-27 19:22:43 +000042class OutputSection final : public BaseCommand, public SectionBase {
Rafael Espindola5805c4f2015-09-21 21:38:08 +000043public:
Rafael Espindola24e6f362017-02-24 15:07:30 +000044 OutputSection(StringRef Name, uint32_t Type, uint64_t Flags);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000045
Rafael Espindola5616adf2017-03-08 22:36:28 +000046 static bool classof(const SectionBase *S) {
47 return S->kind() == SectionBase::Output;
48 }
Rui Ueyama5908c2f2017-10-11 02:28:28 +000049
Rafael Espindola8c022ca2017-07-27 19:22:43 +000050 static bool classof(const BaseCommand *C);
Rafael Espindola5616adf2017-03-08 22:36:28 +000051
Rafael Espindola48798642018-01-25 19:02:08 +000052 uint64_t getLMA() const { return PtLoad ? Addr + PtLoad->LMAOffset : Addr; }
Rafael Espindolae08e78d2016-11-09 23:23:45 +000053 template <typename ELFT> void writeHeaderTo(typename ELFT::Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000054
Rui Ueyama2317d0d2015-10-15 20:55:22 +000055 unsigned SectionIndex;
Rafael Espindola52101412017-05-12 14:52:22 +000056 unsigned SortRank;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000057
Rafael Espindola0b113672016-07-27 14:10:56 +000058 uint32_t getPhdrFlags() const;
Rui Ueyama3b04d832016-07-14 05:46:24 +000059
George Rimar582ede82017-09-07 10:53:07 +000060 // Pointer to the PT_LOAD segment, which this section resides in. This field
61 // is used to correctly compute file offset of a section. When two sections
62 // share the same load segment, difference between their file offsets should
63 // be equal to difference between their virtual addresses. To compute some
64 // section offset we use the following formula: Off = Off_first + VA -
65 // VA_first, where Off_first and VA_first is file offset and VA of first
66 // section in PT_LOAD.
67 PhdrEntry *PtLoad = nullptr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000068
George Rimar990c9cb2017-06-07 09:20:35 +000069 // Pointer to a relocation section for this section. Usually nullptr because
70 // we consume relocations, but if --emit-relocs is specified (which is rare),
71 // it may have a non-null value.
72 OutputSection *RelocationSection = nullptr;
73
James Henderson8d0efdd2017-12-12 11:51:13 +000074 // Initially this field is the number of InputSections that have been added to
75 // the OutputSection so far. Later on, after a call to assignAddresses, it
76 // corresponds to the Elf_Shdr member.
Rafael Espindolae08e78d2016-11-09 23:23:45 +000077 uint64_t Size = 0;
James Henderson8d0efdd2017-12-12 11:51:13 +000078
79 // The following fields correspond to Elf_Shdr members.
Rafael Espindolae08e78d2016-11-09 23:23:45 +000080 uint64_t Offset = 0;
Rafael Espindolae08e78d2016-11-09 23:23:45 +000081 uint64_t Addr = 0;
Rafael Espindola04a2e342016-11-09 01:42:41 +000082 uint32_t ShName = 0;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000083
Rui Ueyama0e2bfb12017-10-07 00:43:31 +000084 void addSection(InputSection *IS);
Eugene Leviant84569e62016-11-29 08:05:44 +000085
86 // Location in the output buffer.
87 uint8_t *Loc = nullptr;
Rafael Espindola8c022ca2017-07-27 19:22:43 +000088
89 // The following members are normally only used in linker scripts.
90 MemoryRegion *MemRegion = nullptr;
Rafael Espindola567175f2018-01-25 01:36:36 +000091 MemoryRegion *LMARegion = nullptr;
Rafael Espindola8c022ca2017-07-27 19:22:43 +000092 Expr AddrExpr;
93 Expr AlignExpr;
94 Expr LMAExpr;
95 Expr SubalignExpr;
Rui Ueyama6b394ca2017-10-11 01:50:56 +000096 std::vector<BaseCommand *> SectionCommands;
Rafael Espindola8c022ca2017-07-27 19:22:43 +000097 std::vector<StringRef> Phdrs;
98 llvm::Optional<uint32_t> Filler;
99 ConstraintKind Constraint = ConstraintKind::NoConstraint;
100 std::string Location;
101 std::string MemoryRegionName;
George Rimar5d01a8b2018-01-12 09:07:35 +0000102 std::string LMARegionName;
Rafael Espindola8c022ca2017-07-27 19:22:43 +0000103 bool Noload = false;
104
105 template <class ELFT> void finalize();
106 template <class ELFT> void writeTo(uint8_t *Buf);
107 template <class ELFT> void maybeCompress();
Rafael Espindola8c022ca2017-07-27 19:22:43 +0000108
109 void sort(std::function<int(InputSectionBase *S)> Order);
110 void sortInitFini();
111 void sortCtorsDtors();
Rui Ueyama40bd97a2017-10-06 21:42:37 +0000112
113private:
114 // Used for implementation of --compress-debug-sections option.
Rafael Espindola10bcc1c2017-12-12 17:37:01 +0000115 std::vector<uint8_t> ZDebugHeader;
116 llvm::SmallVector<char, 1> CompressedData;
117
118 uint32_t getFiller();
119};
120
Rafael Espindola8c022ca2017-07-27 19:22:43 +0000121int getPriority(StringRef S);
122
Rui Ueyama9d1bacb12017-02-27 02:31:26 +0000123// All output sections that are handled by the linker specially are
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000124// globally accessible. Writer initializes them, so don't use them
125// until Writer is initialized.
Rui Ueyama9d1bacb12017-02-27 02:31:26 +0000126struct Out {
Rui Ueyamacfadbd92016-11-01 23:12:51 +0000127 static uint8_t First;
Rafael Espindola24e6f362017-02-24 15:07:30 +0000128 static OutputSection *Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000129 static uint8_t *OpdBuf;
Rafael Espindola17cb7c02016-12-19 17:01:01 +0000130 static PhdrEntry *TlsPhdr;
Rafael Espindola24e6f362017-02-24 15:07:30 +0000131 static OutputSection *DebugInfo;
132 static OutputSection *ElfHeader;
133 static OutputSection *ProgramHeaders;
134 static OutputSection *PreinitArray;
135 static OutputSection *InitArray;
136 static OutputSection *FiniArray;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000137};
Rui Ueyamad888d102015-10-09 19:34:55 +0000138
George Rimar67c60722017-07-18 11:55:35 +0000139} // namespace elf
140} // namespace lld
Rui Ueyama40bd97a2017-10-06 21:42:37 +0000141
Rafael Espindola63866282017-02-16 19:23:15 +0000142namespace lld {
143namespace elf {
George Rimar6892afa2016-07-12 09:49:43 +0000144
George Rimar78aa2702017-03-13 14:40:58 +0000145uint64_t getHeaderSize();
George Rimard6bcde32017-08-04 10:25:29 +0000146void sortByOrder(llvm::MutableArrayRef<InputSection *> In,
147 std::function<int(InputSectionBase *S)> Order);
Rafael Espindola0d4b6d52016-09-22 16:47:21 +0000148
Rafael Espindolaf51c8052017-06-13 23:26:31 +0000149extern std::vector<OutputSection *> OutputSections;
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000150} // namespace elf
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000151} // namespace lld
152
George Rimar6892afa2016-07-12 09:49:43 +0000153#endif