blob: 0775cfa776d7c17fb1473cf15a271549e0de717b [file] [log] [blame]
Bruno Cardoso Lopesb8085882009-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 Lattneraf76e592009-08-22 20:48:53 +000011#include "llvm/MC/MCAsmInfo.h"
Chris Lattner4d2419d2010-01-13 21:21:29 +000012#include "llvm/MC/MCContext.h"
13#include "llvm/MC/MCSymbol.h"
Rafael Espindolac85dca62011-01-23 04:28:49 +000014#include "llvm/Support/ELF.h"
Chris Lattner4d2419d2010-01-13 21:21:29 +000015#include "llvm/Support/raw_ostream.h"
Rafael Espindolac85dca62011-01-23 04:28:49 +000016
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000017using namespace llvm;
18
Chris Lattner74aae472010-04-08 21:26:26 +000019MCSectionELF::~MCSectionELF() {} // anchor.
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000020
21// ShouldOmitSectionDirective - Decides whether a '.section' directive
22// should be printed before the section name
Benjamin Kramera46918d2010-01-22 18:21:23 +000023bool MCSectionELF::ShouldOmitSectionDirective(StringRef Name,
Chris Lattner4d2419d2010-01-13 21:21:29 +000024 const MCAsmInfo &MAI) const {
Jim Grosbach2684d9e2012-05-11 01:41:30 +000025
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000026 // FIXME: Does .section .bss/.data/.text work everywhere??
Benjamin Kramera46918d2010-01-22 18:21:23 +000027 if (Name == ".text" || Name == ".data" ||
28 (Name == ".bss" && !MAI.usesELFSectionDirectiveForBSS()))
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000029 return true;
30
31 return false;
32}
33
Chris Lattner33adcfb2009-08-22 21:43:10 +000034void MCSectionELF::PrintSwitchToSection(const MCAsmInfo &MAI,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000035 raw_ostream &OS) const {
Jim Grosbach2684d9e2012-05-11 01:41:30 +000036
Benjamin Kramera46918d2010-01-22 18:21:23 +000037 if (ShouldOmitSectionDirective(SectionName, MAI)) {
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000038 OS << '\t' << getSectionName() << '\n';
39 return;
40 }
41
Joerg Sonnenbergerea83b132011-03-03 22:31:08 +000042 StringRef name = getSectionName();
Joerg Sonnenberger89e0f382011-03-04 20:03:14 +000043 if (name.find_first_not_of("0123456789_."
44 "abcdefghijklmnopqrstuvwxyz"
45 "ABCDEFGHIJKLMNOPQRSTUVWXYZ") == name.npos) {
46 OS << "\t.section\t" << name;
47 } else {
48 OS << "\t.section\t\"";
49 for (const char *b = name.begin(), *e = name.end(); b < e; ++b) {
50 if (*b == '"') // Unquoted "
51 OS << "\\\"";
52 else if (*b != '\\') // Neither " or backslash
53 OS << *b;
54 else if (b + 1 == e) // Trailing backslash
55 OS << "\\\\";
56 else {
57 OS << b[0] << b[1]; // Quoted character
58 ++b;
59 }
Joerg Sonnenbergerea83b132011-03-03 22:31:08 +000060 }
Joerg Sonnenberger89e0f382011-03-04 20:03:14 +000061 OS << '"';
Joerg Sonnenbergerea83b132011-03-03 22:31:08 +000062 }
Joerg Sonnenbergerea83b132011-03-03 22:31:08 +000063
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000064 // Handle the weird solaris syntax if desired.
Jim Grosbach2684d9e2012-05-11 01:41:30 +000065 if (MAI.usesSunStyleELFSectionSwitchSyntax() &&
Rafael Espindola1c130262011-01-23 04:43:11 +000066 !(Flags & ELF::SHF_MERGE)) {
67 if (Flags & ELF::SHF_ALLOC)
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000068 OS << ",#alloc";
Rafael Espindola1c130262011-01-23 04:43:11 +000069 if (Flags & ELF::SHF_EXECINSTR)
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000070 OS << ",#execinstr";
Rafael Espindola1c130262011-01-23 04:43:11 +000071 if (Flags & ELF::SHF_WRITE)
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000072 OS << ",#write";
Rafael Espindola1c130262011-01-23 04:43:11 +000073 if (Flags & ELF::SHF_TLS)
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000074 OS << ",#tls";
Chris Lattner74aae472010-04-08 21:26:26 +000075 OS << '\n';
76 return;
77 }
Jim Grosbach2684d9e2012-05-11 01:41:30 +000078
Chris Lattner74aae472010-04-08 21:26:26 +000079 OS << ",\"";
Rafael Espindola1c130262011-01-23 04:43:11 +000080 if (Flags & ELF::SHF_ALLOC)
Chris Lattner74aae472010-04-08 21:26:26 +000081 OS << 'a';
Rafael Espindola1c130262011-01-23 04:43:11 +000082 if (Flags & ELF::SHF_EXECINSTR)
Chris Lattner74aae472010-04-08 21:26:26 +000083 OS << 'x';
Rafael Espindola5d618ef2011-02-14 22:23:49 +000084 if (Flags & ELF::SHF_GROUP)
85 OS << 'G';
Rafael Espindola1c130262011-01-23 04:43:11 +000086 if (Flags & ELF::SHF_WRITE)
Chris Lattner74aae472010-04-08 21:26:26 +000087 OS << 'w';
Rafael Espindola1c130262011-01-23 04:43:11 +000088 if (Flags & ELF::SHF_MERGE)
Chris Lattner74aae472010-04-08 21:26:26 +000089 OS << 'M';
Rafael Espindola1c130262011-01-23 04:43:11 +000090 if (Flags & ELF::SHF_STRINGS)
Chris Lattner74aae472010-04-08 21:26:26 +000091 OS << 'S';
Rafael Espindola1c130262011-01-23 04:43:11 +000092 if (Flags & ELF::SHF_TLS)
Chris Lattner74aae472010-04-08 21:26:26 +000093 OS << 'T';
Jim Grosbach2684d9e2012-05-11 01:41:30 +000094
Chris Lattner74aae472010-04-08 21:26:26 +000095 // If there are target-specific flags, print them.
Rafael Espindola1c130262011-01-23 04:43:11 +000096 if (Flags & ELF::XCORE_SHF_CP_SECTION)
Chris Lattner74aae472010-04-08 21:26:26 +000097 OS << 'c';
Rafael Espindola1c130262011-01-23 04:43:11 +000098 if (Flags & ELF::XCORE_SHF_DP_SECTION)
Chris Lattner74aae472010-04-08 21:26:26 +000099 OS << 'd';
Jim Grosbach2684d9e2012-05-11 01:41:30 +0000100
Chris Lattner74aae472010-04-08 21:26:26 +0000101 OS << '"';
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000102
Rafael Espindola34be3962010-11-09 23:42:07 +0000103 OS << ',';
104
105 // If comment string is '@', e.g. as on ARM - use '%' instead
106 if (MAI.getCommentString()[0] == '@')
107 OS << '%';
108 else
109 OS << '@';
110
Rafael Espindolac85dca62011-01-23 04:28:49 +0000111 if (Type == ELF::SHT_INIT_ARRAY)
Rafael Espindola34be3962010-11-09 23:42:07 +0000112 OS << "init_array";
Rafael Espindolac85dca62011-01-23 04:28:49 +0000113 else if (Type == ELF::SHT_FINI_ARRAY)
Rafael Espindola34be3962010-11-09 23:42:07 +0000114 OS << "fini_array";
Rafael Espindolac85dca62011-01-23 04:28:49 +0000115 else if (Type == ELF::SHT_PREINIT_ARRAY)
Rafael Espindola34be3962010-11-09 23:42:07 +0000116 OS << "preinit_array";
Rafael Espindolac85dca62011-01-23 04:28:49 +0000117 else if (Type == ELF::SHT_NOBITS)
Rafael Espindola34be3962010-11-09 23:42:07 +0000118 OS << "nobits";
Rafael Espindolac85dca62011-01-23 04:28:49 +0000119 else if (Type == ELF::SHT_NOTE)
Rafael Espindola98976612010-12-26 21:30:59 +0000120 OS << "note";
Rafael Espindolac85dca62011-01-23 04:28:49 +0000121 else if (Type == ELF::SHT_PROGBITS)
Rafael Espindola34be3962010-11-09 23:42:07 +0000122 OS << "progbits";
123
124 if (EntrySize) {
Rafael Espindola1c130262011-01-23 04:43:11 +0000125 assert(Flags & ELF::SHF_MERGE);
Rafael Espindola34be3962010-11-09 23:42:07 +0000126 OS << "," << EntrySize;
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000127 }
Rafael Espindola34be3962010-11-09 23:42:07 +0000128
Rafael Espindola5d618ef2011-02-14 22:23:49 +0000129 if (Flags & ELF::SHF_GROUP)
130 OS << "," << Group->getName() << ",comdat";
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000131 OS << '\n';
132}
133
Jan Wen Voung083cf152010-10-04 17:32:41 +0000134bool MCSectionELF::UseCodeAlign() const {
Rafael Espindola1c130262011-01-23 04:43:11 +0000135 return getFlags() & ELF::SHF_EXECINSTR;
Jan Wen Voung083cf152010-10-04 17:32:41 +0000136}
137
Rafael Espindolaf2dc4aa2010-11-17 20:03:54 +0000138bool MCSectionELF::isVirtualSection() const {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000139 return getType() == ELF::SHT_NOBITS;
Rafael Espindolaf2dc4aa2010-11-17 20:03:54 +0000140}
141
Jan Wen Voung186e7a02010-09-30 05:59:22 +0000142unsigned MCSectionELF::DetermineEntrySize(SectionKind Kind) {
143 if (Kind.isMergeable1ByteCString()) return 1;
144 if (Kind.isMergeable2ByteCString()) return 2;
145 if (Kind.isMergeable4ByteCString()) return 4;
146 if (Kind.isMergeableConst4()) return 4;
147 if (Kind.isMergeableConst8()) return 8;
148 if (Kind.isMergeableConst16()) return 16;
149 return 0;
150}