Index: lib/Target/RISCV/CMakeLists.txt =================================================================== --- lib/Target/RISCV/CMakeLists.txt +++ lib/Target/RISCV/CMakeLists.txt @@ -22,6 +22,7 @@ RISCVRegisterInfo.cpp RISCVSubtarget.cpp RISCVTargetMachine.cpp + RISCVTargetObjectFile.cpp ) add_subdirectory(AsmParser) Index: lib/Target/RISCV/RISCVTargetMachine.cpp =================================================================== --- lib/Target/RISCV/RISCVTargetMachine.cpp +++ 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: lib/Target/RISCV/RISCVTargetObjectFile.h =================================================================== --- /dev/null +++ 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: lib/Target/RISCV/RISCVTargetObjectFile.cpp =================================================================== --- /dev/null +++ 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: test/CodeGen/RISCV/init-array.ll =================================================================== --- /dev/null +++ test/CodeGen/RISCV/init-array.ll @@ -0,0 +1,16 @@ +; RUN: llc -mtriple=riscv32-unknown-linux-gnu -verify-machineinstrs -o - %s \ +; RUN: | FileCheck %s +; RUN: llc -mtriple=riscv64-unknown-linux-gnu -verify-machineinstrs -o - %s \ +; RUN: | FileCheck %s +; RUN: llc -mtriple=riscv32-unknown-elf -verify-machineinstrs -o - %s \ +; RUN: | FileCheck %s +; RUN: llc -mtriple=riscv64-unknown-elf -verify-machineinstrs -o - %s \ +; RUN: | FileCheck %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 }] + +; CHECK: .section .init_array