1*0b57cec5SDimitry Andric //===-- AVRTargetObjectFile.cpp - AVR Object Files ------------------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric 
9*0b57cec5SDimitry Andric #include "AVRTargetObjectFile.h"
10*0b57cec5SDimitry Andric #include "AVRTargetMachine.h"
11*0b57cec5SDimitry Andric 
12*0b57cec5SDimitry Andric #include "llvm/BinaryFormat/ELF.h"
13*0b57cec5SDimitry Andric #include "llvm/IR/DerivedTypes.h"
14*0b57cec5SDimitry Andric #include "llvm/IR/GlobalValue.h"
15*0b57cec5SDimitry Andric #include "llvm/IR/Mangler.h"
16*0b57cec5SDimitry Andric #include "llvm/MC/MCContext.h"
17*0b57cec5SDimitry Andric #include "llvm/MC/MCSectionELF.h"
18*0b57cec5SDimitry Andric 
19*0b57cec5SDimitry Andric #include "AVR.h"
20*0b57cec5SDimitry Andric 
21*0b57cec5SDimitry Andric namespace llvm {
Initialize(MCContext & Ctx,const TargetMachine & TM)22*0b57cec5SDimitry Andric void AVRTargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &TM) {
23*0b57cec5SDimitry Andric   Base::Initialize(Ctx, TM);
24*0b57cec5SDimitry Andric   ProgmemDataSection =
25*0b57cec5SDimitry Andric       Ctx.getELFSection(".progmem.data", ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
26*0b57cec5SDimitry Andric   Progmem1DataSection =
27*0b57cec5SDimitry Andric       Ctx.getELFSection(".progmem1.data", ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
28*0b57cec5SDimitry Andric   Progmem2DataSection =
29*0b57cec5SDimitry Andric       Ctx.getELFSection(".progmem2.data", ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
30*0b57cec5SDimitry Andric   Progmem3DataSection =
31*0b57cec5SDimitry Andric       Ctx.getELFSection(".progmem3.data", ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
32*0b57cec5SDimitry Andric   Progmem4DataSection =
33*0b57cec5SDimitry Andric       Ctx.getELFSection(".progmem4.data", ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
34*0b57cec5SDimitry Andric   Progmem5DataSection =
35*0b57cec5SDimitry Andric       Ctx.getELFSection(".progmem5.data", ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
36*0b57cec5SDimitry Andric }
37*0b57cec5SDimitry Andric 
SelectSectionForGlobal(const GlobalObject * GO,SectionKind Kind,const TargetMachine & TM) const38*0b57cec5SDimitry Andric MCSection *AVRTargetObjectFile::SelectSectionForGlobal(
39*0b57cec5SDimitry Andric     const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
40*0b57cec5SDimitry Andric   // Global values in flash memory are placed in the progmem*.data section
41   // unless they already have a user assigned section.
42   const auto &AVRTM = static_cast<const AVRTargetMachine &>(TM);
43   if (AVR::isProgramMemoryAddress(GO) && !GO->hasSection() &&
44       Kind.isReadOnly()) {
45     // The AVR subtarget should support LPM to access section '.progmem*.data'.
46     if (!AVRTM.getSubtargetImpl()->hasLPM()) {
47       // TODO: Get the global object's location in source file.
48       getContext().reportError(
49           SMLoc(),
50           "Current AVR subtarget does not support accessing program memory");
51       return Base::SelectSectionForGlobal(GO, Kind, TM);
52     }
53     // The AVR subtarget should support ELPM to access section
54     // '.progmem[1|2|3|4|5].data'.
55     if (!AVRTM.getSubtargetImpl()->hasELPM() &&
56         AVR::getAddressSpace(GO) != AVR::ProgramMemory) {
57       // TODO: Get the global object's location in source file.
58       getContext().reportError(SMLoc(),
59                                "Current AVR subtarget does not support "
60                                "accessing extended program memory");
61       return ProgmemDataSection;
62     }
63     switch (AVR::getAddressSpace(GO)) {
64     case AVR::ProgramMemory: // address space 1
65       return ProgmemDataSection;
66     case AVR::ProgramMemory1: // address space 2
67       return Progmem1DataSection;
68     case AVR::ProgramMemory2: // address space 3
69       return Progmem2DataSection;
70     case AVR::ProgramMemory3: // address space 4
71       return Progmem3DataSection;
72     case AVR::ProgramMemory4: // address space 5
73       return Progmem4DataSection;
74     case AVR::ProgramMemory5: // address space 6
75       return Progmem5DataSection;
76     default:
77       llvm_unreachable("unexpected program memory index");
78     }
79   }
80 
81   // Otherwise, we work the same way as ELF.
82   return Base::SelectSectionForGlobal(GO, Kind, TM);
83 }
84 } // end of namespace llvm
85