Skip to content

Commit b7f0754

Browse files
Xclbin to ELF flow migration for aie2 (#81)
Signed-off-by: HimanshuChoudhary-Xilinx <Himanshu.Choudhary@amd.com>
1 parent 3c438bb commit b7f0754

21 files changed

Lines changed: 518 additions & 70 deletions

src/cpp/assembler/aiebu_assembler.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ aiebu_assembler(buffer_type type,
5656
aiebu::assembler a(assembler::elf_type::aie2ps_asm);
5757
elf_data = a.process(buffer1, libs, libpaths, patch_json);
5858
}
59+
else if (type == buffer_type::config)
60+
{
61+
aiebu::assembler a(assembler::elf_type::config);
62+
elf_data = a.process(buffer1, libs, libpaths, patch_json, buffer2);
63+
}
5964
else {
6065
throw error(error::error_code::invalid_buffer_type, "Buffer_type not supported !!!");
6166
}

src/cpp/assembler/assembler.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include "elfwriter.h"
1313
#include "encoder.h"
1414
#include "preprocessor.h"
15+
#include "elf/config/config_elfwriter.h"
16+
#include "config_preprocessor.h"
1517

1618
#include "aiebu/aiebu_error.h"
1719

@@ -48,6 +50,12 @@ assembler(const elf_type type)
4850
m_elfwriter = std::make_unique<aie2ps_elf_writer>();
4951
m_ppi = std::make_shared<aie2ps_preprocessor_input>();
5052
}
53+
else if (type == elf_type::config) {
54+
m_preprocessor = std::make_unique<config_preprocessor>();
55+
m_enoder = std::make_unique<aie2_blob_encoder>();
56+
m_elfwriter = std::make_unique<config_elf_writer>();
57+
m_ppi = std::make_shared<config_preprocessor_input>();
58+
}
5159
else {
5260
throw error(error::error_code::invalid_buffer_type ,"Invalid elf type!!!");
5361
}

src/cpp/assembler/assembler.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// SPDX-License-Identifier: MIT
2-
// Copyright (C) 2024, Advanced Micro Devices, Inc. All rights reserved.
2+
// Copyright (C) 2024-2025, Advanced Micro Devices, Inc. All rights reserved.
33

44
#ifndef _ADSM_COMMOM_ASSEMBLER_H_
55
#define _ADSM_COMMOM_ASSEMBLER_H_
@@ -29,7 +29,8 @@ class assembler
2929
aie2_transaction_blob,
3030
aie2_dpu_blob,
3131
aie2ps_asm,
32-
aie2_asm
32+
aie2_asm,
33+
config
3334
};
3435

3536
explicit assembler(const elf_type type);

src/cpp/common/file_utils.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ readfile(const std::string& filename)
2222

2323
std::ifstream input(filename, std::ios::in | std::ios::binary);
2424
auto file_size = std::filesystem::file_size(filename);
25+
26+
if (!file_size)
27+
throw error(error::error_code::invalid_asm, "filename " + filename + " is empty!!");
28+
2529
std::vector<char> buffer(file_size);
2630
input.read(buffer.data(), static_cast<std::streamsize>(file_size));
2731
return buffer;

src/cpp/common/symbol.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// SPDX-License-Identifier: MIT
2-
// Copyright (C) 2024, Advanced Micro Devices, Inc. All rights reserved.
2+
// Copyright (C) 2024-2025, Advanced Micro Devices, Inc. All rights reserved.
33

44
#ifndef _AIEBU_COMMOM_SYMBOL_H_
55
#define _AIEBU_COMMOM_SYMBOL_H_
@@ -22,7 +22,8 @@ class symbol
2222
shim_dma_48 = 5,
2323
shim_dma_57_aie4 = 6,
2424
control_packet_57 = 7,
25-
unknown = 8,
25+
address_64 = 8,
26+
unknown = 9,
2627
};
2728

2829
private:

src/cpp/common/writer.h

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
// SPDX-License-Identifier: MIT
2-
// Copyright (C) 2024, Advanced Micro Devices, Inc. All rights reserved.
2+
// Copyright (C) 2024-2025, Advanced Micro Devices, Inc. All rights reserved.
33

44
#ifndef _AIEBU_COMMON_WRITER_H_
55
#define _AIEBU_COMMON_WRITER_H_
66

77
#include <map>
88
#include <string>
99
#include <vector>
10+
#include <unordered_map>
1011
#include "symbol.h"
1112
#include "code_section.h"
1213

