Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #ifndef ADUMP_EXCEPTION_DUMP_ELF_H
12 : #define ADUMP_EXCEPTION_DUMP_ELF_H
13 :
14 : #include <elf.h>
15 : #include <vector>
16 : #include <memory>
17 : #include <string>
18 : #include <sstream>
19 : #include <fstream>
20 : #include <map>
21 :
22 : namespace Adx {
23 : namespace ELF {
24 :
25 : #define ASCEND_SHTYPE_GLOBAL (SHT_LOUSER + 1)
26 : #define ASCEND_SHTYPE_LOCAL (SHT_LOUSER + 2)
27 : #define ASCEND_SHTYPE_REGS (SHT_LOUSER + 3)
28 : #define ASCEND_SHTYPE_DEVTBL (SHT_LOUSER + 4)
29 : #define ASCEND_SHTYPE_AUXINFO_GLOABL (SHT_LOUSER + 5)
30 : #define ASCEND_SHTYPE_AUXINFO_LOCAL (SHT_LOUSER + 6)
31 : #define ASCEND_SHTYPE_HOST_KERNEL_OBJECT (SHT_LOUSER + 7)
32 : #define ASCEND_SHTYPE_FILE_KERNEL_OBJECT (SHT_LOUSER + 8)
33 : #define ASCEND_SHTYPE_FILE_KERNEL_JSON (SHT_LOUSER + 9)
34 : #define ASCEND_SHTYPE_KERNEL_INFO (SHT_LOUSER + 10)
35 :
36 : class Section {
37 : public:
38 : explicit Section(Elf64_Word type);
39 374 : ~Section() = default;
40 :
41 : void SetData(std::string &data);
42 : void SetAddr(Elf64_Addr addr);
43 : void SetOffSet(Elf64_Off offset);
44 : void SetEntSize(Elf64_Word entSize);
45 : void SetNameIndex(Elf64_Word nameIndex);
46 : void SetLink(Elf64_Word link);
47 : void SetInfo(Elf64_Word info);
48 : void SetIndex(uint32_t index);
49 : uint32_t GetIndex() const;
50 : Elf64_Xword GetSize() const;
51 : void SetAddrAlign(Elf64_Xword addrAlign);
52 : Elf64_Xword GetAddrAlign() const;
53 : void Save(std::ofstream &ofs, std::streampos headerPosition);
54 :
55 : private:
56 : void SaveHeader(std::ofstream &ofs, std::streampos offset);
57 : void SaveData(std::ofstream &ofs);
58 :
59 : Elf64_Shdr header_{};
60 : std::string data_;
61 : uint32_t index_{0};
62 : };
63 :
64 : using SectionPtr = std::shared_ptr<Section>;
65 :
66 : class DumpELF {
67 : public:
68 : DumpELF();
69 15 : ~DumpELF() = default;
70 :
71 : SectionPtr AddSection(Elf64_Word type, const std::string &name);
72 : SectionPtr GetSectionByIndex(uint32_t index) const;
73 : void Save(const std::string &filename);
74 :
75 : private:
76 : void CreateMandatorySection();
77 : SectionPtr CreateSection(Elf64_Word type);
78 : void LayoutSections();
79 : void LayoutSectionTable();
80 : void SetSectionHeaderStringTable();
81 : bool SaveHeader(std::ofstream &ofs) const;
82 : void SaveSections(std::ofstream &ofs);
83 :
84 : Elf64_Ehdr header_{};
85 : std::vector<SectionPtr> sections_;
86 : std::stringstream sectionNameStream_;
87 : std::map<std::string, Elf64_Word> sectionNameTable_;
88 : Elf64_Off currentFilePos_{0};
89 : };
90 :
91 : } // namespace ELF
92 : } // namespace Adx
93 :
94 : #endif // ADUMP_EXCEPTION_DUMP_ELF_H
|