blob: 36cdb4216559ee3f3bd4606cd135ecd95478b77f [file] [log] [blame]
Bruno Cardoso Lopes607cd3b2009-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 Carruth6bda14b2017-06-06 11:49:48 +000010#include "llvm/MC/MCSectionELF.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000011#include "llvm/ADT/Triple.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000012#include "llvm/BinaryFormat/ELF.h"
Chris Lattner7b26fce2009-08-22 20:48:53 +000013#include "llvm/MC/MCAsmInfo.h"
Peter Collingbourne2f495b92013-04-17 21:18:16 +000014#include "llvm/MC/MCExpr.h"
Simon Atanasyan29532242017-03-10 08:22:13 +000015#include "llvm/Support/ErrorHandling.h"
Chris Lattnerb9d1f5a2010-01-13 21:21:29 +000016#include "llvm/Support/raw_ostream.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000017#include <cassert>
Rafael Espindolaaea49582011-01-23 04:28:49 +000018
Bruno Cardoso Lopes607cd3b2009-08-13 05:07:35 +000019using namespace llvm;
20
Eugene Zelenkod96089b2017-02-14 00:33:36 +000021MCSectionELF::~MCSectionELF() = default; // anchor.
Bruno Cardoso Lopes607cd3b2009-08-13 05:07:35 +000022
Sanjay Patel352fb462014-10-09 21:23:39 +000023// Decides whether a '.section' directive
24// should be printed before the section name.
Benjamin Kramer94b993e2010-01-22 18:21:23 +000025bool MCSectionELF::ShouldOmitSectionDirective(StringRef Name,
Chris Lattnerb9d1f5a2010-01-13 21:21:29 +000026 const MCAsmInfo &MAI) const {
Rafael Espindola8ca44f02015-04-04 18:02:01 +000027 if (isUnique())
Rafael Espindola68fa2492015-02-17 20:48:01 +000028 return false;
29
Tom Stellard8e025762015-09-25 21:41:14 +000030 return MAI.shouldOmitSectionDirective(Name);
Bruno Cardoso Lopes607cd3b2009-08-13 05:07:35 +000031}
32
Rafael Espindolafdc88132013-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 Espindolae0eba3c2017-01-30 15:38:43 +000056void MCSectionELF::PrintSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
Peter Collingbourne2f495b92013-04-17 21:18:16 +000057 raw_ostream &OS,
58 const MCExpr *Subsection) const {
Benjamin Kramer94b993e2010-01-22 18:21:23 +000059 if (ShouldOmitSectionDirective(SectionName, MAI)) {
Peter Collingbourne2f495b92013-04-17 21:18:16 +000060 OS << '\t' << getSectionName();
Matt Arsenault8b643552015-06-09 00:31:39 +000061 if (Subsection) {
62 OS << '\t';
63 Subsection->print(OS, &MAI);
64 }
Peter Collingbourne2f495b92013-04-17 21:18:16 +000065 OS << '\n';
Bruno Cardoso Lopes607cd3b2009-08-13 05:07:35 +000066 return;
67 }
68
Rafael Espindolafdc88132013-11-13 14:01:59 +000069 OS << "\t.section\t";
70 printName(OS, getSectionName());
Joerg Sonnenberger852ab892011-03-03 22:31:08 +000071
Bruno Cardoso Lopes607cd3b2009-08-13 05:07:35 +000072 // Handle the weird solaris syntax if desired.
Jim Grosbachdc1e36e2012-05-11 01:41:30 +000073 if (MAI.usesSunStyleELFSectionSwitchSyntax() &&
Rafael Espindola0e7e34e2011-01-23 04:43:11 +000074 !(Flags & ELF::SHF_MERGE)) {
75 if (Flags & ELF::SHF_ALLOC)
Bruno Cardoso Lopes607cd3b2009-08-13 05:07:35 +000076 OS << ",#alloc";
Rafael Espindola0e7e34e2011-01-23 04:43:11 +000077 if (Flags & ELF::SHF_EXECINSTR)
Bruno Cardoso Lopes607cd3b2009-08-13 05:07:35 +000078 OS << ",#execinstr";
Rafael Espindola0e7e34e2011-01-23 04:43:11 +000079 if (Flags & ELF::SHF_WRITE)
Bruno Cardoso Lopes607cd3b2009-08-13 05:07:35 +000080 OS << ",#write";
Benjamin Kramerac511ca2013-09-15 19:53:20 +000081 if (Flags & ELF::SHF_EXCLUDE)
82 OS << ",#exclude";
Rafael Espindola0e7e34e2011-01-23 04:43:11 +000083 if (Flags & ELF::SHF_TLS)
Bruno Cardoso Lopes607cd3b2009-08-13 05:07:35 +000084 OS << ",#tls";
Chris Lattner5418dd52010-04-08 21:26:26 +000085 OS << '\n';
86 return;
87 }
Jim Grosbachdc1e36e2012-05-11 01:41:30 +000088
Chris Lattner5418dd52010-04-08 21:26:26 +000089 OS << ",\"";
Rafael Espindola0e7e34e2011-01-23 04:43:11 +000090 if (Flags & ELF::SHF_ALLOC)
Chris Lattner5418dd52010-04-08 21:26:26 +000091 OS << 'a';
Benjamin Kramerac511ca2013-09-15 19:53:20 +000092 if (Flags & ELF::SHF_EXCLUDE)
93 OS << 'e';
Rafael Espindola0e7e34e2011-01-23 04:43:11 +000094 if (Flags & ELF::SHF_EXECINSTR)
Chris Lattner5418dd52010-04-08 21:26:26 +000095 OS << 'x';
Rafael Espindola70d80152011-02-14 22:23:49 +000096 if (Flags & ELF::SHF_GROUP)
97 OS << 'G';
Rafael Espindola0e7e34e2011-01-23 04:43:11 +000098 if (Flags & ELF::SHF_WRITE)
Chris Lattner5418dd52010-04-08 21:26:26 +000099 OS << 'w';
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000100 if (Flags & ELF::SHF_MERGE)
Chris Lattner5418dd52010-04-08 21:26:26 +0000101 OS << 'M';
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000102 if (Flags & ELF::SHF_STRINGS)
Chris Lattner5418dd52010-04-08 21:26:26 +0000103 OS << 'S';
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000104 if (Flags & ELF::SHF_TLS)
Chris Lattner5418dd52010-04-08 21:26:26 +0000105 OS << 'T';
Evgeniy Stepanov43dcf4d2017-03-14 19:28:51 +0000106 if (Flags & ELF::SHF_LINK_ORDER)
Evgeniy Stepanov12de7b22017-04-04 22:35:08 +0000107 OS << 'o';
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000108
Chris Lattner5418dd52010-04-08 21:26:26 +0000109 // If there are target-specific flags, print them.
Rafael Espindolae0eba3c2017-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 Hahna5ba4ee2017-08-12 17:40:18 +0000116 } else if (T.isARM() || T.isThumb()) {
Rafael Espindolae0eba3c2017-01-30 15:38:43 +0000117 if (Flags & ELF::SHF_ARM_PURECODE)
118 OS << 'y';
119 }
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000120
Chris Lattner5418dd52010-04-08 21:26:26 +0000121 OS << '"';
Bruno Cardoso Lopes607cd3b2009-08-13 05:07:35 +0000122
Rafael Espindola9bb44a52010-11-09 23:42:07 +0000123 OS << ',';
124
125 // If comment string is '@', e.g. as on ARM - use '%' instead
126 if (MAI.getCommentString()[0] == '@')
127 OS << '%';
128 else
129 OS << '@';
130
Rafael Espindolaaea49582011-01-23 04:28:49 +0000131 if (Type == ELF::SHT_INIT_ARRAY)
Rafael Espindola9bb44a52010-11-09 23:42:07 +0000132 OS << "init_array";
Rafael Espindolaaea49582011-01-23 04:28:49 +0000133 else if (Type == ELF::SHT_FINI_ARRAY)
Rafael Espindola9bb44a52010-11-09 23:42:07 +0000134 OS << "fini_array";
Rafael Espindolaaea49582011-01-23 04:28:49 +0000135 else if (Type == ELF::SHT_PREINIT_ARRAY)
Rafael Espindola9bb44a52010-11-09 23:42:07 +0000136 OS << "preinit_array";
Rafael Espindolaaea49582011-01-23 04:28:49 +0000137 else if (Type == ELF::SHT_NOBITS)
Rafael Espindola9bb44a52010-11-09 23:42:07 +0000138 OS << "nobits";
Rafael Espindolaaea49582011-01-23 04:28:49 +0000139 else if (Type == ELF::SHT_NOTE)
Rafael Espindola9ae2d052010-12-26 21:30:59 +0000140 OS << "note";
Rafael Espindolaaea49582011-01-23 04:28:49 +0000141 else if (Type == ELF::SHT_PROGBITS)
Rafael Espindola9bb44a52010-11-09 23:42:07 +0000142 OS << "progbits";
Rafael Espindola889d7bb2015-11-06 15:30:45 +0000143 else if (Type == ELF::SHT_X86_64_UNWIND)
144 OS << "unwind";
Simon Atanasyanec8dfb12017-03-10 08:22:20 +0000145 else if (Type == ELF::SHT_MIPS_DWARF)
146 // Print hex value of the flag while we do not have
147 // any standard symbolic representation of the flag.
148 OS << "0x7000001e";
Peter Collingbournef0e26e72017-06-14 18:52:12 +0000149 else if (Type == ELF::SHT_LLVM_ODRTAB)
150 OS << "llvm_odrtab";
Saleem Abdulrasoolb36fbbc2018-01-30 16:29:29 +0000151 else if (Type == ELF::SHT_LLVM_LINKER_OPTIONS)
152 OS << "llvm_linker_options";
Simon Atanasyan29532242017-03-10 08:22:13 +0000153 else
154 report_fatal_error("unsupported type 0x" + Twine::utohexstr(Type) +
155 " for section " + getSectionName());
Rafael Espindola9bb44a52010-11-09 23:42:07 +0000156
157 if (EntrySize) {
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000158 assert(Flags & ELF::SHF_MERGE);
Rafael Espindola9bb44a52010-11-09 23:42:07 +0000159 OS << "," << EntrySize;
Bruno Cardoso Lopes607cd3b2009-08-13 05:07:35 +0000160 }
Rafael Espindola9bb44a52010-11-09 23:42:07 +0000161
Rafael Espindolafdc88132013-11-13 14:01:59 +0000162 if (Flags & ELF::SHF_GROUP) {
163 OS << ",";
164 printName(OS, Group->getName());
165 OS << ",comdat";
166 }
Rafael Espindola68fa2492015-02-17 20:48:01 +0000167
Evgeniy Stepanov43dcf4d2017-03-14 19:28:51 +0000168 if (Flags & ELF::SHF_LINK_ORDER) {
169 assert(AssociatedSymbol);
170 OS << ",";
171 printName(OS, AssociatedSymbol->getName());
172 }
173
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000174 if (isUnique())
Rafael Espindola41e2b5c2015-04-06 16:34:41 +0000175 OS << ",unique," << UniqueID;
Rafael Espindola68fa2492015-02-17 20:48:01 +0000176
Bruno Cardoso Lopes607cd3b2009-08-13 05:07:35 +0000177 OS << '\n';
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000178
Matt Arsenault8b643552015-06-09 00:31:39 +0000179 if (Subsection) {
180 OS << "\t.subsection\t";
181 Subsection->print(OS, &MAI);
182 OS << '\n';
183 }
Bruno Cardoso Lopes607cd3b2009-08-13 05:07:35 +0000184}
185
Jan Wen Voung87f77b52010-10-04 17:32:41 +0000186bool MCSectionELF::UseCodeAlign() const {
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000187 return getFlags() & ELF::SHF_EXECINSTR;
Jan Wen Voung87f77b52010-10-04 17:32:41 +0000188}
189
Rafael Espindola7a2cd8b2010-11-17 20:03:54 +0000190bool MCSectionELF::isVirtualSection() const {
Rafael Espindolaaea49582011-01-23 04:28:49 +0000191 return getType() == ELF::SHT_NOBITS;
Rafael Espindola7a2cd8b2010-11-17 20:03:54 +0000192}