@@ -19,10 +20,14 @@ class writer
1920
const code_section m_type;
2021
std::vector<uint8_t> m_data;
2122
std::vector<symbol> m_symbols;
23+
std::unordered_map<std::string, std::string> m_metadata;
2224

2325
public:
24-
writer(const std::string name, code_section type, std::vector<uint8_t>& data): m_name(name), m_type(type), m_data(std::move(data)) {}
25-
writer(const std::string name, code_section type): m_name(name), m_type(type) {}
26+
writer(std::string name, code_section type, std::vector<uint8_t>&& data)
27+
: m_name(std::move(name)),
28+
m_type(type),
29+
m_data(std::move(data)) {}
30+
writer(std::string name, code_section type): m_name(std::move(name)), m_type(type) {}
2631
virtual ~writer() = default;
2732

2833
virtual void write_byte(uint8_t byte);
@@ -80,6 +85,17 @@ class writer
8085
}
8186

8287
void padding(offset_type size);
88+
89+
void add_metadata(std::unordered_map<std::string, std::string>&& metadata)
90+
{
91+
m_metadata = std::move(metadata);
92+
}
93+
94+
std::string&
95+
get_metadata(const std::string& key)
96+
{
97+
return m_metadata[key];
98+
}
8399
};
84100
}
85101
#endif //_AIEBU_COMMON_WRITER_H_

src/cpp/elf/aie2ps/aie2ps_elfwriter.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// SPDX-License-Identifier: MIT
2-
// Copyright (C) 2024, Advanced Micro Devices, Inc. All rights reserved.
2+
// Copyright (C) 2024-2025, Advanced Micro Devices, Inc. All rights reserved.
33

44
#ifndef _AIEBU_ELF_AIE2PS_ELF_WRITER_H_
55
#define _AIEBU_ELF_AIE2PS_ELF_WRITER_H_
@@ -10,8 +10,8 @@ namespace aiebu {
1010

1111
class aie2ps_elf_writer: public elf_writer
1212
{
13-
constexpr static int ob_abi = 0x40;
14-
constexpr static int version = 0x02;
13+
constexpr static unsigned char ob_abi = 0x40;
14+
constexpr static unsigned char version = 0x02;
1515
public:
1616
aie2ps_elf_writer(): elf_writer(ob_abi, version)
1717
{ }
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// SPDX-License-Identifier: MIT
2+
// Copyright (C) 2025, Advanced Micro Devices, Inc. All rights reserved.
3+
4+
#ifndef AIEBU_ELF_CONFIG_ELF_WRITER_H_
5+
#define AIEBU_ELF_CONFIG_ELF_WRITER_H_
6+
7+
#include <elfwriter.h>
8+
9+
namespace aiebu {
10+
11+
class config_elf_writer: public elf_writer
12+
{
13+
constexpr static unsigned char ob_abi = 0x46;
14+
constexpr static unsigned char version = 0x02;
15+
const std::string const_configuration = "configuration";
16+
const std::string xrt_configuration = ".note.xrt.configuration";
17+
const std::string const_kernel_signature = "kernel.signature";
18+
19+
public:
20+
config_elf_writer(): elf_writer(ob_abi, version)
21+
{ }
22+
23+
std::vector<char>
24+
process(std::vector<writer>& mwriter) override
25+
{
26+
process_common_helper(mwriter);
27+
//std::string uuid = mwriter[0].get_metadata("kernel.config.uuid");
28+
//if (!uuid.empty())
29+
// add_note(NT_XRT_UUID, ".note.xrt.kernel.config.uuid", uuid);
30+
31+
const std::string configuration = mwriter[0].get_metadata(const_configuration);
32+
std::vector<char> configuration_vec(configuration.begin(), configuration.end());
33+
if (!configuration.empty())
34+
add_note(NT_XRT_PARTITION_SIZE, xrt_configuration, configuration_vec);
35+
36+
std::string kernel_signature = mwriter[0].get_metadata(const_kernel_signature);
37+
if (!kernel_signature.empty())
38+
add_symtab(kernel_signature);
39+
40+
return finalize();
41+
}
42+
};
43+
44+
}
45+
#endif //AIEBU_ELF_CONFIG_ELF_WRITER_H_

src/cpp/elf/elfwriter.cpp

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// SPDX-License-Identifier: MIT
2-
// Copyright (C) 2024, Advanced Micro Devices, Inc. All rights reserved.
2+
// Copyright (C) 2024-2025, Advanced Micro Devices, Inc. All rights reserved.
33

44
#include "elfwriter.h"
55

@@ -219,9 +219,33 @@ add_text_data_section(const std::vector<writer>& mwriter, std::vector<symbol>& s
219219
}
220220
}
221221

222-
std::vector<char>
222+
void
223223
elf_writer::
224-
process(std::vector<writer>& mwriter)
224+
add_symtab(const std::string& name)
225+
{
226+
std::call_once(symtab_flag, [this] {
227+
str_sec = m_elfio.sections.add(".strtab");
228+
str_sec->set_type(ELFIO::SHT_STRTAB);
229+
str_sec->set_entry_size(0);
230+
sym_sec = m_elfio.sections.add(".symtab");
231+
sym_sec->set_type(ELFIO::SHT_SYMTAB);
232+
sym_sec->set_info(1);
233+
sym_sec->set_addr_align(0x4);
234+
sym_sec->set_entry_size(m_elfio.get_default_entry_size(ELFIO::SHT_SYMTAB));
235+
sym_sec->set_link(str_sec->get_index());
236+
});
237+
238+
ELFIO::string_section_accessor stra(str_sec);
239+
// Create symbol table writer
240+
ELFIO::symbol_section_accessor syma( m_elfio, sym_sec );
241+
// Another way to add symbol
242+
syma.add_symbol( stra, name.c_str(), 0x00000000, 0, ELFIO::STB_WEAK, ELFIO::STT_FUNC, 0,
243+
ELFIO::SHN_UNDEF );
244+
}
245+
246+
void
247+
elf_writer::
248+
process_common_helper(std::vector<writer>& mwriter)
225249
{
226250
// add sections
227251
std::vector<symbol> syms;
@@ -233,6 +257,13 @@ process(std::vector<writer>& mwriter)
233257
add_reldyn_section(syms);
234258
add_dynamic_section_segment();
235259
}
260+
}
261+
262+
std::vector<char>
263+
elf_writer::
264+
process(std::vector<writer>& mwriter)
265+
{
266+
process_common_helper(mwriter);
236267
return finalize();
237268
}
238269

