blob: 7ee1694ebbf71dd8f09be6386683a6110f91fea2 [file] [log] [blame]
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +00001//===- lib/MC/MCSectionELF.cpp - ELF Code Section Representation ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Chandler Carruthe3e43d92017-06-06 11:49:48 +000010#include "llvm/MC/MCSectionELF.h"
Eugene Zelenko7211c532017-02-14 00:33:36 +000011#include "llvm/ADT/Triple.h"
Zachary Turner19ca2b02017-06-07 03:48:56 +000012#include "llvm/BinaryFormat/ELF.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000013#include "llvm/MC/MCAsmInfo.h"
Peter Collingbournedf39be62013-04-17 21:18:16 +000014#include "llvm/MC/MCExpr.h"
Simon Atanasyand1a4b1d2017-03-10 08:22:13 +000015#include "llvm/Support/ErrorHandling.h"
Chris Lattner4d2419d2010-01-13 21:21:29 +000016#include "llvm/Support/raw_ostream.h"
Eugene Zelenko7211c532017-02-14 00:33:36 +000017#include <cassert>
Rafael Espindolac85dca62011-01-23 04:28:49 +000018
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000019using namespace llvm;
20
Eugene Zelenko7211c532017-02-14 00:33:36 +000021MCSectionELF::~MCSectionELF() = default; // anchor.
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000022
Sanjay Patelc160a9a2014-10-09 21:23:39 +000023// Decides whether a '.section' directive
24// should be printed before the section name.
Benjamin Kramera46918d2010-01-22 18:21:23 +000025bool MCSectionELF::ShouldOmitSectionDirective(StringRef Name,
Chris Lattner4d2419d2010-01-13 21:21:29 +000026 const MCAsmInfo &MAI) const {
Rafael Espindola903f4a22015-04-04 18:02:01 +000027 if (isUnique())
Rafael Espindolae0a25412015-02-17 20:48:01 +000028 return false;
29
Tom Stellard68f9d1c2015-09-25 21:41:14 +000030 return MAI.shouldOmitSectionDirective(Name);
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000031}
32
Rafael Espindolade9a1a22013-11-13 14:01:59 +000033static void printName(raw_ostream &OS, StringRef Name) {
34 if (Name.find_first_not_of("0123456789_."
35 "abcdefghijklmnopqrstuvwxyz"
36 "ABCDEFGHIJKLMNOPQRSTUVWXYZ") == Name.npos) {
37 OS << Name;
38 return;
39 }
40 OS << '"';
41 for (const char *B = Name.begin(), *E = Name.end(); B < E; ++B) {
42 if (*B == '"') // Unquoted "
43 OS << "\\\"";
44 else if (*B != '\\') // Neither " or backslash
45 OS << *B;
46 else if (B + 1 == E) // Trailing backslash
47 OS << "\\\\";
48 else {
49 OS << B[0] << B[1]; // Quoted character
50 ++B;
51 }
52 }
53 OS << '"';
54}
55
Rafael Espindolad5702b72017-01-30 15:38:43 +000056void MCSectionELF::PrintSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
Peter Collingbournedf39be62013-04-17 21:18:16 +000057 raw_ostream &OS,
58 const MCExpr *Subsection) const {
Benjamin Kramera46918d2010-01-22 18:21:23 +000059 if (ShouldOmitSectionDirective(SectionName, MAI)) {
Peter Collingbournedf39be62013-04-17 21:18:16 +000060 OS << '\t' << getSectionName();
Matt Arsenaultd99ce2f2015-06-09 00:31:39 +000061 if (Subsection) {
62 OS << '\t';
63 Subsection->print(OS, &MAI);
64 }
Peter Collingbournedf39be62013-04-17 21:18:16 +000065 OS << '\n';
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000066 return;
67 }
68
Rafael Espindolade9a1a22013-11-13 14:01:59 +000069 OS << "\t.section\t";
70 printName(OS, getSectionName());
Joerg Sonnenbergerea83b132011-03-03 22:31:08 +000071
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000072 // Handle the weird solaris syntax if desired.
Jim Grosbach2684d9e2012-05-11 01:41:30 +000073 if (MAI.usesSunStyleELFSectionSwitchSyntax() &&
Rafael Espindola1c130262011-01-23 04:43:11 +000074 !(Flags & ELF::SHF_MERGE)) {
75 if (Flags & ELF::SHF_ALLOC)
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000076 OS << ",#alloc";
Rafael Espindola1c130262011-01-23 04:43:11 +000077 if (Flags & ELF::SHF_EXECINSTR)
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000078 OS << ",#execinstr";
Rafael Espindola1c130262011-01-23 04:43:11 +000079 if (Flags & ELF::SHF_WRITE)
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000080 OS << ",#write";
Benjamin Kramer766f2532013-09-15 19:53:20 +000081 if (Flags & ELF::SHF_EXCLUDE)
82 OS << ",#exclude";
Rafael Espindola1c130262011-01-23 04:43:11 +000083 if (Flags & ELF::SHF_TLS)
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000084 OS << ",#tls";
Chris Lattner74aae472010-04-08 21:26:26 +000085 OS << '\n';
86 return;
87 }
Jim Grosbach2684d9e2012-05-11 01:41:30 +000088
Chris Lattner74aae472010-04-08 21:26:26 +000089 OS << ",\"";
Rafael Espindola1c130262011-01-23 04:43:11 +000090 if (Flags & ELF::SHF_ALLOC)
Chris Lattner74aae472010-04-08 21:26:26 +000091 OS << 'a';
Benjamin Kramer766f2532013-09-15 19:53:20 +000092 if (Flags & ELF::SHF_EXCLUDE)
93 OS << 'e';
Rafael Espindola1c130262011-01-23 04:43:11 +000094 if (Flags & ELF::SHF_EXECINSTR)
Chris Lattner74aae472010-04-08 21:26:26 +000095 OS << 'x';
Rafael Espindola5d618ef2011-02-14 22:23:49 +000096 if (Flags & ELF::SHF_GROUP)
97 OS << 'G';
Rafael Espindola1c130262011-01-23 04:43:11 +000098 if (Flags & ELF::SHF_WRITE)
Chris Lattner74aae472010-04-08 21:26:26 +000099 OS << 'w';
Rafael Espindola1c130262011-01-23 04:43:11 +0000100 if (Flags & ELF::SHF_MERGE)
Chris Lattner74aae472010-04-08 21:26:26 +0000101 OS << 'M';
Rafael Espindola1c130262011-01-23 04:43:11 +0000102 if (Flags & ELF::SHF_STRINGS)
Chris Lattner74aae472010-04-08 21:26:26 +0000103 OS << 'S';
Rafael Espindola1c130262011-01-23 04:43:11 +0000104 if (Flags & ELF::SHF_TLS)
Chris Lattner74aae472010-04-08 21:26:26 +0000105 OS << 'T';
Evgeniy Stepanovc67a9ef2017-03-14 19:28:51 +0000106 if (Flags & ELF::SHF_LINK_ORDER)
Evgeniy Stepanov8719ab12017-04-04 22:35:08 +0000107 OS << 'o';
Jim Grosbach2684d9e2012-05-11 01:41:30 +0000108
Chris Lattner74aae472010-04-08 21:26:26 +0000109 // If there are target-specific flags, print them.
Rafael Espindolad5702b72017-01-30 15:38:43 +0000110 Triple::ArchType Arch = T.getArch();
111 if (Arch == Triple::xcore) {
112 if (Flags & ELF::XCORE_SHF_CP_SECTION)
113 OS << 'c';
114 if (Flags & ELF::XCORE_SHF_DP_SECTION)
115 OS << 'd';
Florian Hahnace970e2017-08-12 17:40:18 +0000116 } else if (T.isARM() || T.isThumb()) {
Rafael Espindolad5702b72017-01-30 15:38:43 +0000117 if (Flags & ELF::SHF_ARM_PURECODE)
118 OS << 'y';
Krzysztof Parzyszek39279752018-11-09 14:17:27 +0000119 } else if (Arch == Triple::hexagon) {
120 if (Flags & ELF::SHF_HEX_GPREL)
121 OS << 's';
Rafael Espindolad5702b72017-01-30 15:38:43 +0000122 }
Jim Grosbach2684d9e2012-05-11 01:41:30 +0000123
Chris Lattner74aae472010-04-08 21:26:26 +0000124 OS << '"';
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000125
Rafael Espindola34be3962010-11-09 23:42:07 +0000126 OS << ',';
127
128 // If comment string is '@', e.g. as on ARM - use '%' instead
129 if (MAI.getCommentString()[0] == '@')
130 OS << '%';
131 else
132 OS << '@';
133
Rafael Espindolac85dca62011-01-23 04:28:49 +0000134 if (Type == ELF::SHT_INIT_ARRAY)
Rafael Espindola34be3962010-11-09 23:42:07 +0000135 OS << "init_array";
Rafael Espindolac85dca62011-01-23 04:28:49 +0000136 else if (Type == ELF::SHT_FINI_ARRAY)
Rafael Espindola34be3962010-11-09 23:42:07 +0000137 OS << "fini_array";
Rafael Espindolac85dca62011-01-23 04:28:49 +0000138 else if (Type == ELF::SHT_PREINIT_ARRAY)
Rafael Espindola34be3962010-11-09 23:42:07 +0000139 OS << "preinit_array";
Rafael Espindolac85dca62011-01-23 04:28:49 +0000140 else if (Type == ELF::SHT_NOBITS)
Rafael Espindola34be3962010-11-09 23:42:07 +0000141 OS << "nobits";
Rafael Espindolac85dca62011-01-23 04:28:49 +0000142 else if (Type == ELF::SHT_NOTE)
Rafael Espindola98976612010-12-26 21:30:59 +0000143 OS << "note";
Rafael Espindolac85dca62011-01-23 04:28:49 +0000144 else if (Type == ELF::SHT_PROGBITS)
Rafael Espindola34be3962010-11-09 23:42:07 +0000145 OS << "progbits";
Rafael Espindola012d9592015-11-06 15:30:45 +0000146 else if (Type == ELF::SHT_X86_64_UNWIND)
147 OS << "unwind";
Simon Atanasyanb479d372017-03-10 08:22:20 +0000148 else if (Type == ELF::SHT_MIPS_DWARF)
149 // Print hex value of the flag while we do not have
150 // any standard symbolic representation of the flag.
151 OS << "0x7000001e";
Peter Collingbourne6bd5b272017-06-14 18:52:12 +0000152 else if (Type == ELF::SHT_LLVM_ODRTAB)
153 OS << "llvm_odrtab";
Saleem Abdulrasoole7676fe2018-01-30 16:29:29 +0000154 else if (Type == ELF::SHT_LLVM_LINKER_OPTIONS)
155 OS << "llvm_linker_options";
Michael J. Spencer56500c72018-06-02 16:33:01 +0000156 else if (Type == ELF::SHT_LLVM_CALL_GRAPH_PROFILE)
157 OS << "llvm_call_graph_profile";
Simon Atanasyand1a4b1d2017-03-10 08:22:13 +0000158 else
159 report_fatal_error("unsupported type 0x" + Twine::utohexstr(Type) +
160 " for section " + getSectionName());
Rafael Espindola34be3962010-11-09 23:42:07 +0000161
162 if (EntrySize) {
Rafael Espindola1c130262011-01-23 04:43:11 +0000163 assert(Flags & ELF::SHF_MERGE);
Rafael Espindola34be3962010-11-09 23:42:07 +0000164 OS << "," << EntrySize;
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000165 }
Rafael Espindola34be3962010-11-09 23:42:07 +0000166
Rafael Espindolade9a1a22013-11-13 14:01:59 +0000167 if (Flags & ELF::SHF_GROUP) {
168 OS << ",";
169 printName(OS, Group->getName());
170 OS << ",comdat";
171 }
Rafael Espindolae0a25412015-02-17 20:48:01 +0000172
Evgeniy Stepanovc67a9ef2017-03-14 19:28:51 +0000173 if (Flags & ELF::SHF_LINK_ORDER) {
174 assert(AssociatedSymbol);
175 OS << ",";
176 printName(OS, AssociatedSymbol->getName());
177 }
178
Rafael Espindola903f4a22015-04-04 18:02:01 +0000179 if (isUnique())
Rafael Espindola6ba6e552015-04-06 16:34:41 +0000180 OS << ",unique," << UniqueID;
Rafael Espindolae0a25412015-02-17 20:48:01 +0000181
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000182 OS << '\n';
Peter Collingbournedf39be62013-04-17 21:18:16 +0000183
Matt Arsenaultd99ce2f2015-06-09 00:31:39 +0000184 if (Subsection) {
185 OS << "\t.subsection\t";
186 Subsection->print(OS, &MAI);
187 OS << '\n';
188 }
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000189}
190
Jan Wen Voung083cf152010-10-04 17:32:41 +0000191bool MCSectionELF::UseCodeAlign() const {
Rafael Espindola1c130262011-01-23 04:43:11 +0000192 return getFlags() & ELF::SHF_EXECINSTR;
Jan Wen Voung083cf152010-10-04 17:32:41 +0000193}
194
Rafael Espindolaf2dc4aa2010-11-17 20:03:54 +0000195bool MCSectionELF::isVirtualSection() const {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000196 return getType() == ELF::SHT_NOBITS;
Rafael Espindolaf2dc4aa2010-11-17 20:03:54 +0000197}