blob: c73fafe151b09034a4d55a1ac631f01be2ca382f [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
Eugene Zelenkod96089b2017-02-14 00:33:36 +000010#include "llvm/ADT/Triple.h"
Chris Lattner7b26fce2009-08-22 20:48:53 +000011#include "llvm/MC/MCAsmInfo.h"
Peter Collingbourne2f495b92013-04-17 21:18:16 +000012#include "llvm/MC/MCExpr.h"
Eugene Zelenkod96089b2017-02-14 00:33:36 +000013#include "llvm/MC/MCSectionELF.h"
Rafael Espindolaaea49582011-01-23 04:28:49 +000014#include "llvm/Support/ELF.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';
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000106
Chris Lattner5418dd52010-04-08 21:26:26 +0000107 // If there are target-specific flags, print them.
Rafael Espindolae0eba3c2017-01-30 15:38:43 +0000108 Triple::ArchType Arch = T.getArch();
109 if (Arch == Triple::xcore) {
110 if (Flags & ELF::XCORE_SHF_CP_SECTION)
111 OS << 'c';
112 if (Flags & ELF::XCORE_SHF_DP_SECTION)
113 OS << 'd';
114 } else if (Arch == Triple::arm || Arch == Triple::armeb ||
115 Arch == Triple::thumb || Arch == Triple::thumbeb) {
116 if (Flags & ELF::SHF_ARM_PURECODE)
117 OS << 'y';
118 }
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000119
Chris Lattner5418dd52010-04-08 21:26:26 +0000120 OS << '"';
Bruno Cardoso Lopes607cd3b2009-08-13 05:07:35 +0000121
Rafael Espindola9bb44a52010-11-09 23:42:07 +0000122 OS << ',';
123
124 // If comment string is '@', e.g. as on ARM - use '%' instead
125 if (MAI.getCommentString()[0] == '@')
126 OS << '%';
127 else
128 OS << '@';
129
Rafael Espindolaaea49582011-01-23 04:28:49 +0000130 if (Type == ELF::SHT_INIT_ARRAY)
Rafael Espindola9bb44a52010-11-09 23:42:07 +0000131 OS << "init_array";
Rafael Espindolaaea49582011-01-23 04:28:49 +0000132 else if (Type == ELF::SHT_FINI_ARRAY)
Rafael Espindola9bb44a52010-11-09 23:42:07 +0000133 OS << "fini_array";
Rafael Espindolaaea49582011-01-23 04:28:49 +0000134 else if (Type == ELF::SHT_PREINIT_ARRAY)
Rafael Espindola9bb44a52010-11-09 23:42:07 +0000135 OS << "preinit_array";
Rafael Espindolaaea49582011-01-23 04:28:49 +0000136 else if (Type == ELF::SHT_NOBITS)
Rafael Espindola9bb44a52010-11-09 23:42:07 +0000137 OS << "nobits";
Rafael Espindolaaea49582011-01-23 04:28:49 +0000138 else if (Type == ELF::SHT_NOTE)
Rafael Espindola9ae2d052010-12-26 21:30:59 +0000139 OS << "note";
Rafael Espindolaaea49582011-01-23 04:28:49 +0000140 else if (Type == ELF::SHT_PROGBITS)
Rafael Espindola9bb44a52010-11-09 23:42:07 +0000141 OS << "progbits";
Rafael Espindola889d7bb2015-11-06 15:30:45 +0000142 else if (Type == ELF::SHT_X86_64_UNWIND)
143 OS << "unwind";
Simon Atanasyan29532242017-03-10 08:22:13 +0000144 else
145 report_fatal_error("unsupported type 0x" + Twine::utohexstr(Type) +
146 " for section " + getSectionName());
Rafael Espindola9bb44a52010-11-09 23:42:07 +0000147
148 if (EntrySize) {
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000149 assert(Flags & ELF::SHF_MERGE);
Rafael Espindola9bb44a52010-11-09 23:42:07 +0000150 OS << "," << EntrySize;
Bruno Cardoso Lopes607cd3b2009-08-13 05:07:35 +0000151 }
Rafael Espindola9bb44a52010-11-09 23:42:07 +0000152
Rafael Espindolafdc88132013-11-13 14:01:59 +0000153 if (Flags & ELF::SHF_GROUP) {
154 OS << ",";
155 printName(OS, Group->getName());
156 OS << ",comdat";
157 }
Rafael Espindola68fa2492015-02-17 20:48:01 +0000158
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000159 if (isUnique())
Rafael Espindola41e2b5c2015-04-06 16:34:41 +0000160 OS << ",unique," << UniqueID;
Rafael Espindola68fa2492015-02-17 20:48:01 +0000161
Bruno Cardoso Lopes607cd3b2009-08-13 05:07:35 +0000162 OS << '\n';
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000163
Matt Arsenault8b643552015-06-09 00:31:39 +0000164 if (Subsection) {
165 OS << "\t.subsection\t";
166 Subsection->print(OS, &MAI);
167 OS << '\n';
168 }
Bruno Cardoso Lopes607cd3b2009-08-13 05:07:35 +0000169}
170
Jan Wen Voung87f77b52010-10-04 17:32:41 +0000171bool MCSectionELF::UseCodeAlign() const {
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000172 return getFlags() & ELF::SHF_EXECINSTR;
Jan Wen Voung87f77b52010-10-04 17:32:41 +0000173}
174
Rafael Espindola7a2cd8b2010-11-17 20:03:54 +0000175bool MCSectionELF::isVirtualSection() const {
Rafael Espindolaaea49582011-01-23 04:28:49 +0000176 return getType() == ELF::SHT_NOBITS;
Rafael Espindola7a2cd8b2010-11-17 20:03:54 +0000177}