src/cpp/elf/elfwriter.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
// SPDX-License-Identifier: MIT
2-
// Copyright (C) 2024, Advanced Micro Devices, Inc. All rights reserved.
2+
// Copyright (C) 2024-2025, Advanced Micro Devices, Inc. All rights reserved.
33

44
#ifndef _AIEBU_ELF_ELF_WRITER_H_
55
#define _AIEBU_ELF_ELF_WRITER_H_
66

77
#include <sstream>
88
#include <iterator>
9+
#include <mutex>
910
#include "writer.h"
1011
#include "symbol.h"
1112
#include "elfio/elfio.hpp"
@@ -21,6 +22,8 @@ constexpr int program_header_static_count = 2;
2122
constexpr int program_header_dynamic_count = 3;
2223

2324
constexpr ELFIO::Elf_Word NT_XRT_UID = 4;
25+
constexpr ELFIO::Elf_Word NT_XRT_UUID = 5;
26+
constexpr ELFIO::Elf_Word NT_XRT_PARTITION_SIZE = 6;
2427

2528
class elf_section
2629
{
@@ -69,6 +72,9 @@ class elf_writer
6972
{
7073
protected:
7174
ELFIO::elfio m_elfio;
75+
ELFIO::section* str_sec = nullptr;
76+
ELFIO::section* sym_sec = nullptr;
77+
std::once_flag symtab_flag;
7278
uid_md5 m_uid;
7379

7480
ELFIO::section* add_section(const elf_section& data);
@@ -80,7 +86,8 @@ class elf_writer
8086
std::vector<char> finalize();
8187
void add_text_data_section(const std::vector<writer>& mwriter, std::vector<symbol>& syms);
8288
void add_note(ELFIO::Elf_Word type, const std::string& name, const std::vector<char>& dec);
83-
89+
void add_symtab(const std::string& name);
90+
void process_common_helper(std::vector<writer>& mwriter);
8491
public:
8592

8693
elf_writer(unsigned char abi, unsigned char version)
@@ -103,7 +110,7 @@ class elf_writer
103110

104111
}
105112

106-
std::vector<char> process(std::vector<writer>& mwriter);
113+
virtual std::vector<char> process(std::vector<writer>& mwriter);
107114

108115
virtual ~elf_writer() = default;
109116

0 commit comments

Comments
 (0)