blob: 422652e5ef50cf80f3e12992c5256c8ae56afcf5 [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
10#include "llvm/MC/MCSectionELF.h"
Chris Lattner7b26fce2009-08-22 20:48:53 +000011#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerb9d1f5a2010-01-13 21:21:29 +000012#include "llvm/MC/MCContext.h"
Peter Collingbourne2f495b92013-04-17 21:18:16 +000013#include "llvm/MC/MCExpr.h"
Chris Lattnerb9d1f5a2010-01-13 21:21:29 +000014#include "llvm/MC/MCSymbol.h"
Rafael Espindolaaea49582011-01-23 04:28:49 +000015#include "llvm/Support/ELF.h"
Chris Lattnerb9d1f5a2010-01-13 21:21:29 +000016#include "llvm/Support/raw_ostream.h"
Rafael Espindolaaea49582011-01-23 04:28:49 +000017
Bruno Cardoso Lopes607cd3b2009-08-13 05:07:35 +000018using namespace llvm;
19
Chris Lattner5418dd52010-04-08 21:26:26 +000020MCSectionELF::~MCSectionELF() {} // anchor.
Bruno Cardoso Lopes607cd3b2009-08-13 05:07:35 +000021
Sanjay Patel352fb462014-10-09 21:23:39 +000022// Decides whether a '.section' directive
23// should be printed before the section name.
Benjamin Kramer94b993e2010-01-22 18:21:23 +000024bool MCSectionELF::ShouldOmitSectionDirective(StringRef Name,
Chris Lattnerb9d1f5a2010-01-13 21:21:29 +000025 const MCAsmInfo &MAI) const {
Jim Grosbachdc1e36e2012-05-11 01:41:30 +000026
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 {
Jim Grosbachdc1e36e2012-05-11 01:41:30 +000059
Benjamin Kramer94b993e2010-01-22 18:21:23 +000060 if (ShouldOmitSectionDirective(SectionName, MAI)) {
Peter Collingbourne2f495b92013-04-17 21:18:16 +000061 OS << '\t' << getSectionName();
Matt Arsenault8b643552015-06-09 00:31:39 +000062 if (Subsection) {
63 OS << '\t';
64 Subsection->print(OS, &MAI);
65 }
Peter Collingbourne2f495b92013-04-17 21:18:16 +000066 OS << '\n';
Bruno Cardoso Lopes607cd3b2009-08-13 05:07:35 +000067 return;
68 }
69
Rafael Espindolafdc88132013-11-13 14:01:59 +000070 OS << "\t.section\t";
71 printName(OS, getSectionName());
Joerg Sonnenberger852ab892011-03-03 22:31:08 +000072
Bruno Cardoso Lopes607cd3b2009-08-13 05:07:35 +000073 // Handle the weird solaris syntax if desired.
Jim Grosbachdc1e36e2012-05-11 01:41:30 +000074 if (MAI.usesSunStyleELFSectionSwitchSyntax() &&
Rafael Espindola0e7e34e2011-01-23 04:43:11 +000075 !(Flags & ELF::SHF_MERGE)) {
76 if (Flags & ELF::SHF_ALLOC)
Bruno Cardoso Lopes607cd3b2009-08-13 05:07:35 +000077 OS << ",#alloc";
Rafael Espindola0e7e34e2011-01-23 04:43:11 +000078 if (Flags & ELF::SHF_EXECINSTR)
Bruno Cardoso Lopes607cd3b2009-08-13 05:07:35 +000079 OS << ",#execinstr";
Rafael Espindola0e7e34e2011-01-23 04:43:11 +000080 if (Flags & ELF::SHF_WRITE)
Bruno Cardoso Lopes607cd3b2009-08-13 05:07:35 +000081 OS << ",#write";
Benjamin Kramerac511ca2013-09-15 19:53:20 +000082 if (Flags & ELF::SHF_EXCLUDE)
83 OS << ",#exclude";
Rafael Espindola0e7e34e2011-01-23 04:43:11 +000084 if (Flags & ELF::SHF_TLS)
Bruno Cardoso Lopes607cd3b2009-08-13 05:07:35 +000085 OS << ",#tls";
Chris Lattner5418dd52010-04-08 21:26:26 +000086 OS << '\n';
87 return;
88 }
Jim Grosbachdc1e36e2012-05-11 01:41:30 +000089
Chris Lattner5418dd52010-04-08 21:26:26 +000090 OS << ",\"";
Rafael Espindola0e7e34e2011-01-23 04:43:11 +000091 if (Flags & ELF::SHF_ALLOC)
Chris Lattner5418dd52010-04-08 21:26:26 +000092 OS << 'a';
Benjamin Kramerac511ca2013-09-15 19:53:20 +000093 if (Flags & ELF::SHF_EXCLUDE)
94 OS << 'e';
Rafael Espindola0e7e34e2011-01-23 04:43:11 +000095 if (Flags & ELF::SHF_EXECINSTR)
Chris Lattner5418dd52010-04-08 21:26:26 +000096 OS << 'x';
Rafael Espindola70d80152011-02-14 22:23:49 +000097 if (Flags & ELF::SHF_GROUP)
98 OS << 'G';
Rafael Espindola0e7e34e2011-01-23 04:43:11 +000099 if (Flags & ELF::SHF_WRITE)
Chris Lattner5418dd52010-04-08 21:26:26 +0000100 OS << 'w';
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000101 if (Flags & ELF::SHF_MERGE)
Chris Lattner5418dd52010-04-08 21:26:26 +0000102 OS << 'M';
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000103 if (Flags & ELF::SHF_STRINGS)
Chris Lattner5418dd52010-04-08 21:26:26 +0000104 OS << 'S';
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000105 if (Flags & ELF::SHF_TLS)
Chris Lattner5418dd52010-04-08 21:26:26 +0000106 OS << 'T';
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000107
Chris Lattner5418dd52010-04-08 21:26:26 +0000108 // If there are target-specific flags, print them.
Rafael Espindolae0eba3c2017-01-30 15:38:43 +0000109 Triple::ArchType Arch = T.getArch();
110 if (Arch == Triple::xcore) {
111 if (Flags & ELF::XCORE_SHF_CP_SECTION)
112 OS << 'c';
113 if (Flags & ELF::XCORE_SHF_DP_SECTION)
114 OS << 'd';
115 } else if (Arch == Triple::arm || Arch == Triple::armeb ||
116 Arch == Triple::thumb || Arch == Triple::thumbeb) {
117 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";
Rafael Espindola9bb44a52010-11-09 23:42:07 +0000145
146 if (EntrySize) {
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000147 assert(Flags & ELF::SHF_MERGE);
Rafael Espindola9bb44a52010-11-09 23:42:07 +0000148 OS << "," << EntrySize;
Bruno Cardoso Lopes607cd3b2009-08-13 05:07:35 +0000149 }
Rafael Espindola9bb44a52010-11-09 23:42:07 +0000150
Rafael Espindolafdc88132013-11-13 14:01:59 +0000151 if (Flags & ELF::SHF_GROUP) {
152 OS << ",";
153 printName(OS, Group->getName());
154 OS << ",comdat";
155 }
Rafael Espindola68fa2492015-02-17 20:48:01 +0000156
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000157 if (isUnique())
Rafael Espindola41e2b5c2015-04-06 16:34:41 +0000158 OS << ",unique," << UniqueID;
Rafael Espindola68fa2492015-02-17 20:48:01 +0000159
Bruno Cardoso Lopes607cd3b2009-08-13 05:07:35 +0000160 OS << '\n';
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000161
Matt Arsenault8b643552015-06-09 00:31:39 +0000162 if (Subsection) {
163 OS << "\t.subsection\t";
164 Subsection->print(OS, &MAI);
165 OS << '\n';
166 }
Bruno Cardoso Lopes607cd3b2009-08-13 05:07:35 +0000167}
168
Jan Wen Voung87f77b52010-10-04 17:32:41 +0000169bool MCSectionELF::UseCodeAlign() const {
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000170 return getFlags() & ELF::SHF_EXECINSTR;
Jan Wen Voung87f77b52010-10-04 17:32:41 +0000171}
172
Rafael Espindola7a2cd8b2010-11-17 20:03:54 +0000173bool MCSectionELF::isVirtualSection() const {
Rafael Espindolaaea49582011-01-23 04:28:49 +0000174 return getType() == ELF::SHT_NOBITS;
Rafael Espindola7a2cd8b2010-11-17 20:03:54 +0000175}