23 #ifndef ELFIO_NOTE_HPP
24 #define ELFIO_NOTE_HPP
45 : elf_file(elf_file_), note_section(section_) {
50 Elf_Word get_notes_num()
const {
return (Elf_Word)note_start_positions.size(); }
53 bool get_note(Elf_Word index, Elf_Word& type, std::string& name,
void*& desc,
54 Elf_Word& descSize)
const {
55 if (index >= note_section->get_size()) {
59 const char* pData = note_section->get_data() + note_start_positions[index];
60 int align =
sizeof(Elf_Word);
63 type = convertor(*(Elf_Word*)(pData + 2 * align));
64 Elf_Word namesz = convertor(*(Elf_Word*)(pData));
65 descSize = convertor(*(Elf_Word*)(pData +
sizeof(namesz)));
66 Elf_Word max_name_size = note_section->get_size() - note_start_positions[index];
67 if (namesz > max_name_size || namesz + descSize > max_name_size) {
70 name.assign(pData + 3 * align, namesz - 1);
74 desc =
const_cast<char*
>(pData + 3 * align + ((namesz + align - 1) / align) * align);
81 void add_note(Elf_Word type,
const std::string& name,
const void* desc, Elf_Word descSize) {
84 int align =
sizeof(Elf_Word);
85 Elf_Word nameLen = (Elf_Word)name.size() + 1;
86 Elf_Word nameLenConv = convertor(nameLen);
87 std::string buffer(
reinterpret_cast<char*
>(&nameLenConv), align);
88 Elf_Word descSizeConv = convertor(descSize);
89 buffer.append(
reinterpret_cast<char*
>(&descSizeConv), align);
90 type = convertor(type);
91 buffer.append(
reinterpret_cast<char*
>(&type), align);
93 buffer.append(1,
'\x00');
94 const char pad[] = {
'\0',
'\0',
'\0',
'\0'};
95 if (nameLen % align != 0) {
96 buffer.append(pad, align - nameLen % align);
98 if (desc != 0 && descSize != 0) {
99 buffer.append(
reinterpret_cast<const char*
>(desc), descSize);
100 if (descSize % align != 0) {
101 buffer.append(pad, align - descSize % align);
105 note_start_positions.push_back(note_section->get_size());
106 note_section->append_data(buffer);
111 void process_section() {
113 const char* data = note_section->get_data();
114 Elf_Xword size = note_section->get_size();
115 Elf_Xword current = 0;
117 note_start_positions.clear();
120 if (0 == data || 0 == size) {
124 int align =
sizeof(Elf_Word);
125 while (current + 3 * align <= size) {
126 note_start_positions.push_back(current);
127 Elf_Word namesz = convertor(*(Elf_Word*)(data + current));
128 Elf_Word descsz = convertor(*(Elf_Word*)(data + current +
sizeof(namesz)));
130 current += 3 *
sizeof(Elf_Word) + ((namesz + align - 1) / align) * align +
131 ((descsz + align - 1) / align) * align;
137 const elfio& elf_file;
139 std::vector<Elf_Xword> note_start_positions;
144 #endif // ELFIO_NOTE_HPP