Index: llvm/trunk/lib/Target/RISCV/CMakeLists.txt =================================================================== --- llvm/trunk/lib/Target/RISCV/CMakeLists.txt +++ llvm/trunk/lib/Target/RISCV/CMakeLists.txt @@ -22,6 +22,7 @@ RISCVRegisterInfo.cpp RISCVSubtarget.cpp RISCVTargetMachine.cpp + RISCVTargetObjectFile.cpp ) add_subdirectory(AsmParser) Index: llvm/trunk/lib/Target/RISCV/RISCVTargetMachine.cpp =================================================================== --- llvm/trunk/lib/Target/RISCV/RISCVTargetMachine.cpp +++ llvm/trunk/lib/Target/RISCV/RISCVTargetMachine.cpp @@ -13,6 +13,7 @@ #include "RISCV.h" #include "RISCVTargetMachine.h" +#include "RISCVTargetObjectFile.h" #include "llvm/ADT/STLExtras.h" #include "llvm/CodeGen/Passes.h" #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h" @@ -59,7 +60,7 @@ : LLVMTargetMachine(T, computeDataLayout(TT), TT, CPU, FS, Options, getEffectiveRelocModel(TT, RM), getEffectiveCodeModel(CM), OL), - TLOF(make_unique()), + TLOF(make_unique()), Subtarget(TT, CPU, FS, *this) { initAsmInfo(); } Index: llvm/trunk/lib/Target/RISCV/RISCVTargetObjectFile.h =================================================================== --- llvm/trunk/lib/Target/RISCV/RISCVTargetObjectFile.h +++ llvm/trunk/lib/Target/RISCV/RISCVTargetObjectFile.h @@ -0,0 +1,25 @@ +//===-- RISCVTargetObjectFile.h - RISCV Object Info -*- C++ ---------*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIB_TARGET_RISCV_RISCVTARGETOBJECTFILE_H +#define LLVM_LIB_TARGET_RISCV_RISCVTARGETOBJECTFILE_H + +#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h" + +namespace llvm { +class RISCVTargetMachine; + +/// This implementation is used for RISCV ELF targets. +class RISCVELFTargetObjectFile : public TargetLoweringObjectFileELF { + void Initialize(MCContext &Ctx, const TargetMachine &TM) override; +}; + +} // end namespace llvm + +#endif Index: llvm/trunk/lib/Target/RISCV/RISCVTargetObjectFile.cpp =================================================================== --- llvm/trunk/lib/Target/RISCV/RISCVTargetObjectFile.cpp +++ llvm/trunk/lib/Target/RISCV/RISCVTargetObjectFile.cpp @@ -0,0 +1,19 @@ +//===-- RISCVTargetObjectFile.cpp - RISCV Object Info -----------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "RISCVTargetObjectFile.h" +#include "RISCVTargetMachine.h" + +using namespace llvm; + +void RISCVELFTargetObjectFile::Initialize(MCContext &Ctx, + const TargetMachine &TM) { + TargetLoweringObjectFileELF::Initialize(Ctx, TM); + InitializeELF(TM.Options.UseInitArray); +} Index: llvm/trunk/test/CodeGen/RISCV/init-array.ll =================================================================== --- llvm/trunk/test/CodeGen/RISCV/init-array.ll +++ llvm/trunk/test/CodeGen/RISCV/init-array.ll @@ -0,0 +1,30 @@ +; RUN: llc -mtriple=riscv32-unknown-linux-gnu -verify-machineinstrs -o - %s \ +; RUN: | FileCheck --check-prefix=INITARRAY %s +; RUN: llc -mtriple=riscv32-unknown-elf -verify-machineinstrs -o - %s \ +; RUN: | FileCheck --check-prefix=INITARRAY %s +; RUN: llc -mtriple=riscv64-unknown-linux-gnu -verify-machineinstrs -o - %s \ +; RUN: | FileCheck --check-prefix=INITARRAY %s +; RUN: llc -mtriple=riscv64-unknown-elf -verify-machineinstrs -o - %s \ +; RUN: | FileCheck --check-prefix=INITARRAY %s + +; RUN: llc -mtriple=riscv32-unknown-linux-gnu -verify-machineinstrs -use-ctors -o - %s \ +; RUN: | FileCheck --check-prefix=CTOR %s +; RUN: llc -mtriple=riscv32-unknown-elf -verify-machineinstrs -use-ctors -o - %s \ +; RUN: | FileCheck --check-prefix=CTOR %s +; RUN: llc -mtriple=riscv64-unknown-linux-gnu -verify-machineinstrs -use-ctors -o - %s \ +; RUN: | FileCheck --check-prefix=CTOR %s +; RUN: llc -mtriple=riscv64-unknown-elf -verify-machineinstrs -use-ctors -o - %s \ +; RUN: | FileCheck --check-prefix=CTOR %s + +define internal void @_GLOBAL__I_a() section ".text.startup" { + ret void +} + +@llvm.global_ctors = appending global [1 x { i32, void ()* }] [{ i32, void ()* } { i32 65535, void ()* @_GLOBAL__I_a }] + +;INITARRAY: section .init_array +;INITARRAY-NOT: .section .ctors + +;CTOR: .section .ctors +;CTOR-NOT: section .init_array +