blob: b4448d79a2d5b5925253902ec1c02d3461713f52 [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
Bruno Cardoso Lopes607cd3b2009-08-13 05:07:35 +000030 // FIXME: Does .section .bss/.data/.text work everywhere??
Benjamin Kramer94b993e2010-01-22 18:21:23 +000031 if (Name == ".text" || Name == ".data" ||
32 (Name == ".bss" && !MAI.usesELFSectionDirectiveForBSS()))
Bruno Cardoso Lopes607cd3b2009-08-13 05:07:35 +000033 return true;
34
35 return false;
36}
37
Rafael Espindolafdc88132013-11-13 14:01:59 +000038static void printName(raw_ostream &OS, StringRef Name) {
39 if (Name.find_first_not_of("0123456789_."
40 "abcdefghijklmnopqrstuvwxyz"
41 "ABCDEFGHIJKLMNOPQRSTUVWXYZ") == Name.npos) {
42 OS << Name;
43 return;
44 }
45 OS << '"';
46 for (const char *B = Name.begin(), *E = Name.end(); B < E; ++B) {
47 if (*B == '"') // Unquoted "
48 OS << "\\\"";
49 else if (*B != '\\') // Neither " or backslash
50 OS << *B;
51 else if (B + 1 == E) // Trailing backslash
52 OS << "\\\\";
53 else {
54 OS << B[0] << B[1]; // Quoted character
55 ++B;
56 }
57 }
58 OS << '"';
59}
60
Chris Lattnere9a75a62009-08-22 21:43:10 +000061void MCSectionELF::PrintSwitchToSection(const MCAsmInfo &MAI,
Peter Collingbourne2f495b92013-04-17 21:18:16 +000062 raw_ostream &OS,
63 const MCExpr *Subsection) const {
Jim Grosbachdc1e36e2012-05-11 01:41:30 +000064
Benjamin Kramer94b993e2010-01-22 18:21:23 +000065 if (ShouldOmitSectionDirective(SectionName, MAI)) {
Peter Collingbourne2f495b92013-04-17 21:18:16 +000066 OS << '\t' << getSectionName();
Matt Arsenault8b643552015-06-09 00:31:39 +000067 if (Subsection) {
68 OS << '\t';
69 Subsection->print(OS, &MAI);
70 }
Peter Collingbourne2f495b92013-04-17 21:18:16 +000071 OS << '\n';
Bruno Cardoso Lopes607cd3b2009-08-13 05:07:35 +000072 return;
73 }
74
Rafael Espindolafdc88132013-11-13 14:01:59 +000075 OS << "\t.section\t";
76 printName(OS, getSectionName());
Joerg Sonnenberger852ab892011-03-03 22:31:08 +000077
Bruno Cardoso Lopes607cd3b2009-08-13 05:07:35 +000078 // Handle the weird solaris syntax if desired.
Jim Grosbachdc1e36e2012-05-11 01:41:30 +000079 if (MAI.usesSunStyleELFSectionSwitchSyntax() &&
Rafael Espindola0e7e34e2011-01-23 04:43:11 +000080 !(Flags & ELF::SHF_MERGE)) {
81 if (Flags & ELF::SHF_ALLOC)
Bruno Cardoso Lopes607cd3b2009-08-13 05:07:35 +000082 OS << ",#alloc";
Rafael Espindola0e7e34e2011-01-23 04:43:11 +000083 if (Flags & ELF::SHF_EXECINSTR)
Bruno Cardoso Lopes607cd3b2009-08-13 05:07:35 +000084 OS << ",#execinstr";
Rafael Espindola0e7e34e2011-01-23 04:43:11 +000085 if (Flags & ELF::SHF_WRITE)
Bruno Cardoso Lopes607cd3b2009-08-13 05:07:35 +000086 OS << ",#write";
Benjamin Kramerac511ca2013-09-15 19:53:20 +000087 if (Flags & ELF::SHF_EXCLUDE)
88 OS << ",#exclude";
Rafael Espindola0e7e34e2011-01-23 04:43:11 +000089 if (Flags & ELF::SHF_TLS)
Bruno Cardoso Lopes607cd3b2009-08-13 05:07:35 +000090 OS << ",#tls";
Chris Lattner5418dd52010-04-08 21:26:26 +000091 OS << '\n';
92 return;
93 }
Jim Grosbachdc1e36e2012-05-11 01:41:30 +000094
Chris Lattner5418dd52010-04-08 21:26:26 +000095 OS << ",\"";
Rafael Espindola0e7e34e2011-01-23 04:43:11 +000096 if (Flags & ELF::SHF_ALLOC)
Chris Lattner5418dd52010-04-08 21:26:26 +000097 OS << 'a';
Benjamin Kramerac511ca2013-09-15 19:53:20 +000098 if (Flags & ELF::SHF_EXCLUDE)
99 OS << 'e';
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000100 if (Flags & ELF::SHF_EXECINSTR)
Chris Lattner5418dd52010-04-08 21:26:26 +0000101 OS << 'x';
Rafael Espindola70d80152011-02-14 22:23:49 +0000102 if (Flags & ELF::SHF_GROUP)
103 OS << 'G';
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000104 if (Flags & ELF::SHF_WRITE)
Chris Lattner5418dd52010-04-08 21:26:26 +0000105 OS << 'w';
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000106 if (Flags & ELF::SHF_MERGE)
Chris Lattner5418dd52010-04-08 21:26:26 +0000107 OS << 'M';
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000108 if (Flags & ELF::SHF_STRINGS)
Chris Lattner5418dd52010-04-08 21:26:26 +0000109 OS << 'S';
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000110 if (Flags & ELF::SHF_TLS)
Chris Lattner5418dd52010-04-08 21:26:26 +0000111 OS << 'T';
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000112
Chris Lattner5418dd52010-04-08 21:26:26 +0000113 // If there are target-specific flags, print them.
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000114 if (Flags & ELF::XCORE_SHF_CP_SECTION)
Chris Lattner5418dd52010-04-08 21:26:26 +0000115 OS << 'c';
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000116 if (Flags & ELF::XCORE_SHF_DP_SECTION)
Chris Lattner5418dd52010-04-08 21:26:26 +0000117 OS << 'd';
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000118
Chris Lattner5418dd52010-04-08 21:26:26 +0000119 OS << '"';
Bruno Cardoso Lopes607cd3b2009-08-13 05:07:35 +0000120
Rafael Espindola9bb44a52010-11-09 23:42:07 +0000121 OS << ',';
122
123 // If comment string is '@', e.g. as on ARM - use '%' instead
124 if (MAI.getCommentString()[0] == '@')
125 OS << '%';
126 else
127 OS << '@';
128
Rafael Espindolaaea49582011-01-23 04:28:49 +0000129 if (Type == ELF::SHT_INIT_ARRAY)
Rafael Espindola9bb44a52010-11-09 23:42:07 +0000130 OS << "init_array";
Rafael Espindolaaea49582011-01-23 04:28:49 +0000131 else if (Type == ELF::SHT_FINI_ARRAY)
Rafael Espindola9bb44a52010-11-09 23:42:07 +0000132 OS << "fini_array";
Rafael Espindolaaea49582011-01-23 04:28:49 +0000133 else if (Type == ELF::SHT_PREINIT_ARRAY)
Rafael Espindola9bb44a52010-11-09 23:42:07 +0000134 OS << "preinit_array";
Rafael Espindolaaea49582011-01-23 04:28:49 +0000135 else if (Type == ELF::SHT_NOBITS)
Rafael Espindola9bb44a52010-11-09 23:42:07 +0000136 OS << "nobits";
Rafael Espindolaaea49582011-01-23 04:28:49 +0000137 else if (Type == ELF::SHT_NOTE)
Rafael Espindola9ae2d052010-12-26 21:30:59 +0000138 OS << "note";
Rafael Espindolaaea49582011-01-23 04:28:49 +0000139 else if (Type == ELF::SHT_PROGBITS)
Rafael Espindola9bb44a52010-11-09 23:42:07 +0000140 OS << "progbits";
141
142 if (EntrySize) {
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000143 assert(Flags & ELF::SHF_MERGE);
Rafael Espindola9bb44a52010-11-09 23:42:07 +0000144 OS << "," << EntrySize;
Bruno Cardoso Lopes607cd3b2009-08-13 05:07:35 +0000145 }
Rafael Espindola9bb44a52010-11-09 23:42:07 +0000146
Rafael Espindolafdc88132013-11-13 14:01:59 +0000147 if (Flags & ELF::SHF_GROUP) {
148 OS << ",";
149 printName(OS, Group->getName());
150 OS << ",comdat";
151 }
Rafael Espindola68fa2492015-02-17 20:48:01 +0000152
Rafael Espindola8ca44f02015-04-04 18:02:01 +0000153 if (isUnique())
Rafael Espindola41e2b5c2015-04-06 16:34:41 +0000154 OS << ",unique," << UniqueID;
Rafael Espindola68fa2492015-02-17 20:48:01 +0000155
Bruno Cardoso Lopes607cd3b2009-08-13 05:07:35 +0000156 OS << '\n';
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000157
Matt Arsenault8b643552015-06-09 00:31:39 +0000158 if (Subsection) {
159 OS << "\t.subsection\t";
160 Subsection->print(OS, &MAI);
161 OS << '\n';
162 }
Bruno Cardoso Lopes607cd3b2009-08-13 05:07:35 +0000163}
164
Jan Wen Voung87f77b52010-10-04 17:32:41 +0000165bool MCSectionELF::UseCodeAlign() const {
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000166 return getFlags() & ELF::SHF_EXECINSTR;
Jan Wen Voung87f77b52010-10-04 17:32:41 +0000167}
168
Rafael Espindola7a2cd8b2010-11-17 20:03:54 +0000169bool MCSectionELF::isVirtualSection() const {
Rafael Espindolaaea49582011-01-23 04:28:49 +0000170 return getType() == ELF::SHT_NOBITS;
Rafael Espindola7a2cd8b2010-11-17 20:03:54 +0000171}