blob: d32aea144e6ec8f37a4a694d6f46eb8f6a9e5582 [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 {
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +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 {
Bruno Cardoso Lopesfdf229e2009-08-13 23:30:21 +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
42 OS << "\t.section\t" << getSectionName();
43
44 // Handle the weird solaris syntax if desired.
Chris Lattner33adcfb2009-08-22 21:43:10 +000045 if (MAI.usesSunStyleELFSectionSwitchSyntax() &&
Rafael Espindola1c130262011-01-23 04:43:11 +000046 !(Flags & ELF::SHF_MERGE)) {
47 if (Flags & ELF::SHF_ALLOC)
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000048 OS << ",#alloc";
Rafael Espindola1c130262011-01-23 04:43:11 +000049 if (Flags & ELF::SHF_EXECINSTR)
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000050 OS << ",#execinstr";
Rafael Espindola1c130262011-01-23 04:43:11 +000051 if (Flags & ELF::SHF_WRITE)
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000052 OS << ",#write";
Rafael Espindola1c130262011-01-23 04:43:11 +000053 if (Flags & ELF::SHF_TLS)
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000054 OS << ",#tls";
Chris Lattner74aae472010-04-08 21:26:26 +000055 OS << '\n';
56 return;
57 }
58
59 OS << ",\"";
Rafael Espindola1c130262011-01-23 04:43:11 +000060 if (Flags & ELF::SHF_ALLOC)
Chris Lattner74aae472010-04-08 21:26:26 +000061 OS << 'a';
Rafael Espindola1c130262011-01-23 04:43:11 +000062 if (Flags & ELF::SHF_EXECINSTR)
Chris Lattner74aae472010-04-08 21:26:26 +000063 OS << 'x';
Rafael Espindola5d618ef2011-02-14 22:23:49 +000064 if (Flags & ELF::SHF_GROUP)
65 OS << 'G';
Rafael Espindola1c130262011-01-23 04:43:11 +000066 if (Flags & ELF::SHF_WRITE)
Chris Lattner74aae472010-04-08 21:26:26 +000067 OS << 'w';
Rafael Espindola1c130262011-01-23 04:43:11 +000068 if (Flags & ELF::SHF_MERGE)
Chris Lattner74aae472010-04-08 21:26:26 +000069 OS << 'M';
Rafael Espindola1c130262011-01-23 04:43:11 +000070 if (Flags & ELF::SHF_STRINGS)
Chris Lattner74aae472010-04-08 21:26:26 +000071 OS << 'S';
Rafael Espindola1c130262011-01-23 04:43:11 +000072 if (Flags & ELF::SHF_TLS)
Chris Lattner74aae472010-04-08 21:26:26 +000073 OS << 'T';
74
75 // If there are target-specific flags, print them.
Rafael Espindola1c130262011-01-23 04:43:11 +000076 if (Flags & ELF::XCORE_SHF_CP_SECTION)
Chris Lattner74aae472010-04-08 21:26:26 +000077 OS << 'c';
Rafael Espindola1c130262011-01-23 04:43:11 +000078 if (Flags & ELF::XCORE_SHF_DP_SECTION)
Chris Lattner74aae472010-04-08 21:26:26 +000079 OS << 'd';
80
81 OS << '"';
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000082
Rafael Espindola34be3962010-11-09 23:42:07 +000083 OS << ',';
84
85 // If comment string is '@', e.g. as on ARM - use '%' instead
86 if (MAI.getCommentString()[0] == '@')
87 OS << '%';
88 else
89 OS << '@';
90
Rafael Espindolac85dca62011-01-23 04:28:49 +000091 if (Type == ELF::SHT_INIT_ARRAY)
Rafael Espindola34be3962010-11-09 23:42:07 +000092 OS << "init_array";
Rafael Espindolac85dca62011-01-23 04:28:49 +000093 else if (Type == ELF::SHT_FINI_ARRAY)
Rafael Espindola34be3962010-11-09 23:42:07 +000094 OS << "fini_array";
Rafael Espindolac85dca62011-01-23 04:28:49 +000095 else if (Type == ELF::SHT_PREINIT_ARRAY)
Rafael Espindola34be3962010-11-09 23:42:07 +000096 OS << "preinit_array";
Rafael Espindolac85dca62011-01-23 04:28:49 +000097 else if (Type == ELF::SHT_NOBITS)
Rafael Espindola34be3962010-11-09 23:42:07 +000098 OS << "nobits";
Rafael Espindolac85dca62011-01-23 04:28:49 +000099 else if (Type == ELF::SHT_NOTE)
Rafael Espindola98976612010-12-26 21:30:59 +0000100 OS << "note";
Rafael Espindolac85dca62011-01-23 04:28:49 +0000101 else if (Type == ELF::SHT_PROGBITS)
Rafael Espindola34be3962010-11-09 23:42:07 +0000102 OS << "progbits";
103
104 if (EntrySize) {
Rafael Espindola1c130262011-01-23 04:43:11 +0000105 assert(Flags & ELF::SHF_MERGE);
Rafael Espindola34be3962010-11-09 23:42:07 +0000106 OS << "," << EntrySize;
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000107 }
Rafael Espindola34be3962010-11-09 23:42:07 +0000108
Rafael Espindola5d618ef2011-02-14 22:23:49 +0000109 if (Flags & ELF::SHF_GROUP)
110 OS << "," << Group->getName() << ",comdat";
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000111 OS << '\n';
112}
113
Jan Wen Voung083cf152010-10-04 17:32:41 +0000114bool MCSectionELF::UseCodeAlign() const {
Rafael Espindola1c130262011-01-23 04:43:11 +0000115 return getFlags() & ELF::SHF_EXECINSTR;
Jan Wen Voung083cf152010-10-04 17:32:41 +0000116}
117
Rafael Espindolaf2dc4aa2010-11-17 20:03:54 +0000118bool MCSectionELF::isVirtualSection() const {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000119 return getType() == ELF::SHT_NOBITS;
Rafael Espindolaf2dc4aa2010-11-17 20:03:54 +0000120}
121
Jan Wen Voung186e7a02010-09-30 05:59:22 +0000122unsigned MCSectionELF::DetermineEntrySize(SectionKind Kind) {
123 if (Kind.isMergeable1ByteCString()) return 1;
124 if (Kind.isMergeable2ByteCString()) return 2;
125 if (Kind.isMergeable4ByteCString()) return 4;
126 if (Kind.isMergeableConst4()) return 4;
127 if (Kind.isMergeableConst8()) return 8;
128 if (Kind.isMergeableConst16()) return 16;
129 return 0;
130}