| 1 |
// SPDX-License-Identifier: MPL-2.0 |
| 2 |
// (c) Hare authors <https://harelang.org> |
| 3 |
|
| 4 |
use debug::image; |
| 5 |
use errors; |
| 6 |
use format::elf; |
| 7 |
use io; |
| 8 |
|
| 9 |
export type abbrev_table = struct { |
| 10 |
items: []abbrev, |
| 11 |
}; |
| 12 |
|
| 13 |
// A single abbreviated tag from a .debug_abbrev section. |
| 14 |
export type abbrev = struct { |
| 15 |
code: u64, |
| 16 |
tag: u32, |
| 17 |
has_children: bool, |
| 18 |
fields: []afield, |
| 19 |
}; |
| 20 |
|
| 21 |
// A field in a .debug_abbrev section |
| 22 |
export type afield = struct { |
| 23 |
attr: u32, |
| 24 |
form: u32, |
| 25 |
}; |
| 26 |
|
| 27 |
// Loads an abbreviation table from the .debug_abbrev section, loading the table |
| 28 |
// at the provided offset from the start of the ELF section. |
| 29 |
// |
| 30 |
// Pass the result to [[abbrev_table_finish]] to free resources associated with |
| 31 |
// the table when you're done with it. |
| 32 |
export fn load_abbrevs( |
| 33 |
image: *image::image, |
| 34 |
offs: u64, |
| 35 |
) (abbrev_table | void | errors::invalid | nomem) = { |
| 36 |
const sec = match (image::section_byname(image, ".debug_abbrev")) { |
| 37 |
case let sec: *elf::section64 => |
| 38 |
yield sec; |
| 39 |
case null => |
| 40 |
return; |
| 41 |
}; |
| 42 |
|
| 43 |
const rd = image::section_reader(image, sec); |
| 44 |
io::seek(&rd, offs: io::off, io::whence::SET)!; |
| 45 |
const rd = new_table_reader(&rd, false)! as table_reader; |
| 46 |
|
| 47 |
let abbrevs: []abbrev = []; |
| 48 |
let ok = false; |
| 49 |
defer if (!ok) { |
| 50 |
free(abbrevs); |
| 51 |
}; |
| 52 |
|
| 53 |
for (true) { |
| 54 |
match (read_abbrev(&rd)) { |
| 55 |
case io::EOF => break; |
| 56 |
case io::error => return errors::invalid; |
| 57 |
case nomem => return nomem; |
| 58 |
case let ab: abbrev => |
| 59 |
append(abbrevs, ab)?; |
| 60 |
}; |
| 61 |
}; |
| 62 |
|
| 63 |
ok = true; |
| 64 |
return abbrev_table { |
| 65 |
items = abbrevs, |
| 66 |
}; |
| 67 |
}; |
| 68 |
|
| 69 |
// Reads an entry from an abbreviation table. |
| 70 |
fn read_abbrev( |
| 71 |
rd: *table_reader, |
| 72 |
) (abbrev | io::EOF | io::error | nomem) = { |
| 73 |
const code = read_uleb128(rd)?; |
| 74 |
if (code == 0) { |
| 75 |
return io::EOF; |
| 76 |
}; |
| 77 |
const tag = read_uleb128(rd)?; |
| 78 |
const children = read_ubyte(rd)? != 0; |
| 79 |
|
| 80 |
let fields: []afield = []; |
| 81 |
let ok = false; |
| 82 |
defer if (!ok) { |
| 83 |
free(fields); |
| 84 |
}; |
| 85 |
|
| 86 |
for (true) { |
| 87 |
const name = read_uleb128(rd)?; |
| 88 |
const form = read_uleb128(rd)?; |
| 89 |
if (name == 0 && form == 0) { |
| 90 |
break; |
| 91 |
}; |
| 92 |
append(fields, afield { |
| 93 |
attr = name: u32, |
| 94 |
form = form: u32, |
| 95 |
})?; |
| 96 |
}; |
| 97 |
|
| 98 |
ok = true; |
| 99 |
|
| 100 |
return abbrev { |
| 101 |
code = code, |
| 102 |
tag = tag: u32, |
| 103 |
has_children = children, |
| 104 |
fields = fields, |
| 105 |
}; |
| 106 |
}; |
| 107 |
|
| 108 |
// Frees resources associated with an [[abbrev_table]]. |
| 109 |
export fn abbrev_table_finish(table: *abbrev_table) void = { |
| 110 |
for (let i = 0z; i < len(table.items); i += 1) { |
| 111 |
free(table.items[i].fields); |
| 112 |
}; |
| 113 |
free(table.items); |
| 114 |
}; |
| 115 |
|
| 116 |
// Retrieves an abbreviation from an [[abbrev_table]] by its abbreviation code. |
| 117 |
export fn get_abbrev(table: *abbrev_table, code: u64) const nullable *abbrev = { |
| 118 |
// TODO: Sort the list and do this faster |
| 119 |
for (let item &.. table.items) { |
| 120 |
if (item.code == code) { |
| 121 |
return item; |
| 122 |
}; |
| 123 |
}; |
| 124 |
return null; |
| 125 |
}; |