blob: a53489790d28c3143149c4292803fbe30b7b3a4d [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"
14#include "llvm/Support/raw_ostream.h"
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000015using namespace llvm;
16
Chris Lattner74aae472010-04-08 21:26:26 +000017MCSectionELF::~MCSectionELF() {} // anchor.
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000018
19// ShouldOmitSectionDirective - Decides whether a '.section' directive
20// should be printed before the section name
Benjamin Kramera46918d2010-01-22 18:21:23 +000021bool MCSectionELF::ShouldOmitSectionDirective(StringRef Name,
Chris Lattner4d2419d2010-01-13 21:21:29 +000022 const MCAsmInfo &MAI) const {
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000023
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000024 // FIXME: Does .section .bss/.data/.text work everywhere??
Benjamin Kramera46918d2010-01-22 18:21:23 +000025 if (Name == ".text" || Name == ".data" ||
26 (Name == ".bss" && !MAI.usesELFSectionDirectiveForBSS()))
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000027 return true;
28
29 return false;
30}
31
Chris Lattner33adcfb2009-08-22 21:43:10 +000032void MCSectionELF::PrintSwitchToSection(const MCAsmInfo &MAI,
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000033 raw_ostream &OS) const {
Bruno Cardoso Lopesfdf229e2009-08-13 23:30:21 +000034
Benjamin Kramera46918d2010-01-22 18:21:23 +000035 if (ShouldOmitSectionDirective(SectionName, MAI)) {
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000036 OS << '\t' << getSectionName() << '\n';
37 return;
38 }
39
40 OS << "\t.section\t" << getSectionName();
41
42 // Handle the weird solaris syntax if desired.
Chris Lattner33adcfb2009-08-22 21:43:10 +000043 if (MAI.usesSunStyleELFSectionSwitchSyntax() &&
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000044 !(Flags & MCSectionELF::SHF_MERGE)) {
45 if (Flags & MCSectionELF::SHF_ALLOC)
46 OS << ",#alloc";
47 if (Flags & MCSectionELF::SHF_EXECINSTR)
48 OS << ",#execinstr";
49 if (Flags & MCSectionELF::SHF_WRITE)
50 OS << ",#write";
51 if (Flags & MCSectionELF::SHF_TLS)
52 OS << ",#tls";
Chris Lattner74aae472010-04-08 21:26:26 +000053 OS << '\n';
54 return;
55 }
56
57 OS << ",\"";
58 if (Flags & MCSectionELF::SHF_ALLOC)
59 OS << 'a';
60 if (Flags & MCSectionELF::SHF_EXECINSTR)
61 OS << 'x';
62 if (Flags & MCSectionELF::SHF_WRITE)
63 OS << 'w';
64 if (Flags & MCSectionELF::SHF_MERGE)
65 OS << 'M';
66 if (Flags & MCSectionELF::SHF_STRINGS)
67 OS << 'S';
68 if (Flags & MCSectionELF::SHF_TLS)
69 OS << 'T';
70
71 // If there are target-specific flags, print them.
72 if (Flags & MCSectionELF::XCORE_SHF_CP_SECTION)
73 OS << 'c';
74 if (Flags & MCSectionELF::XCORE_SHF_DP_SECTION)
75 OS << 'd';
76
77 OS << '"';
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000078
Rafael Espindola34be3962010-11-09 23:42:07 +000079 OS << ',';
80
81 // If comment string is '@', e.g. as on ARM - use '%' instead
82 if (MAI.getCommentString()[0] == '@')
83 OS << '%';
84 else
85 OS << '@';
86
87 if (Type == MCSectionELF::SHT_INIT_ARRAY)
88 OS << "init_array";
89 else if (Type == MCSectionELF::SHT_FINI_ARRAY)
90 OS << "fini_array";
91 else if (Type == MCSectionELF::SHT_PREINIT_ARRAY)
92 OS << "preinit_array";
93 else if (Type == MCSectionELF::SHT_NOBITS)
94 OS << "nobits";
Rafael Espindola98976612010-12-26 21:30:59 +000095 else if (Type == MCSectionELF::SHT_NOTE)
96 OS << "note";
Rafael Espindola34be3962010-11-09 23:42:07 +000097 else if (Type == MCSectionELF::SHT_PROGBITS)
98 OS << "progbits";
99
100 if (EntrySize) {
101 assert(Flags & MCSectionELF::SHF_MERGE);
102 OS << "," << EntrySize;
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000103 }
Rafael Espindola34be3962010-11-09 23:42:07 +0000104
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000105 OS << '\n';
106}
107
Jan Wen Voung083cf152010-10-04 17:32:41 +0000108bool MCSectionELF::UseCodeAlign() const {
109 return getFlags() & MCSectionELF::SHF_EXECINSTR;
110}
111
Rafael Espindolaf2dc4aa2010-11-17 20:03:54 +0000112bool MCSectionELF::isVirtualSection() const {
113 return getType() == MCSectionELF::SHT_NOBITS;
114}
115
Bruno Cardoso Lopesabd75142009-08-14 19:45:38 +0000116// HasCommonSymbols - True if this section holds common symbols, this is
117// indicated on the ELF object file by a symbol with SHN_COMMON section
118// header index.
119bool MCSectionELF::HasCommonSymbols() const {
Bruno Cardoso Lopes4aa688e2009-08-13 21:08:56 +0000120
Benjamin Kramera46918d2010-01-22 18:21:23 +0000121 if (StringRef(SectionName).startswith(".gnu.linkonce."))
Bruno Cardoso Lopes4aa688e2009-08-13 21:08:56 +0000122 return true;
123
124 return false;
125}
126
Jan Wen Voung186e7a02010-09-30 05:59:22 +0000127unsigned MCSectionELF::DetermineEntrySize(SectionKind Kind) {
128 if (Kind.isMergeable1ByteCString()) return 1;
129 if (Kind.isMergeable2ByteCString()) return 2;
130 if (Kind.isMergeable4ByteCString()) return 4;
131 if (Kind.isMergeableConst4()) return 4;
132 if (Kind.isMergeableConst8()) return 8;
133 if (Kind.isMergeableConst16()) return 16;
134 return 0;
135}