162 struct SectionFlags {
167 bool has_relocs =
true;
177 std::array<SectionFlags, unsigned(SectionKind::Max)> section_flags;
181 const TargetInfo &target_info;
183 util::BumpAllocator<> section_allocator;
184 util::SmallVector<util::BumpAllocUniquePtr<DataSection>, 16> sections;
186 std::array<SecRef, unsigned(SectionKind::Max)> default_sections;
188 Assembler(
const TargetInfo &target_info) noexcept
189 : target_info(target_info) {}
190 virtual ~Assembler();
193 virtual void reset() noexcept;
198 DataSection &get_section(SecRef ref) noexcept {
200 return *sections[ref.id()];
203 const DataSection &get_section(SecRef ref)
const noexcept {
205 return *sections[ref.id()];
208 SecRef create_section(
const TargetInfo::SectionFlags &flags)
noexcept;
210 SecRef create_section(SectionKind kind)
noexcept {
211 return create_section(target_info.section_flags[
unsigned(kind)]);
214 SecRef get_default_section(SectionKind kind)
noexcept {
215 SecRef &res = default_sections[unsigned(kind)];
217 res = create_section(kind);
222 SecRef get_text_section() noexcept {
223 return get_default_section(SectionKind::Text);
225 SecRef get_data_section(
bool rodata,
bool relro =
false) noexcept {
226 return get_default_section(!rodata ? SectionKind::Data
227 : relro ? SectionKind::DataRelRO
228 : SectionKind::ReadOnly);
230 SecRef get_bss_section() noexcept {
231 return get_default_section(SectionKind::BSS);
233 SecRef get_tdata_section() noexcept {
234 return get_default_section(SectionKind::ThreadData);
236 SecRef get_tbss_section() noexcept {
237 return get_default_section(SectionKind::ThreadBSS);
240 virtual void rename_section(SecRef, std::string_view name)
noexcept = 0;
242 virtual SymRef section_symbol(SecRef)
noexcept = 0;
246 virtual SymRef sym_add_undef(std::string_view,
SymBinding)
noexcept = 0;
247 virtual SymRef sym_predef_func(std::string_view,
SymBinding)
noexcept = 0;
248 virtual SymRef sym_predef_data(std::string_view,
SymBinding)
noexcept = 0;
249 virtual SymRef sym_predef_tls(std::string_view,
SymBinding)
noexcept = 0;
251 virtual void sym_def(SymRef, SecRef, u64 pos, u64 size)
noexcept = 0;
256 std::span<const u8> data,
260 [[nodiscard]] SymRef sym_def_data(SecRef sec,
261 std::string_view name,
262 std::span<const u8> data,
265 u32 *off =
nullptr) {
266 SymRef sym = sym_predef_data(name, binding);
276 u32 *off =
nullptr) noexcept;
284 SecRef sec, SymRef sym, u32 type, u32 offset, i64 addend) noexcept {
285 assert(i32(addend) == addend &&
"non-32-bit addends are unsupported");
286 get_section(sec).relocs.emplace_back(offset, sym, type, addend);
289 void reloc_pc32(SecRef sec, SymRef sym, u32 offset, i64 addend)
noexcept {
290 reloc_sec(sec, sym, target_info.reloc_pc32, offset, addend);
293 void reloc_abs(SecRef sec, SymRef sym, u32 offset, i64 addend)
noexcept {
294 reloc_sec(sec, sym, target_info.reloc_abs64, offset, addend);
299 virtual void finalize() noexcept {}
301 virtual std::vector<u8> build_object_file() noexcept = 0;