Remove trailing whitespace to reduce later commit patch noise.
[oota-llvm.git] / lib / CodeGen / ELFWriter.cpp
index 3b9653740cc02d47419f3b286f5342569902fc83..b698178d780db6007b36cf325c3548f65d46ca05 100644 (file)
@@ -2,8 +2,8 @@
 //
 //                     The LLVM Compiler Infrastructure
 //
-// This file was developed by Chris Lattner and is distributed under the
-// University of Illinois Open Source License. See LICENSE.TXT for details.
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
 //
 //===----------------------------------------------------------------------===//
 //
 //
 //===----------------------------------------------------------------------===//
 
-#include "llvm/CodeGen/ELFWriter.h"
+#include "ELFWriter.h"
 #include "llvm/Module.h"
+#include "llvm/PassManager.h"
+#include "llvm/DerivedTypes.h"
+#include "llvm/CodeGen/FileWriters.h"
 #include "llvm/CodeGen/MachineCodeEmitter.h"
 #include "llvm/CodeGen/MachineConstantPool.h"
+#include "llvm/CodeGen/MachineFunctionPass.h"
 #include "llvm/Target/TargetData.h"
+#include "llvm/Target/TargetELFWriterInfo.h"
 #include "llvm/Target/TargetMachine.h"
 #include "llvm/Support/Mangler.h"
 #include "llvm/Support/OutputBuffer.h"
 #include "llvm/Support/Streams.h"
+#include "llvm/Support/raw_ostream.h"
+#include <list>
 using namespace llvm;
 
+char ELFWriter::ID = 0;
+/// AddELFWriter - Concrete function to add the ELF writer to the function pass
+/// manager.
+MachineCodeEmitter *llvm::AddELFWriter(PassManagerBase &PM,
+                                       raw_ostream &O,
+                                       TargetMachine &TM) {
+  ELFWriter *EW = new ELFWriter(O, TM);
+  PM.add(EW);
+  return &EW->getMachineCodeEmitter();
+}
+
 //===----------------------------------------------------------------------===//
 //                       ELFCodeEmitter Implementation
 //===----------------------------------------------------------------------===//
