blob: 4a44f5a9067fb7bea251702db2ce96de88e6beeb [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() &&
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000046 !(Flags & MCSectionELF::SHF_MERGE)) {
47 if (Flags & MCSectionELF::SHF_ALLOC)
48 OS << ",#alloc";
49 if (Flags & MCSectionELF::SHF_EXECINSTR)
50 OS << ",#execinstr";
51 if (Flags & MCSectionELF::SHF_WRITE)
52 OS << ",#write";
53 if (Flags & MCSectionELF::SHF_TLS)
54 OS << ",#tls";
Chris Lattner74aae472010-04-08 21:26:26 +000055 OS << '\n';
56 return;
57 }
58
59 OS << ",\"";
60 if (Flags & MCSectionELF::SHF_ALLOC)
61 OS << 'a';
62 if (Flags & MCSectionELF::SHF_EXECINSTR)
63 OS << 'x';
64 if (Flags & MCSectionELF::SHF_WRITE)
65 OS << 'w';
66 if (Flags & MCSectionELF::SHF_MERGE)
67 OS << 'M';
68 if (Flags & MCSectionELF::SHF_STRINGS)
69 OS << 'S';
70 if (Flags & MCSectionELF::SHF_TLS)
71 OS << 'T';
72
73 // If there are target-specific flags, print them.
74 if (Flags & MCSectionELF::XCORE_SHF_CP_SECTION)
75 OS << 'c';
76 if (Flags & MCSectionELF::XCORE_SHF_DP_SECTION)
77 OS << 'd';
78
79 OS << '"';
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +000080
Rafael Espindola34be3962010-11-09 23:42:07 +000081 OS << ',';
82
83 // If comment string is '@', e.g. as on ARM - use '%' instead
84 if (MAI.getCommentString()[0] == '@')
85 OS << '%';
86 else
87 OS << '@';
88
Rafael Espindolac85dca62011-01-23 04:28:49 +000089 if (Type == ELF::SHT_INIT_ARRAY)
Rafael Espindola34be3962010-11-09 23:42:07 +000090 OS << "init_array";
Rafael Espindolac85dca62011-01-23 04:28:49 +000091 else if (Type == ELF::SHT_FINI_ARRAY)
Rafael Espindola34be3962010-11-09 23:42:07 +000092 OS << "fini_array";
Rafael Espindolac85dca62011-01-23 04:28:49 +000093 else if (Type == ELF::SHT_PREINIT_ARRAY)
Rafael Espindola34be3962010-11-09 23:42:07 +000094 OS << "preinit_array";
Rafael Espindolac85dca62011-01-23 04:28:49 +000095 else if (Type == ELF::SHT_NOBITS)
Rafael Espindola34be3962010-11-09 23:42:07 +000096 OS << "nobits";
Rafael Espindolac85dca62011-01-23 04:28:49 +000097 else if (Type == ELF::SHT_NOTE)
Rafael Espindola98976612010-12-26 21:30:59 +000098 OS << "note";
Rafael Espindolac85dca62011-01-23 04:28:49 +000099 else if (Type == ELF::SHT_PROGBITS)
Rafael Espindola34be3962010-11-09 23:42:07 +0000100 OS << "progbits";
101
102 if (EntrySize) {
103 assert(Flags & MCSectionELF::SHF_MERGE);
104 OS << "," << EntrySize;
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000105 }
Rafael Espindola34be3962010-11-09 23:42:07 +0000106
Bruno Cardoso Lopesb8085882009-08-13 05:07:35 +0000107 OS << '\n';
108}
109
Jan Wen Voung083cf152010-10-04 17:32:41 +0000110bool MCSectionELF::UseCodeAlign() const {
111 return getFlags() & MCSectionELF::SHF_EXECINSTR;
112}
113
Rafael Espindolaf2dc4aa2010-11-17 20:03:54 +0000114bool MCSectionELF::isVirtualSection() const {
Rafael Espindolac85dca62011-01-23 04:28:49 +0000115 return getType() == ELF::SHT_NOBITS;
Rafael Espindolaf2dc4aa2010-11-17 20:03:54 +0000116}
117
Bruno Cardoso Lopesabd75142009-08-14 19:45:38 +0000118// HasCommonSymbols - True if this section holds common symbols, this is
119// indicated on the ELF object file by a symbol with SHN_COMMON section
120// header index.
121bool MCSectionELF::HasCommonSymbols() const {
Bruno Cardoso Lopes4aa688e2009-08-13 21:08:56 +0000122
Benjamin Kramera46918d2010-01-22 18:21:23 +0000123 if (StringRef(SectionName).startswith(".gnu.linkonce."))
Bruno Cardoso Lopes4aa688e2009-08-13 21:08:56 +0000124 return true;
125
126 return false;
127}
128
Jan Wen Voung186e7a02010-09-30 05:59:22 +0000129unsigned MCSectionELF::DetermineEntrySize(SectionKind Kind) {
130 if (Kind.isMergeable1ByteCString()) return 1;
131 if (Kind.isMergeable2ByteCString()) return 2;
132 if (Kind.isMergeable4ByteCString()) return 4;
133 if (Kind.isMergeableConst4()) return 4;
134 if (Kind.isMergeableConst8()) return 8;
135 if (Kind.isMergeableConst16()) return 16;
136 return 0;
137}