blob: 09eb3e7829582daf1b1749b492a9f87c8c430144 [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
22// ShouldOmitSectionDirective - 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
Bruno Cardoso Lopes607cd3b2009-08-13 05:07:35 +000027 // FIXME: Does .section .bss/.data/.text work everywhere??
Benjamin Kramer94b993e2010-01-22 18:21:23 +000028 if (Name == ".text" || Name == ".data" ||
29 (Name == ".bss" && !MAI.usesELFSectionDirectiveForBSS()))
Bruno Cardoso Lopes607cd3b2009-08-13 05:07:35 +000030 return true;
31
32 return false;
33}
34
Rafael Espindolafdc88132013-11-13 14:01:59 +000035static void printName(raw_ostream &OS, StringRef Name) {
36 if (Name.find_first_not_of("0123456789_."
37 "abcdefghijklmnopqrstuvwxyz"
38 "ABCDEFGHIJKLMNOPQRSTUVWXYZ") == Name.npos) {
39 OS << Name;
40 return;
41 }
42 OS << '"';
43 for (const char *B = Name.begin(), *E = Name.end(); B < E; ++B) {
44 if (*B == '"') // Unquoted "
45 OS << "\\\"";
46 else if (*B != '\\') // Neither " or backslash
47 OS << *B;
48 else if (B + 1 == E) // Trailing backslash
49 OS << "\\\\";
50 else {
51 OS << B[0] << B[1]; // Quoted character
52 ++B;
53 }
54 }
55 OS << '"';
56}
57
Chris Lattnere9a75a62009-08-22 21:43:10 +000058void MCSectionELF::PrintSwitchToSection(const MCAsmInfo &MAI,
Peter Collingbourne2f495b92013-04-17 21:18:16 +000059 raw_ostream &OS,
60 const MCExpr *Subsection) const {
Jim Grosbachdc1e36e2012-05-11 01:41:30 +000061
Benjamin Kramer94b993e2010-01-22 18:21:23 +000062 if (ShouldOmitSectionDirective(SectionName, MAI)) {
Peter Collingbourne2f495b92013-04-17 21:18:16 +000063 OS << '\t' << getSectionName();
64 if (Subsection)
65 OS << '\t' << *Subsection;
66 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 Espindola0e7e34e2011-01-23 04:43:11 +0000109 if (Flags & ELF::XCORE_SHF_CP_SECTION)
Chris Lattner5418dd52010-04-08 21:26:26 +0000110 OS << 'c';
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000111 if (Flags & ELF::XCORE_SHF_DP_SECTION)
Chris Lattner5418dd52010-04-08 21:26:26 +0000112 OS << 'd';
Jim Grosbachdc1e36e2012-05-11 01:41:30 +0000113
Chris Lattner5418dd52010-04-08 21:26:26 +0000114 OS << '"';
Bruno Cardoso Lopes607cd3b2009-08-13 05:07:35 +0000115
Rafael Espindola9bb44a52010-11-09 23:42:07 +0000116 OS << ',';
117
118 // If comment string is '@', e.g. as on ARM - use '%' instead
119 if (MAI.getCommentString()[0] == '@')
120 OS << '%';
121 else
122 OS << '@';
123
Rafael Espindolaaea49582011-01-23 04:28:49 +0000124 if (Type == ELF::SHT_INIT_ARRAY)
Rafael Espindola9bb44a52010-11-09 23:42:07 +0000125 OS << "init_array";
Rafael Espindolaaea49582011-01-23 04:28:49 +0000126 else if (Type == ELF::SHT_FINI_ARRAY)
Rafael Espindola9bb44a52010-11-09 23:42:07 +0000127 OS << "fini_array";
Rafael Espindolaaea49582011-01-23 04:28:49 +0000128 else if (Type == ELF::SHT_PREINIT_ARRAY)
Rafael Espindola9bb44a52010-11-09 23:42:07 +0000129 OS << "preinit_array";
Rafael Espindolaaea49582011-01-23 04:28:49 +0000130 else if (Type == ELF::SHT_NOBITS)
Rafael Espindola9bb44a52010-11-09 23:42:07 +0000131 OS << "nobits";
Rafael Espindolaaea49582011-01-23 04:28:49 +0000132 else if (Type == ELF::SHT_NOTE)
Rafael Espindola9ae2d052010-12-26 21:30:59 +0000133 OS << "note";
Rafael Espindolaaea49582011-01-23 04:28:49 +0000134 else if (Type == ELF::SHT_PROGBITS)
Rafael Espindola9bb44a52010-11-09 23:42:07 +0000135 OS << "progbits";
136
137 if (EntrySize) {
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000138 assert(Flags & ELF::SHF_MERGE);
Rafael Espindola9bb44a52010-11-09 23:42:07 +0000139 OS << "," << EntrySize;
Bruno Cardoso Lopes607cd3b2009-08-13 05:07:35 +0000140 }
Rafael Espindola9bb44a52010-11-09 23:42:07 +0000141
Rafael Espindolafdc88132013-11-13 14:01:59 +0000142 if (Flags & ELF::SHF_GROUP) {
143 OS << ",";
144 printName(OS, Group->getName());
145 OS << ",comdat";
146 }
Bruno Cardoso Lopes607cd3b2009-08-13 05:07:35 +0000147 OS << '\n';
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000148
149 if (Subsection)
150 OS << "\t.subsection\t" << *Subsection << '\n';
Bruno Cardoso Lopes607cd3b2009-08-13 05:07:35 +0000151}
152
Jan Wen Voung87f77b52010-10-04 17:32:41 +0000153bool MCSectionELF::UseCodeAlign() const {
Rafael Espindola0e7e34e2011-01-23 04:43:11 +0000154 return getFlags() & ELF::SHF_EXECINSTR;
Jan Wen Voung87f77b52010-10-04 17:32:41 +0000155}
156
Rafael Espindola7a2cd8b2010-11-17 20:03:54 +0000157bool MCSectionELF::isVirtualSection() const {
Rafael Espindolaaea49582011-01-23 04:28:49 +0000158 return getType() == ELF::SHT_NOBITS;
Rafael Espindola7a2cd8b2010-11-17 20:03:54 +0000159}
160
Jan Wen Voungefbdbe52010-09-30 05:59:22 +0000161unsigned MCSectionELF::DetermineEntrySize(SectionKind Kind) {
162 if (Kind.isMergeable1ByteCString()) return 1;
163 if (Kind.isMergeable2ByteCString()) return 2;
164 if (Kind.isMergeable4ByteCString()) return 4;
165 if (Kind.isMergeableConst4()) return 4;
166 if (Kind.isMergeableConst8()) return 8;
167 if (Kind.isMergeableConst16()) return 16;
168 return 0;
169}