• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "llvm_aot_builder.h"
17 
18 namespace ark::compiler {
19 
GetSectionsAddresses(const std::string & cmdline,const std::string & fileName)20 std::unordered_map<std::string, size_t> LLVMAotBuilder::GetSectionsAddresses(const std::string &cmdline,
21                                                                              const std::string &fileName)
22 {
23     switch (arch_) {
24         case Arch::AARCH32:
25             return GetSectionsAddressesImpl<Arch::AARCH32>(cmdline, fileName);
26         case Arch::AARCH64:
27             return GetSectionsAddressesImpl<Arch::AARCH64>(cmdline, fileName);
28         case Arch::X86:
29             return GetSectionsAddressesImpl<Arch::X86>(cmdline, fileName);
30         case Arch::X86_64:
31             return GetSectionsAddressesImpl<Arch::X86_64>(cmdline, fileName);
32         default:
33             LOG(ERROR, COMPILER) << "LLVMAotBuilder: Unsupported arch";
34             UNREACHABLE();
35     }
36 }
37 
38 template <Arch ARCH>
GetSectionsAddressesImpl(const std::string & cmdline,const std::string & fileName)39 std::unordered_map<std::string, size_t> LLVMAotBuilder::GetSectionsAddressesImpl(const std::string &cmdline,
40                                                                                  const std::string &fileName)
41 {
42     ElfBuilder<ARCH> builder;
43     // stringTable_ is the only field modified by PrepareElfBuilder not idempotently
44     auto oldStringTableSize = stringTable_.size();
45 
46     PrepareElfBuilder(builder, cmdline, fileName);
47     builder.Build(fileName);
48 
49     stringTable_.resize(oldStringTableSize);
50 
51     auto textSection = builder.GetTextSection();
52     auto roDataSections = builder.GetRoDataSections();
53     auto aotSection = builder.GetAotSection();
54     auto gotSection = builder.GetGotSection();
55 
56     static constexpr auto FIRST_ENTRYPOINT_OFFSET =
57         static_cast<int32_t>(RuntimeInterface::IntrinsicId::COUNT) * PointerSize(ARCH);
58 
59     std::unordered_map<std::string, size_t> sectionAddresses {
60         {textSection->GetName(), textSection->GetOffset()},
61         {aotSection->GetName(), aotSection->GetOffset()},
62         // At runtime the .text section is placed right after the .aot_got section
63         // without any padding, so we must use the first entrypoint address as a start
64         // of the .aot_got section.
65         {gotSection->GetName(), textSection->GetOffset() - FIRST_ENTRYPOINT_OFFSET}};
66     for (auto section : *roDataSections) {
67         sectionAddresses.emplace(section->GetName(), section->GetOffset());
68     }
69     return sectionAddresses;
70 }
71 
72 }  // namespace ark::compiler
73