@@ -56,7 +74,7 @@ namespace llvm {
     std::vector<unsigned char> *OutBuffer;
     size_t FnStart;
   public:
-    ELFCodeEmitter(ELFWriter &ew) : EW(ew), TM(EW.TM), OutBuffer(0) {}
+    explicit ELFCodeEmitter(ELFWriter &ew) : EW(ew), TM(EW.TM), OutBuffer(0) {}
 
     void startFunction(MachineFunction &F);
     bool finishFunction(MachineFunction &F);
@@ -68,26 +86,42 @@ namespace llvm {
     virtual void StartMachineBasicBlock(MachineBasicBlock *MBB) {
     }
 
-    virtual intptr_t getConstantPoolEntryAddress(unsigned Index) const {
+    virtual uintptr_t getConstantPoolEntryAddress(unsigned Index) const {
       assert(0 && "CP not implementated yet!");
       return 0;
     }
-    virtual intptr_t getJumpTableEntryAddress(unsigned Index) const {
+    virtual uintptr_t getJumpTableEntryAddress(unsigned Index) const {
       assert(0 && "JT not implementated yet!");
       return 0;
     }
 
-    virtual intptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const {
+    virtual uintptr_t getMachineBasicBlockAddress(MachineBasicBlock *MBB) const {
       assert(0 && "JT not implementated yet!");
       return 0;
     }
 
+    virtual uintptr_t getLabelAddress(uint64_t Label) const {
+      assert(0 && "Label address not implementated yet!");
+      abort();
+      return 0;
+    }
+
+    virtual void emitLabel(uint64_t LabelID) {
+      assert(0 && "emit Label not implementated yet!");
+      abort();
+    }
+
+
+    virtual void setModuleInfo(llvm::MachineModuleInfo* MMI) { }
+
+
     /// JIT SPECIFIC FUNCTIONS - DO NOT IMPLEMENT THESE HERE!
-    void startFunctionStub(unsigned StubSize, unsigned Alignment = 1) {
+    void startGVStub(const GlobalValue* F, unsigned StubSize,
+                     unsigned Alignment = 1) {
       assert(0 && "JIT specific function called!");
       abort();
     }
-    void *finishFunctionStub(const Function *F) {
+    void *finishGVStub(const GlobalValue *F) {
       assert(0 && "JIT specific function called!");
       abort();
       return 0;
@@ -140,6 +174,8 @@ bool ELFCodeEmitter::finishFunction(MachineFunction &F) {
   case GlobalValue::WeakLinkage:
     FnSym.SetBind(ELFWriter::ELFSym::STB_WEAK);
     break;
+  case GlobalValue::PrivateLinkage:
+    assert (0 && "PrivateLinkage should not be in the symbol table.");
   case GlobalValue::InternalLinkage:
     FnSym.SetBind(ELFWriter::ELFSym::STB_LOCAL);
     break;
@@ -161,8 +197,8 @@ bool ELFCodeEmitter::finishFunction(MachineFunction &F) {
 //                          ELFWriter Implementation
 //===----------------------------------------------------------------------===//
 
-ELFWriter::ELFWriter(std::ostream &o, TargetMachine &tm) : O(o), TM(tm) {
-  e_machine = 0;  // e_machine defaults to 'No Machine'
+ELFWriter::ELFWriter(raw_ostream &o, TargetMachine &tm) 
+  : MachineFunctionPass(&ID), O(o), TM(tm) {
   e_flags = 0;    // e_flags defaults to 0, no flags.
 
   is64Bit = TM.getTargetData()->getPointerSizeInBits() == 64;
@@ -197,7 +233,7 @@ bool ELFWriter::doInitialization(Module &M) {
 
   // This should change for shared objects.
   FHOut.outhalf(1);                 // e_type = ET_REL
-  FHOut.outhalf(e_machine);         // e_machine = whatever the target wants
+  FHOut.outhalf(TM.getELFWriterInfo()->getEMachine()); // target-defined
   FHOut.outword(1);                 // e_version = 1
   FHOut.outaddr(0);                 // e_entry = 0 -> no entry point in .o file
   FHOut.outaddr(0);                 // e_phoff = 0 -> no program header for .o
@@ -240,9 +276,9 @@ void ELFWriter::EmitGlobal(GlobalVariable *GV) {
     return;
   }
 
-  const Type *GVType = (const Type*)GV->getType();
-  unsigned Align = TM.getTargetData()->getTypeAlignmentPref(GVType);
-  unsigned Size  = TM.getTargetData()->getTypeSize(GVType);
+  unsigned Align = TM.getTargetData()->getPreferredAlignment(GV);
+  unsigned Size  =
+    TM.getTargetData()->getTypePaddedSize(GV->getType()->getElementType());
 
   // If this global has a zero initializer, it is part of the .bss or common
   // section.
@@ -250,7 +286,8 @@ void ELFWriter::EmitGlobal(GlobalVariable *GV) {
     // If this global is part of the common block, add it now.  Variables are
     // part of the common block if they are zero initialized and allowed to be
     // merged with other symbols.
-    if (GV->hasLinkOnceLinkage() || GV->hasWeakLinkage()) {
+    if (GV->hasLinkOnceLinkage() || GV->hasWeakLinkage() ||
+        GV->hasCommonLinkage()) {
       ELFSym CommonSym(GV);
       // Value for common symbols is the alignment required.
       CommonSym.Value = Align;
@@ -281,7 +318,7 @@ void ELFWriter::EmitGlobal(GlobalVariable *GV) {
     BSSSym.SetType(ELFSym::STT_OBJECT);
 
     switch (GV->getLinkage()) {
-    default:  // weak/linkonce handled above
+    default:  // weak/linkonce/common handled above
       assert(0 && "Unexpected linkage type!");
     case GlobalValue::AppendingLinkage:  // FIXME: This should be improved!
     case GlobalValue::ExternalLinkage:
@@ -294,7 +331,8 @@ void ELFWriter::EmitGlobal(GlobalVariable *GV) {
 
     // Set the idx of the .bss section
     BSSSym.SectionIdx = BSSSection.SectionIdx;
-    SymbolTable.push_back(BSSSym);
+    if (!GV->hasPrivateLinkage())
+      SymbolTable.push_back(BSSSym);
 
     // Reserve space in the .bss section for this symbol.
     BSSSection.Size += Size;
@@ -503,7 +541,7 @@ void ELFWriter::OutputSectionsAndSectionTable() {
     if (S.Align)
       for (size_t NewFileOff = (FileOff+S.Align-1) & ~(S.Align-1);
            FileOff != NewFileOff; ++FileOff)
-        O.put((char)0xAB);
+        O << (char)0xAB;
     O.write((char*)&S.SectionData[0], S.SectionData.size());
     FileOff += S.SectionData.size();
 
@@ -524,7 +562,7 @@ void ELFWriter::OutputSectionsAndSectionTable() {
   // Align output for the section table.
   for (size_t NewFileOff = (FileOff+TableAlign-1) & ~(TableAlign-1);
        FileOff != NewFileOff; ++FileOff)
-    O.put((char)0xAB);
+    O << (char)0xAB;
 
   // Emit the section table itself.
   O.write((char*)&Table[0], Table.size());