blob: bf1a984a9bf6cf5ed888e2225b70ed3244be56f5 [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"
Peter Collingbournedf39be62013-04-17 21:18:16 +000013#include "llvm/MC/MCExpr.h"
Chris Lattner4d2419d2010-01-13 21:21:29 +000014#include "llvm/MC/MCSymbol.h"
Rafael Espindolac85dca62011-01-23 04:28:49 +000015#include "llvm/Support/ELF.h"
Chris Lattner4d2419d2010-01-13 21:21:29 +000016#include "llvm/Support/raw_ostream.h"
Rafael Espindolac85dca62011-01-23 04:28:49 +000017
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000018using namespace llvm;
19
Chris Lattner74aae472010-04-08 21:26:26 +000020MCSectionELF::~MCSectionELF() {} // anchor.
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000021
22// ShouldOmitSectionDirective - Decides whether a '.section' directive
23// should be printed before the section name
Benjamin Kramera46918d2010-01-22 18:21:23 +000024bool MCSectionELF::ShouldOmitSectionDirective(StringRef Name,
Chris Lattner4d2419d2010-01-13 21:21:29 +000025 const MCAsmInfo &MAI) const {
Jim Grosbach2684d9e2012-05-11 01:41:30 +000026
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000027 // FIXME: Does .section .bss/.data/.text work everywhere??
Benjamin Kramera46918d2010-01-22 18:21:23 +000028 if (Name == ".text" || Name == ".data" ||
29 (Name == ".bss" && !MAI.usesELFSectionDirectiveForBSS()))
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000030 return true;
31
32 return false;
33}
34
Chris Lattner33adcfb2009-08-22 21:43:10 +000035void MCSectionELF::PrintSwitchToSection(const MCAsmInfo &MAI,
Peter Collingbournedf39be62013-04-17 21:18:16 +000036 raw_ostream &OS,
37 const MCExpr *Subsection) const {
Jim Grosbach2684d9e2012-05-11 01:41:30 +000038
Benjamin Kramera46918d2010-01-22 18:21:23 +000039 if (ShouldOmitSectionDirective(SectionName, MAI)) {
Peter Collingbournedf39be62013-04-17 21:18:16 +000040 OS << '\t' << getSectionName();
41 if (Subsection)
42 OS << '\t' << *Subsection;
43 OS << '\n';
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000044 return;
45 }
46
Joerg Sonnenbergerea83b132011-03-03 22:31:08 +000047 StringRef name = getSectionName();
Joerg Sonnenberger89e0f382011-03-04 20:03:14 +000048 if (name.find_first_not_of("0123456789_."
49 "abcdefghijklmnopqrstuvwxyz"
50 "ABCDEFGHIJKLMNOPQRSTUVWXYZ") == name.npos) {
51 OS << "\t.section\t" << name;
52 } else {
53 OS << "\t.section\t\"";
54 for (const char *b = name.begin(), *e = name.end(); b < e; ++b) {
55 if (*b == '"') // Unquoted "
56 OS << "\\\"";
57 else if (*b != '\\') // Neither " or backslash
58 OS << *b;
59 else if (b + 1 == e) // Trailing backslash
60 OS << "\\\\";
61 else {
62 OS << b[0] << b[1]; // Quoted character
63 ++b;
64 }
Joerg Sonnenbergerea83b132011-03-03 22:31:08 +000065 }
Joerg Sonnenberger89e0f382011-03-04 20:03:14 +000066 OS << '"';
Joerg Sonnenbergerea83b132011-03-03 22:31:08 +000067 }
Joerg Sonnenbergerea83b132011-03-03 22:31:08 +000068
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000069 // Handle the weird solaris syntax if desired.
Jim Grosbach2684d9e2012-05-11 01:41:30 +000070 if (MAI.usesSunStyleELFSectionSwitchSyntax() &&
Rafael Espindola1c130262011-01-23 04:43:11 +000071 !(Flags & ELF::SHF_MERGE)) {
72 if (Flags & ELF::SHF_ALLOC)
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000073 OS << ",#alloc";
Rafael Espindola1c130262011-01-23 04:43:11 +000074 if (Flags & ELF::SHF_EXECINSTR)
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000075 OS << ",#execinstr";
Rafael Espindola1c130262011-01-23 04:43:11 +000076 if (Flags & ELF::SHF_WRITE)
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000077 OS << ",#write";
Rafael Espindola1c130262011-01-23 04:43:11 +000078 if (Flags & ELF::SHF_TLS)
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000079 OS << ",#tls";
Chris Lattner74aae472010-04-08 21:26:26 +000080 OS << '\n';
81 return;
82 }
Jim Grosbach2684d9e2012-05-11 01:41:30 +000083
Chris Lattner74aae472010-04-08 21:26:26 +000084 OS << ",\"";
Rafael Espindola1c130262011-01-23 04:43:11 +000085 if (Flags & ELF::SHF_ALLOC)
Chris Lattner74aae472010-04-08 21:26:26 +000086 OS << 'a';
Rafael Espindola1c130262011-01-23 04:43:11 +000087 if (Flags & ELF::SHF_EXECINSTR)
Chris Lattner74aae472010-04-08 21:26:26 +000088 OS << 'x';
Rafael Espindola5d618ef2011-02-14 22:23:49 +000089 if (Flags & ELF::SHF_GROUP)
90 OS << 'G';
Rafael Espindola1c130262011-01-23 04:43:11 +000091 if (Flags & ELF::SHF_WRITE)
Chris Lattner74aae472010-04-08 21:26:26 +000092 OS << 'w';
Rafael Espindola1c130262011-01-23 04:43:11 +000093 if (Flags & ELF::SHF_MERGE)
Chris Lattner74aae472010-04-08 21:26:26 +000094 OS << 'M';
Rafael Espindola1c130262011-01-23 04:43:11 +000095 if (Flags & ELF::SHF_STRINGS)
Chris Lattner74aae472010-04-08 21:26:26 +000096 OS << 'S';
Rafael Espindola1c130262011-01-23 04:43:11 +000097 if (Flags & ELF::SHF_TLS)
Chris Lattner74aae472010-04-08 21:26:26 +000098 OS << 'T';
Jim Grosbach2684d9e2012-05-11 01:41:30 +000099
Chris Lattner74aae472010-04-08 21:26:26 +0000100 // If there are target-specific flags, print them.
Rafael Espindola1c130262011-01-23 04:43:11 +0000101 if (Flags & ELF::XCORE_SHF_CP_SECTION)
Chris Lattner74aae472010-04-08 21:26:26 +0000102 OS << 'c';
Rafael Espindola1c130262011-01-23 04:43:11 +0000103 if (Flags & ELF::XCORE_SHF_DP_SECTION)
Chris Lattner74aae472010-04-08 21:26:26 +0000104 OS << 'd';
Jim Grosbach2684d9e2012-05-11 01:41:30 +0000105
Chris Lattner74aae472010-04-08 21:26:26 +0000106 OS << '"';
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000107
Rafael Espindola34be3962010-11-09 23:42:07 +0000108 OS << ',';
109
110 // If comment string is '@', e.g. as on ARM - use '%' instead
111 if (MAI.getCommentString()[0] == '@')
112 OS << '%';
113 else
114 OS << '@';
115
Rafael Espindolac85dca62011-01-23 04:28:49 +0000116 if (Type == ELF::SHT_INIT_ARRAY)
Rafael Espindola34be3962010-11-09 23:42:07 +0000117 OS << "init_array";
Rafael Espindolac85dca62011-01-23 04:28:49 +0000118 else if (Type == ELF::SHT_FINI_ARRAY)
Rafael Espindola34be3962010-11-09 23:42:07 +0000119 OS << "fini_array";
Rafael Espindolac85dca62011-01-23 04:28:49 +0000120 else if (Type == ELF::SHT_PREINIT_ARRAY)
Rafael Espindola34be3962010-11-09 23:42:07 +0000121 OS << "preinit_array";
Rafael Espindolac85dca62011-01-23 04:28:49 +0000122 else if (Type == ELF::SHT_NOBITS)
Rafael Espindola34be3962010-11-09 23:42:07 +0000123 OS << "nobits";
Rafael Espindolac85dca62011-01-23 04:28:49 +0000124 else if (Type == ELF::SHT_NOTE)
Rafael Espindola98976612010-12-26 21:30:59 +0000125 OS << "note";
Rafael Espindolac85dca62011-01-23 04:28:49 +0000126 else if (Type == ELF::SHT_PROGBITS)
Rafael Espindola34be3962010-11-09 23:42:07 +0000127 OS << "progbits";
128
129 if (EntrySize) {
Rafael Espindola1c130262011-01-23 04:43:11 +0000130 assert(Flags & ELF::SHF_MERGE);
Rafael Espindola34be3962010-11-09 23:42:07 +0000131 OS << "," << EntrySize;
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000132 }
Rafael Espindola34be3962010-11-09 23:42:07 +0000133
Rafael Espindola5d618ef2011-02-14 22:23:49 +0000134 if (Flags & ELF::SHF_GROUP)
135 OS << "," << Group->getName() << ",comdat";
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000136 OS << '\n';
Peter Collingbournedf39be62013-04-17 21:18:16 +0000137
138 if (Subsection)
139 OS << "\t.subsection\t" << *Subsection << '\n';
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000140}
141
Jan Wen Voung083cf152010-10-04 17:32:41 +0000142bool MCSectionELF::UseCodeAlign() const {
Rafael Espindola1c130262011-01-23 04:43:11 +0000143 return getFlags() & ELF::SHF_EXECINSTR;
Jan Wen Voung083cf152010-10-04 17:32:41 +0000144}
145
Rafael Espindolaf2dc4aa2010-11-17 20:03:54 +0000146bool MCSectionELF::isVirtualSection() const {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000147 return getType() == ELF::SHT_NOBITS;
Rafael Espindolaf2dc4aa2010-11-17 20:03:54 +0000148}
149
Jan Wen Voung186e7a02010-09-30 05:59:22 +0000150unsigned MCSectionELF::DetermineEntrySize(SectionKind Kind) {
151 if (Kind.isMergeable1ByteCString()) return 1;
152 if (Kind.isMergeable2ByteCString()) return 2;
153 if (Kind.isMergeable4ByteCString()) return 4;
154 if (Kind.isMergeableConst4()) return 4;
155 if (Kind.isMergeableConst8()) return 8;
156 if (Kind.isMergeableConst16()) return 16;
157 return 0;
158}