2021-11-21 23:59:44 +00:00
|
|
|
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
2022-11-02 00:05:16 +00:00
|
|
|
# SPDX-License-Identifier: LGPL-2.1-or-later
|
2020-05-15 23:13:02 +01:00
|
|
|
|
2023-07-19 09:00:06 +01:00
|
|
|
from collections import OrderedDict
|
Rewrite drgn core in C
The current mixed Python/C implementation works well, but it has a
couple of important limitations:
- It's too slow for some common use cases, like iterating over large
data structures.
- It can't be reused in utilities written in other languages.
This replaces the internals with a new library written in C, libdrgn. It
includes Python bindings with mostly the same public interface as
before, with some important improvements:
- Types are now represented by a single Type class rather than the messy
polymorphism in the Python implementation.
- Qualifiers are a bitmask instead of a set of strings.
- Bit fields are not considered a separate type.
- The lvalue/rvalue terminology is replaced with reference/value.
- Structure, union, and array values are better supported.
- Function objects are supported.
- Program distinguishes between lookups of variables, constants, and
functions.
The C rewrite is about 6x as fast as the original Python when using the
Python bindings, and about 8x when using the C API directly.
Currently, the exposed API in C is fairly conservative. In the future,
the memory reader, type index, and object index APIs will probably be
exposed for more flexibility.
2019-03-22 23:27:46 +00:00
|
|
|
import os.path
|
2022-07-15 23:02:08 +01:00
|
|
|
from typing import Any, NamedTuple, Optional, Sequence, Union
|
2023-06-16 21:51:48 +01:00
|
|
|
import zlib
|
Rewrite drgn core in C
The current mixed Python/C implementation works well, but it has a
couple of important limitations:
- It's too slow for some common use cases, like iterating over large
data structures.
- It can't be reused in utilities written in other languages.
This replaces the internals with a new library written in C, libdrgn. It
includes Python bindings with mostly the same public interface as
before, with some important improvements:
- Types are now represented by a single Type class rather than the messy
polymorphism in the Python implementation.
- Qualifiers are a bitmask instead of a set of strings.
- Bit fields are not considered a separate type.
- The lvalue/rvalue terminology is replaced with reference/value.
- Structure, union, and array values are better supported.
- Function objects are supported.
- Program distinguishes between lookups of variables, constants, and
functions.
The C rewrite is about 6x as fast as the original Python when using the
Python bindings, and about 8x when using the C API directly.
Currently, the exposed API in C is fairly conservative. In the future,
the memory reader, type index, and object index APIs will probably be
exposed for more flexibility.
2019-03-22 23:27:46 +00:00
|
|
|
|
2021-06-02 21:17:22 +01:00
|
|
|
from tests.assembler import _append_sleb128, _append_uleb128
|
2023-07-19 09:00:06 +01:00
|
|
|
from tests.dwarf import DW_AT, DW_FORM, DW_LNCT, DW_TAG, DW_UT
|
2022-05-17 23:14:50 +01:00
|
|
|
from tests.elf import ET, SHT
|
2019-05-10 07:53:16 +01:00
|
|
|
from tests.elfwriter import ElfSection, create_elf_file
|
Rewrite drgn core in C
The current mixed Python/C implementation works well, but it has a
couple of important limitations:
- It's too slow for some common use cases, like iterating over large
data structures.
- It can't be reused in utilities written in other languages.
This replaces the internals with a new library written in C, libdrgn. It
includes Python bindings with mostly the same public interface as
before, with some important improvements:
- Types are now represented by a single Type class rather than the messy
polymorphism in the Python implementation.
- Qualifiers are a bitmask instead of a set of strings.
- Bit fields are not considered a separate type.
- The lvalue/rvalue terminology is replaced with reference/value.
- Structure, union, and array values are better supported.
- Function objects are supported.
- Program distinguishes between lookups of variables, constants, and
functions.
The C rewrite is about 6x as fast as the original Python when using the
Python bindings, and about 8x when using the C API directly.
Currently, the exposed API in C is fairly conservative. In the future,
the memory reader, type index, and object index APIs will probably be
exposed for more flexibility.
2019-03-22 23:27:46 +00:00
|
|
|
|
2022-07-15 23:02:08 +01:00
|
|
|
|
|
|
|
class DwarfAttrib(NamedTuple):
|
2023-07-19 09:00:06 +01:00
|
|
|
name: DW_AT
|
2022-07-15 23:02:08 +01:00
|
|
|
form: DW_FORM
|
|
|
|
value: Any
|
|
|
|
|
|
|
|
|
|
|
|
class DwarfLabel(NamedTuple):
|
|
|
|
name: str
|
|
|
|
|
|
|
|
|
|
|
|
class DwarfDie(NamedTuple):
|
|
|
|
tag: DW_TAG
|
|
|
|
attribs: Sequence[DwarfAttrib]
|
|
|
|
children: Sequence[Union["DwarfDie", DwarfLabel]] = ()
|
2023-07-19 09:00:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
class DwarfUnit(NamedTuple):
|
|
|
|
type: DW_UT
|
|
|
|
die: DwarfDie
|
2023-02-10 01:24:17 +00:00
|
|
|
dwo_id: Optional[int] = None
|
2022-07-15 23:02:08 +01:00
|
|
|
type_signature: Optional[int] = None
|
|
|
|
type_offset: Optional[str] = None
|
Rewrite drgn core in C
The current mixed Python/C implementation works well, but it has a
couple of important limitations:
- It's too slow for some common use cases, like iterating over large
data structures.
- It can't be reused in utilities written in other languages.
This replaces the internals with a new library written in C, libdrgn. It
includes Python bindings with mostly the same public interface as
before, with some important improvements:
- Types are now represented by a single Type class rather than the messy
polymorphism in the Python implementation.
- Qualifiers are a bitmask instead of a set of strings.
- Bit fields are not considered a separate type.
- The lvalue/rvalue terminology is replaced with reference/value.
- Structure, union, and array values are better supported.
- Function objects are supported.
- Program distinguishes between lookups of variables, constants, and
functions.
The C rewrite is about 6x as fast as the original Python when using the
Python bindings, and about 8x when using the C API directly.
Currently, the exposed API in C is fairly conservative. In the future,
the memory reader, type index, and object index APIs will probably be
exposed for more flexibility.
2019-03-22 23:27:46 +00:00
|
|
|
|
|
|
|
|
2023-07-19 09:00:06 +01:00
|
|
|
def _compile_debug_abbrev(units, use_dw_form_indirect):
|
2019-05-10 07:53:16 +01:00
|
|
|
buf = bytearray()
|
Rewrite drgn core in C
The current mixed Python/C implementation works well, but it has a
couple of important limitations:
- It's too slow for some common use cases, like iterating over large
data structures.
- It can't be reused in utilities written in other languages.
This replaces the internals with a new library written in C, libdrgn. It
includes Python bindings with mostly the same public interface as
before, with some important improvements:
- Types are now represented by a single Type class rather than the messy
polymorphism in the Python implementation.
- Qualifiers are a bitmask instead of a set of strings.
- Bit fields are not considered a separate type.
- The lvalue/rvalue terminology is replaced with reference/value.
- Structure, union, and array values are better supported.
- Function objects are supported.
- Program distinguishes between lookups of variables, constants, and
functions.
The C rewrite is about 6x as fast as the original Python when using the
Python bindings, and about 8x when using the C API directly.
Currently, the exposed API in C is fairly conservative. In the future,
the memory reader, type index, and object index APIs will probably be
exposed for more flexibility.
2019-03-22 23:27:46 +00:00
|
|
|
code = 1
|
2020-01-14 19:43:58 +00:00
|
|
|
|
Rewrite drgn core in C
The current mixed Python/C implementation works well, but it has a
couple of important limitations:
- It's too slow for some common use cases, like iterating over large
data structures.
- It can't be reused in utilities written in other languages.
This replaces the internals with a new library written in C, libdrgn. It
includes Python bindings with mostly the same public interface as
before, with some important improvements:
- Types are now represented by a single Type class rather than the messy
polymorphism in the Python implementation.
- Qualifiers are a bitmask instead of a set of strings.
- Bit fields are not considered a separate type.
- The lvalue/rvalue terminology is replaced with reference/value.
- Structure, union, and array values are better supported.
- Function objects are supported.
- Program distinguishes between lookups of variables, constants, and
functions.
The C rewrite is about 6x as fast as the original Python when using the
Python bindings, and about 8x when using the C API directly.
Currently, the exposed API in C is fairly conservative. In the future,
the memory reader, type index, and object index APIs will probably be
exposed for more flexibility.
2019-03-22 23:27:46 +00:00
|
|
|
def aux(die):
|
2022-07-15 23:02:08 +01:00
|
|
|
if isinstance(die, DwarfLabel):
|
|
|
|
return
|
Rewrite drgn core in C
The current mixed Python/C implementation works well, but it has a
couple of important limitations:
- It's too slow for some common use cases, like iterating over large
data structures.
- It can't be reused in utilities written in other languages.
This replaces the internals with a new library written in C, libdrgn. It
includes Python bindings with mostly the same public interface as
before, with some important improvements:
- Types are now represented by a single Type class rather than the messy
polymorphism in the Python implementation.
- Qualifiers are a bitmask instead of a set of strings.
- Bit fields are not considered a separate type.
- The lvalue/rvalue terminology is replaced with reference/value.
- Structure, union, and array values are better supported.
- Function objects are supported.
- Program distinguishes between lookups of variables, constants, and
functions.
The C rewrite is about 6x as fast as the original Python when using the
Python bindings, and about 8x when using the C API directly.
Currently, the exposed API in C is fairly conservative. In the future,
the memory reader, type index, and object index APIs will probably be
exposed for more flexibility.
2019-03-22 23:27:46 +00:00
|
|
|
nonlocal code
|
|
|
|
_append_uleb128(buf, code)
|
|
|
|
code += 1
|
|
|
|
_append_uleb128(buf, die.tag)
|
|
|
|
buf.append(bool(die.children))
|
|
|
|
for attrib in die.attribs:
|
|
|
|
_append_uleb128(buf, attrib.name)
|
2021-05-05 00:51:40 +01:00
|
|
|
_append_uleb128(
|
|
|
|
buf, DW_FORM.indirect if use_dw_form_indirect else attrib.form
|
|
|
|
)
|
Rewrite drgn core in C
The current mixed Python/C implementation works well, but it has a
couple of important limitations:
- It's too slow for some common use cases, like iterating over large
data structures.
- It can't be reused in utilities written in other languages.
This replaces the internals with a new library written in C, libdrgn. It
includes Python bindings with mostly the same public interface as
before, with some important improvements:
- Types are now represented by a single Type class rather than the messy
polymorphism in the Python implementation.
- Qualifiers are a bitmask instead of a set of strings.
- Bit fields are not considered a separate type.
- The lvalue/rvalue terminology is replaced with reference/value.
- Structure, union, and array values are better supported.
- Function objects are supported.
- Program distinguishes between lookups of variables, constants, and
functions.
The C rewrite is about 6x as fast as the original Python when using the
Python bindings, and about 8x when using the C API directly.
Currently, the exposed API in C is fairly conservative. In the future,
the memory reader, type index, and object index APIs will probably be
exposed for more flexibility.
2019-03-22 23:27:46 +00:00
|
|
|
buf.append(0)
|
|
|
|
buf.append(0)
|
|
|
|
if die.children:
|
|
|
|
for child in die.children:
|
|
|
|
aux(child)
|
2020-01-14 19:43:58 +00:00
|
|
|
|
2023-07-19 09:00:06 +01:00
|
|
|
for unit in units:
|
|
|
|
aux(unit.die)
|
Rewrite drgn core in C
The current mixed Python/C implementation works well, but it has a
couple of important limitations:
- It's too slow for some common use cases, like iterating over large
data structures.
- It can't be reused in utilities written in other languages.
This replaces the internals with a new library written in C, libdrgn. It
includes Python bindings with mostly the same public interface as
before, with some important improvements:
- Types are now represented by a single Type class rather than the messy
polymorphism in the Python implementation.
- Qualifiers are a bitmask instead of a set of strings.
- Bit fields are not considered a separate type.
- The lvalue/rvalue terminology is replaced with reference/value.
- Structure, union, and array values are better supported.
- Function objects are supported.
- Program distinguishes between lookups of variables, constants, and
functions.
The C rewrite is about 6x as fast as the original Python when using the
Python bindings, and about 8x when using the C API directly.
Currently, the exposed API in C is fairly conservative. In the future,
the memory reader, type index, and object index APIs will probably be
exposed for more flexibility.
2019-03-22 23:27:46 +00:00
|
|
|
buf.append(0)
|
2019-05-10 07:53:16 +01:00
|
|
|
return buf
|
Rewrite drgn core in C
The current mixed Python/C implementation works well, but it has a
couple of important limitations:
- It's too slow for some common use cases, like iterating over large
data structures.
- It can't be reused in utilities written in other languages.
This replaces the internals with a new library written in C, libdrgn. It
includes Python bindings with mostly the same public interface as
before, with some important improvements:
- Types are now represented by a single Type class rather than the messy
polymorphism in the Python implementation.
- Qualifiers are a bitmask instead of a set of strings.
- Bit fields are not considered a separate type.
- The lvalue/rvalue terminology is replaced with reference/value.
- Structure, union, and array values are better supported.
- Function objects are supported.
- Program distinguishes between lookups of variables, constants, and
functions.
The C rewrite is about 6x as fast as the original Python when using the
Python bindings, and about 8x when using the C API directly.
Currently, the exposed API in C is fairly conservative. In the future,
the memory reader, type index, and object index APIs will probably be
exposed for more flexibility.
2019-03-22 23:27:46 +00:00
|
|
|
|
|
|
|
|
2023-07-19 09:00:06 +01:00
|
|
|
def _compile_debug_info(units, little_endian, bits, version, use_dw_form_indirect):
|
Rewrite drgn core in C
The current mixed Python/C implementation works well, but it has a
couple of important limitations:
- It's too slow for some common use cases, like iterating over large
data structures.
- It can't be reused in utilities written in other languages.
This replaces the internals with a new library written in C, libdrgn. It
includes Python bindings with mostly the same public interface as
before, with some important improvements:
- Types are now represented by a single Type class rather than the messy
polymorphism in the Python implementation.
- Qualifiers are a bitmask instead of a set of strings.
- Bit fields are not considered a separate type.
- The lvalue/rvalue terminology is replaced with reference/value.
- Structure, union, and array values are better supported.
- Function objects are supported.
- Program distinguishes between lookups of variables, constants, and
functions.
The C rewrite is about 6x as fast as the original Python when using the
Python bindings, and about 8x when using the C API directly.
Currently, the exposed API in C is fairly conservative. In the future,
the memory reader, type index, and object index APIs will probably be
exposed for more flexibility.
2019-03-22 23:27:46 +00:00
|
|
|
byteorder = "little" if little_endian else "big"
|
2022-07-15 23:02:08 +01:00
|
|
|
all_labels = set()
|
|
|
|
labels = {}
|
Rewrite drgn core in C
The current mixed Python/C implementation works well, but it has a
couple of important limitations:
- It's too slow for some common use cases, like iterating over large
data structures.
- It can't be reused in utilities written in other languages.
This replaces the internals with a new library written in C, libdrgn. It
includes Python bindings with mostly the same public interface as
before, with some important improvements:
- Types are now represented by a single Type class rather than the messy
polymorphism in the Python implementation.
- Qualifiers are a bitmask instead of a set of strings.
- Bit fields are not considered a separate type.
- The lvalue/rvalue terminology is replaced with reference/value.
- Structure, union, and array values are better supported.
- Function objects are supported.
- Program distinguishes between lookups of variables, constants, and
functions.
The C rewrite is about 6x as fast as the original Python when using the
Python bindings, and about 8x when using the C API directly.
Currently, the exposed API in C is fairly conservative. In the future,
the memory reader, type index, and object index APIs will probably be
exposed for more flexibility.
2019-03-22 23:27:46 +00:00
|
|
|
relocations = []
|
|
|
|
code = 1
|
|
|
|
decl_file = 1
|
2020-01-14 19:43:58 +00:00
|
|
|
|
2021-02-18 00:27:04 +00:00
|
|
|
def aux(buf, die, depth):
|
2022-07-15 23:02:08 +01:00
|
|
|
if isinstance(die, DwarfLabel):
|
2020-12-16 00:38:21 +00:00
|
|
|
# For now, labels are only supported within a unit, but make sure
|
2022-07-15 23:02:08 +01:00
|
|
|
# they're unique across all units.
|
|
|
|
if die.name in all_labels:
|
|
|
|
raise ValueError(f"duplicate label {die.name!r}")
|
|
|
|
all_labels.add(die.name)
|
|
|
|
labels[die.name] = len(buf)
|
|
|
|
return
|
|
|
|
|
Rewrite drgn core in C
The current mixed Python/C implementation works well, but it has a
couple of important limitations:
- It's too slow for some common use cases, like iterating over large
data structures.
- It can't be reused in utilities written in other languages.
This replaces the internals with a new library written in C, libdrgn. It
includes Python bindings with mostly the same public interface as
before, with some important improvements:
- Types are now represented by a single Type class rather than the messy
polymorphism in the Python implementation.
- Qualifiers are a bitmask instead of a set of strings.
- Bit fields are not considered a separate type.
- The lvalue/rvalue terminology is replaced with reference/value.
- Structure, union, and array values are better supported.
- Function objects are supported.
- Program distinguishes between lookups of variables, constants, and
functions.
The C rewrite is about 6x as fast as the original Python when using the
Python bindings, and about 8x when using the C API directly.
Currently, the exposed API in C is fairly conservative. In the future,
the memory reader, type index, and object index APIs will probably be
exposed for more flexibility.
2019-03-22 23:27:46 +00:00
|
|
|
nonlocal code, decl_file
|
|
|
|
_append_uleb128(buf, code)
|
|
|
|
code += 1
|
|
|
|
for attrib in die.attribs:
|
2021-05-05 00:51:40 +01:00
|
|
|
if use_dw_form_indirect:
|
|
|
|
_append_uleb128(buf, attrib.form)
|
Rewrite drgn core in C
The current mixed Python/C implementation works well, but it has a
couple of important limitations:
- It's too slow for some common use cases, like iterating over large
data structures.
- It can't be reused in utilities written in other languages.
This replaces the internals with a new library written in C, libdrgn. It
includes Python bindings with mostly the same public interface as
before, with some important improvements:
- Types are now represented by a single Type class rather than the messy
polymorphism in the Python implementation.
- Qualifiers are a bitmask instead of a set of strings.
- Bit fields are not considered a separate type.
- The lvalue/rvalue terminology is replaced with reference/value.
- Structure, union, and array values are better supported.
- Function objects are supported.
- Program distinguishes between lookups of variables, constants, and
functions.
The C rewrite is about 6x as fast as the original Python when using the
Python bindings, and about 8x when using the C API directly.
Currently, the exposed API in C is fairly conservative. In the future,
the memory reader, type index, and object index APIs will probably be
exposed for more flexibility.
2019-03-22 23:27:46 +00:00
|
|
|
if attrib.name == DW_AT.decl_file:
|
|
|
|
value = decl_file
|
|
|
|
decl_file += 1
|
|
|
|
else:
|
|
|
|
value = attrib.value
|
|
|
|
if attrib.form == DW_FORM.addr:
|
|
|
|
buf.extend(value.to_bytes(bits // 8, byteorder))
|
|
|
|
elif attrib.form == DW_FORM.data1:
|
|
|
|
buf.append(value)
|
2020-07-13 23:21:55 +01:00
|
|
|
elif attrib.form == DW_FORM.data2:
|
|
|
|
buf.extend(value.to_bytes(2, byteorder))
|
|
|
|
elif attrib.form == DW_FORM.data4:
|
|
|
|
buf.extend(value.to_bytes(4, byteorder))
|
|
|
|
elif attrib.form == DW_FORM.data8:
|
|
|
|
buf.extend(value.to_bytes(8, byteorder))
|
Rewrite drgn core in C
The current mixed Python/C implementation works well, but it has a
couple of important limitations:
- It's too slow for some common use cases, like iterating over large
data structures.
- It can't be reused in utilities written in other languages.
This replaces the internals with a new library written in C, libdrgn. It
includes Python bindings with mostly the same public interface as
before, with some important improvements:
- Types are now represented by a single Type class rather than the messy
polymorphism in the Python implementation.
- Qualifiers are a bitmask instead of a set of strings.
- Bit fields are not considered a separate type.
- The lvalue/rvalue terminology is replaced with reference/value.
- Structure, union, and array values are better supported.
- Function objects are supported.
- Program distinguishes between lookups of variables, constants, and
functions.
The C rewrite is about 6x as fast as the original Python when using the
Python bindings, and about 8x when using the C API directly.
Currently, the exposed API in C is fairly conservative. In the future,
the memory reader, type index, and object index APIs will probably be
exposed for more flexibility.
2019-03-22 23:27:46 +00:00
|
|
|
elif attrib.form == DW_FORM.udata:
|
|
|
|
_append_uleb128(buf, value)
|
|
|
|
elif attrib.form == DW_FORM.sdata:
|
|
|
|
_append_sleb128(buf, value)
|
2021-06-30 09:33:35 +01:00
|
|
|
elif attrib.form == DW_FORM.block:
|
|
|
|
_append_uleb128(buf, len(value))
|
|
|
|
buf.extend(value)
|
2020-07-13 23:21:55 +01:00
|
|
|
elif attrib.form == DW_FORM.block1:
|
|
|
|
buf.append(len(value))
|
|
|
|
buf.extend(value)
|
Rewrite drgn core in C
The current mixed Python/C implementation works well, but it has a
couple of important limitations:
- It's too slow for some common use cases, like iterating over large
data structures.
- It can't be reused in utilities written in other languages.
This replaces the internals with a new library written in C, libdrgn. It
includes Python bindings with mostly the same public interface as
before, with some important improvements:
- Types are now represented by a single Type class rather than the messy
polymorphism in the Python implementation.
- Qualifiers are a bitmask instead of a set of strings.
- Bit fields are not considered a separate type.
- The lvalue/rvalue terminology is replaced with reference/value.
- Structure, union, and array values are better supported.
- Function objects are supported.
- Program distinguishes between lookups of variables, constants, and
functions.
The C rewrite is about 6x as fast as the original Python when using the
Python bindings, and about 8x when using the C API directly.
Currently, the exposed API in C is fairly conservative. In the future,
the memory reader, type index, and object index APIs will probably be
exposed for more flexibility.
2019-03-22 23:27:46 +00:00
|
|
|
elif attrib.form == DW_FORM.string:
|
|
|
|
buf.extend(value.encode())
|
|
|
|
buf.append(0)
|
|
|
|
elif attrib.form == DW_FORM.ref4:
|
|
|
|
relocations.append((len(buf), value))
|
|
|
|
buf.extend(b"\0\0\0\0")
|
2021-02-18 00:27:04 +00:00
|
|
|
elif attrib.form == DW_FORM.ref_sig8:
|
2022-07-15 23:02:08 +01:00
|
|
|
buf.extend(value.to_bytes(8, byteorder))
|
Rewrite drgn core in C
The current mixed Python/C implementation works well, but it has a
couple of important limitations:
- It's too slow for some common use cases, like iterating over large
data structures.
- It can't be reused in utilities written in other languages.
This replaces the internals with a new library written in C, libdrgn. It
includes Python bindings with mostly the same public interface as
before, with some important improvements:
- Types are now represented by a single Type class rather than the messy
polymorphism in the Python implementation.
- Qualifiers are a bitmask instead of a set of strings.
- Bit fields are not considered a separate type.
- The lvalue/rvalue terminology is replaced with reference/value.
- Structure, union, and array values are better supported.
- Function objects are supported.
- Program distinguishes between lookups of variables, constants, and
functions.
The C rewrite is about 6x as fast as the original Python when using the
Python bindings, and about 8x when using the C API directly.
Currently, the exposed API in C is fairly conservative. In the future,
the memory reader, type index, and object index APIs will probably be
exposed for more flexibility.
2019-03-22 23:27:46 +00:00
|
|
|
elif attrib.form == DW_FORM.sec_offset:
|
|
|
|
buf.extend(b"\0\0\0\0")
|
|
|
|
elif attrib.form == DW_FORM.flag_present:
|
|
|
|
pass
|
|
|
|
elif attrib.form == DW_FORM.exprloc:
|
|
|
|
_append_uleb128(buf, len(value))
|
|
|
|
buf.extend(value)
|
|
|
|
else:
|
|
|
|
assert False, attrib.form
|
|
|
|
if die.children:
|
|
|
|
for child in die.children:
|
2021-02-18 00:27:04 +00:00
|
|
|
aux(buf, child, depth + 1)
|
Rewrite drgn core in C
The current mixed Python/C implementation works well, but it has a
couple of important limitations:
- It's too slow for some common use cases, like iterating over large
data structures.
- It can't be reused in utilities written in other languages.
This replaces the internals with a new library written in C, libdrgn. It
includes Python bindings with mostly the same public interface as
before, with some important improvements:
- Types are now represented by a single Type class rather than the messy
polymorphism in the Python implementation.
- Qualifiers are a bitmask instead of a set of strings.
- Bit fields are not considered a separate type.
- The lvalue/rvalue terminology is replaced with reference/value.
- Structure, union, and array values are better supported.
- Function objects are supported.
- Program distinguishes between lookups of variables, constants, and
functions.
The C rewrite is about 6x as fast as the original Python when using the
Python bindings, and about 8x when using the C API directly.
Currently, the exposed API in C is fairly conservative. In the future,
the memory reader, type index, and object index APIs will probably be
exposed for more flexibility.
2019-03-22 23:27:46 +00:00
|
|
|
buf.append(0)
|
2020-01-14 19:43:58 +00:00
|
|
|
|
2021-02-18 00:27:04 +00:00
|
|
|
debug_info = bytearray()
|
|
|
|
debug_types = bytearray()
|
2023-07-19 09:00:06 +01:00
|
|
|
for unit in units:
|
2022-07-15 23:02:08 +01:00
|
|
|
labels.clear()
|
2021-02-18 00:27:04 +00:00
|
|
|
relocations.clear()
|
2023-07-19 09:00:06 +01:00
|
|
|
decl_file = 1
|
|
|
|
if version == 4 and unit.type in (DW_UT.type, DW_UT.split_type):
|
|
|
|
buf = debug_types
|
|
|
|
else:
|
|
|
|
buf = debug_info
|
2021-02-18 00:27:04 +00:00
|
|
|
orig_len = len(buf)
|
|
|
|
buf.extend(b"\0\0\0\0") # unit_length
|
2023-07-19 09:00:06 +01:00
|
|
|
buf.extend(version.to_bytes(2, byteorder)) # version
|
|
|
|
if version >= 5:
|
|
|
|
buf.append(unit.type) # unit_type
|
|
|
|
buf.append(bits // 8) # address_size
|
2021-02-18 00:27:04 +00:00
|
|
|
buf.extend((0).to_bytes(4, byteorder)) # debug_abbrev_offset
|
2023-07-19 09:00:06 +01:00
|
|
|
if version < 5:
|
|
|
|
buf.append(bits // 8) # address_size
|
Rewrite drgn core in C
The current mixed Python/C implementation works well, but it has a
couple of important limitations:
- It's too slow for some common use cases, like iterating over large
data structures.
- It can't be reused in utilities written in other languages.
This replaces the internals with a new library written in C, libdrgn. It
includes Python bindings with mostly the same public interface as
before, with some important improvements:
- Types are now represented by a single Type class rather than the messy
polymorphism in the Python implementation.
- Qualifiers are a bitmask instead of a set of strings.
- Bit fields are not considered a separate type.
- The lvalue/rvalue terminology is replaced with reference/value.
- Structure, union, and array values are better supported.
- Function objects are supported.
- Program distinguishes between lookups of variables, constants, and
functions.
The C rewrite is about 6x as fast as the original Python when using the
Python bindings, and about 8x when using the C API directly.
Currently, the exposed API in C is fairly conservative. In the future,
the memory reader, type index, and object index APIs will probably be
exposed for more flexibility.
2019-03-22 23:27:46 +00:00
|
|
|
|
2023-02-10 01:24:17 +00:00
|
|
|
if version >= 5 and unit.type in (DW_UT.skeleton, DW_UT.split_compile):
|
|
|
|
buf.extend(unit.dwo_id.to_bytes(8, byteorder)) # dwo_id
|
|
|
|
else:
|
|
|
|
assert unit.dwo_id is None
|
2023-07-19 09:00:06 +01:00
|
|
|
if unit.type in (DW_UT.type, DW_UT.split_type):
|
|
|
|
buf.extend(unit.type_signature.to_bytes(8, byteorder)) # type_signature
|
|
|
|
relocations.append((len(buf), unit.type_offset))
|
2021-02-18 00:27:04 +00:00
|
|
|
buf.extend(b"\0\0\0\0") # type_offset
|
2022-07-15 23:02:08 +01:00
|
|
|
else:
|
2023-07-19 09:00:06 +01:00
|
|
|
assert unit.type_signature is None
|
|
|
|
assert unit.type_offset is None
|
Rewrite drgn core in C
The current mixed Python/C implementation works well, but it has a
couple of important limitations:
- It's too slow for some common use cases, like iterating over large
data structures.
- It can't be reused in utilities written in other languages.
This replaces the internals with a new library written in C, libdrgn. It
includes Python bindings with mostly the same public interface as
before, with some important improvements:
- Types are now represented by a single Type class rather than the messy
polymorphism in the Python implementation.
- Qualifiers are a bitmask instead of a set of strings.
- Bit fields are not considered a separate type.
- The lvalue/rvalue terminology is replaced with reference/value.
- Structure, union, and array values are better supported.
- Function objects are supported.
- Program distinguishes between lookups of variables, constants, and
functions.
The C rewrite is about 6x as fast as the original Python when using the
Python bindings, and about 8x when using the C API directly.
Currently, the exposed API in C is fairly conservative. In the future,
the memory reader, type index, and object index APIs will probably be
exposed for more flexibility.
2019-03-22 23:27:46 +00:00
|
|
|
|
2023-07-19 09:00:06 +01:00
|
|
|
aux(buf, unit.die, 0)
|
2021-02-18 00:27:04 +00:00
|
|
|
|
|
|
|
unit_length = len(buf) - orig_len - 4
|
|
|
|
buf[orig_len : orig_len + 4] = unit_length.to_bytes(4, byteorder)
|
Rewrite drgn core in C
The current mixed Python/C implementation works well, but it has a
couple of important limitations:
- It's too slow for some common use cases, like iterating over large
data structures.
- It can't be reused in utilities written in other languages.
This replaces the internals with a new library written in C, libdrgn. It
includes Python bindings with mostly the same public interface as
before, with some important improvements:
- Types are now represented by a single Type class rather than the messy
polymorphism in the Python implementation.
- Qualifiers are a bitmask instead of a set of strings.
- Bit fields are not considered a separate type.
- The lvalue/rvalue terminology is replaced with reference/value.
- Structure, union, and array values are better supported.
- Function objects are supported.
- Program distinguishes between lookups of variables, constants, and
functions.
The C rewrite is about 6x as fast as the original Python when using the
Python bindings, and about 8x when using the C API directly.
Currently, the exposed API in C is fairly conservative. In the future,
the memory reader, type index, and object index APIs will probably be
exposed for more flexibility.
2019-03-22 23:27:46 +00:00
|
|
|
|
2022-07-15 23:02:08 +01:00
|
|
|
for offset, label in relocations:
|
|
|
|
die_offset = labels[label] - orig_len
|
2021-02-18 00:27:04 +00:00
|
|
|
buf[offset : offset + 4] = die_offset.to_bytes(4, byteorder)
|
|
|
|
return debug_info, debug_types
|
Rewrite drgn core in C
The current mixed Python/C implementation works well, but it has a
couple of important limitations:
- It's too slow for some common use cases, like iterating over large
data structures.
- It can't be reused in utilities written in other languages.
This replaces the internals with a new library written in C, libdrgn. It
includes Python bindings with mostly the same public interface as
before, with some important improvements:
- Types are now represented by a single Type class rather than the messy
polymorphism in the Python implementation.
- Qualifiers are a bitmask instead of a set of strings.
- Bit fields are not considered a separate type.
- The lvalue/rvalue terminology is replaced with reference/value.
- Structure, union, and array values are better supported.
- Function objects are supported.
- Program distinguishes between lookups of variables, constants, and
functions.
The C rewrite is about 6x as fast as the original Python when using the
Python bindings, and about 8x when using the C API directly.
Currently, the exposed API in C is fairly conservative. In the future,
the memory reader, type index, and object index APIs will probably be
exposed for more flexibility.
2019-03-22 23:27:46 +00:00
|
|
|
|
2021-02-18 00:27:04 +00:00
|
|
|
|
2023-07-19 09:00:06 +01:00
|
|
|
def _compile_debug_line(units, little_endian, bits, version):
|
Rewrite drgn core in C
The current mixed Python/C implementation works well, but it has a
couple of important limitations:
- It's too slow for some common use cases, like iterating over large
data structures.
- It can't be reused in utilities written in other languages.
This replaces the internals with a new library written in C, libdrgn. It
includes Python bindings with mostly the same public interface as
before, with some important improvements:
- Types are now represented by a single Type class rather than the messy
polymorphism in the Python implementation.
- Qualifiers are a bitmask instead of a set of strings.
- Bit fields are not considered a separate type.
- The lvalue/rvalue terminology is replaced with reference/value.
- Structure, union, and array values are better supported.
- Function objects are supported.
- Program distinguishes between lookups of variables, constants, and
functions.
The C rewrite is about 6x as fast as the original Python when using the
Python bindings, and about 8x when using the C API directly.
Currently, the exposed API in C is fairly conservative. In the future,
the memory reader, type index, and object index APIs will probably be
exposed for more flexibility.
2019-03-22 23:27:46 +00:00
|
|
|
byteorder = "little" if little_endian else "big"
|
|
|
|
|
2023-07-19 09:00:06 +01:00
|
|
|
if not units:
|
|
|
|
units = [DwarfUnit(DW_UT.compile, DwarfDie(DW_TAG.compile_unit, []))]
|
|
|
|
|
|
|
|
buf = bytearray()
|
|
|
|
for unit in units:
|
|
|
|
unit.die.attribs.append(
|
|
|
|
DwarfAttrib(DW_AT.stmt_list, DW_FORM.sec_offset, len(buf))
|
|
|
|
)
|
|
|
|
if unit.type in (DW_UT.compile, DW_UT.partial, DW_UT.skeleton):
|
|
|
|
unit.die.attribs.append(DwarfAttrib(DW_AT.name, DW_FORM.string, "main.c"))
|
|
|
|
unit.die.attribs.append(
|
|
|
|
DwarfAttrib(DW_AT.comp_dir, DW_FORM.string, "/usr/src")
|
|
|
|
)
|
|
|
|
|
|
|
|
unit_length_start = len(buf)
|
|
|
|
buf.extend(b"\0\0\0\0") # unit_length
|
|
|
|
unit_length_end = len(buf)
|
|
|
|
buf.extend(version.to_bytes(2, byteorder)) # version
|
|
|
|
if version >= 5:
|
|
|
|
buf.append(bits // 8) # address_size
|
|
|
|
buf.append(0) # segment_selector_size
|
|
|
|
header_length_start = len(buf)
|
|
|
|
buf.extend(b"\0\0\0\0") # header_length
|
|
|
|
header_length_end = len(buf)
|
|
|
|
buf.append(1) # minimum_instruction_length
|
|
|
|
buf.append(1) # maximum_operations_per_instruction
|
|
|
|
buf.append(1) # default_is_stmt
|
|
|
|
buf.append(1) # line_base
|
|
|
|
buf.append(1) # line_range
|
|
|
|
buf.append(1) # opcode_base
|
|
|
|
# Don't need standard_opcode_lengths
|
|
|
|
if version >= 5:
|
|
|
|
buf.append(1) # directory_entry_format_count
|
|
|
|
# directory_entry_format
|
|
|
|
_append_uleb128(buf, DW_LNCT.path)
|
|
|
|
_append_uleb128(buf, DW_FORM.string)
|
|
|
|
|
|
|
|
directories = OrderedDict([("/usr/src", 0)])
|
|
|
|
|
|
|
|
def collect_directories(die):
|
|
|
|
if isinstance(die, DwarfLabel):
|
|
|
|
return
|
|
|
|
for attrib in die.attribs:
|
|
|
|
if attrib.name != DW_AT.decl_file:
|
|
|
|
continue
|
|
|
|
dirname = os.path.dirname(attrib.value)
|
|
|
|
if dirname:
|
|
|
|
directories.setdefault(dirname, len(directories))
|
Rewrite drgn core in C
The current mixed Python/C implementation works well, but it has a
couple of important limitations:
- It's too slow for some common use cases, like iterating over large
data structures.
- It can't be reused in utilities written in other languages.
This replaces the internals with a new library written in C, libdrgn. It
includes Python bindings with mostly the same public interface as
before, with some important improvements:
- Types are now represented by a single Type class rather than the messy
polymorphism in the Python implementation.
- Qualifiers are a bitmask instead of a set of strings.
- Bit fields are not considered a separate type.
- The lvalue/rvalue terminology is replaced with reference/value.
- Structure, union, and array values are better supported.
- Function objects are supported.
- Program distinguishes between lookups of variables, constants, and
functions.
The C rewrite is about 6x as fast as the original Python when using the
Python bindings, and about 8x when using the C API directly.
Currently, the exposed API in C is fairly conservative. In the future,
the memory reader, type index, and object index APIs will probably be
exposed for more flexibility.
2019-03-22 23:27:46 +00:00
|
|
|
for child in die.children:
|
2023-07-19 09:00:06 +01:00
|
|
|
collect_directories(child)
|
2020-01-14 19:43:58 +00:00
|
|
|
|
2023-07-19 09:00:06 +01:00
|
|
|
collect_directories(unit.die)
|
Rewrite drgn core in C
The current mixed Python/C implementation works well, but it has a
couple of important limitations:
- It's too slow for some common use cases, like iterating over large
data structures.
- It can't be reused in utilities written in other languages.
This replaces the internals with a new library written in C, libdrgn. It
includes Python bindings with mostly the same public interface as
before, with some important improvements:
- Types are now represented by a single Type class rather than the messy
polymorphism in the Python implementation.
- Qualifiers are a bitmask instead of a set of strings.
- Bit fields are not considered a separate type.
- The lvalue/rvalue terminology is replaced with reference/value.
- Structure, union, and array values are better supported.
- Function objects are supported.
- Program distinguishes between lookups of variables, constants, and
functions.
The C rewrite is about 6x as fast as the original Python when using the
Python bindings, and about 8x when using the C API directly.
Currently, the exposed API in C is fairly conservative. In the future,
the memory reader, type index, and object index APIs will probably be
exposed for more flexibility.
2019-03-22 23:27:46 +00:00
|
|
|
|
2023-07-19 09:00:06 +01:00
|
|
|
if version >= 5:
|
|
|
|
_append_uleb128(buf, len(directories)) # directories_count
|
2020-01-14 19:43:58 +00:00
|
|
|
|
2023-07-19 09:00:06 +01:00
|
|
|
# directories (or include_directories in version <= 4)
|
|
|
|
for directory, index in directories.items():
|
|
|
|
if index > 0 or version >= 5:
|
|
|
|
buf.extend(directory.encode("ascii"))
|
|
|
|
buf.append(0)
|
|
|
|
if version < 5:
|
Rewrite drgn core in C
The current mixed Python/C implementation works well, but it has a
couple of important limitations:
- It's too slow for some common use cases, like iterating over large
data structures.
- It can't be reused in utilities written in other languages.
This replaces the internals with a new library written in C, libdrgn. It
includes Python bindings with mostly the same public interface as
before, with some important improvements:
- Types are now represented by a single Type class rather than the messy
polymorphism in the Python implementation.
- Qualifiers are a bitmask instead of a set of strings.
- Bit fields are not considered a separate type.
- The lvalue/rvalue terminology is replaced with reference/value.
- Structure, union, and array values are better supported.
- Function objects are supported.
- Program distinguishes between lookups of variables, constants, and
functions.
The C rewrite is about 6x as fast as the original Python when using the
Python bindings, and about 8x when using the C API directly.
Currently, the exposed API in C is fairly conservative. In the future,
the memory reader, type index, and object index APIs will probably be
exposed for more flexibility.
2019-03-22 23:27:46 +00:00
|
|
|
buf.append(0)
|
2023-07-19 09:00:06 +01:00
|
|
|
|
|
|
|
if version >= 5:
|
|
|
|
buf.append(2) # file_name_entry_format_count
|
|
|
|
# file_name_entry_format
|
|
|
|
_append_uleb128(buf, DW_LNCT.path)
|
|
|
|
_append_uleb128(buf, DW_FORM.string)
|
|
|
|
_append_uleb128(buf, DW_LNCT.directory_index)
|
|
|
|
_append_uleb128(buf, DW_FORM.udata)
|
|
|
|
|
|
|
|
file_names = [("main.c", 0)]
|
|
|
|
|
|
|
|
def collect_file_names(die):
|
|
|
|
if isinstance(die, DwarfLabel):
|
|
|
|
return
|
|
|
|
for attrib in die.attribs:
|
|
|
|
if attrib.name != DW_AT.decl_file:
|
|
|
|
continue
|
|
|
|
dirname, basename = os.path.split(attrib.value)
|
|
|
|
directory_index = directories[dirname] if dirname else 0
|
|
|
|
file_names.append((basename, directory_index))
|
Rewrite drgn core in C
The current mixed Python/C implementation works well, but it has a
couple of important limitations:
- It's too slow for some common use cases, like iterating over large
data structures.
- It can't be reused in utilities written in other languages.
This replaces the internals with a new library written in C, libdrgn. It
includes Python bindings with mostly the same public interface as
before, with some important improvements:
- Types are now represented by a single Type class rather than the messy
polymorphism in the Python implementation.
- Qualifiers are a bitmask instead of a set of strings.
- Bit fields are not considered a separate type.
- The lvalue/rvalue terminology is replaced with reference/value.
- Structure, union, and array values are better supported.
- Function objects are supported.
- Program distinguishes between lookups of variables, constants, and
functions.
The C rewrite is about 6x as fast as the original Python when using the
Python bindings, and about 8x when using the C API directly.
Currently, the exposed API in C is fairly conservative. In the future,
the memory reader, type index, and object index APIs will probably be
exposed for more flexibility.
2019-03-22 23:27:46 +00:00
|
|
|
for child in die.children:
|
2023-07-19 09:00:06 +01:00
|
|
|
collect_file_names(child)
|
2020-01-14 19:43:58 +00:00
|
|
|
|
2023-07-19 09:00:06 +01:00
|
|
|
collect_file_names(unit.die)
|
|
|
|
|
|
|
|
if version >= 5:
|
|
|
|
_append_uleb128(buf, len(file_names)) # file_names_count
|
Rewrite drgn core in C
The current mixed Python/C implementation works well, but it has a
couple of important limitations:
- It's too slow for some common use cases, like iterating over large
data structures.
- It can't be reused in utilities written in other languages.
This replaces the internals with a new library written in C, libdrgn. It
includes Python bindings with mostly the same public interface as
before, with some important improvements:
- Types are now represented by a single Type class rather than the messy
polymorphism in the Python implementation.
- Qualifiers are a bitmask instead of a set of strings.
- Bit fields are not considered a separate type.
- The lvalue/rvalue terminology is replaced with reference/value.
- Structure, union, and array values are better supported.
- Function objects are supported.
- Program distinguishes between lookups of variables, constants, and
functions.
The C rewrite is about 6x as fast as the original Python when using the
Python bindings, and about 8x when using the C API directly.
Currently, the exposed API in C is fairly conservative. In the future,
the memory reader, type index, and object index APIs will probably be
exposed for more flexibility.
2019-03-22 23:27:46 +00:00
|
|
|
|
2023-07-19 09:00:06 +01:00
|
|
|
# file_names
|
|
|
|
for path, directory_index in file_names[0 if version >= 5 else 1 :]:
|
|
|
|
# path
|
|
|
|
buf.extend(path.encode("ascii"))
|
|
|
|
buf.append(0)
|
|
|
|
_append_uleb128(buf, directory_index) # directory_index
|
|
|
|
if version < 5:
|
|
|
|
_append_uleb128(buf, 0) # mtime
|
|
|
|
_append_uleb128(buf, 0) # size
|
|
|
|
|
|
|
|
if version < 5:
|
|
|
|
buf.append(0)
|
|
|
|
|
|
|
|
buf[unit_length_start:unit_length_end] = (len(buf) - unit_length_end).to_bytes(
|
|
|
|
unit_length_end - unit_length_start, byteorder
|
|
|
|
)
|
|
|
|
buf[header_length_start:header_length_end] = (
|
|
|
|
len(buf) - header_length_end
|
|
|
|
).to_bytes(header_length_end - header_length_start, byteorder)
|
2019-05-10 07:53:16 +01:00
|
|
|
return buf
|
Rewrite drgn core in C
The current mixed Python/C implementation works well, but it has a
couple of important limitations:
- It's too slow for some common use cases, like iterating over large
data structures.
- It can't be reused in utilities written in other languages.
This replaces the internals with a new library written in C, libdrgn. It
includes Python bindings with mostly the same public interface as
before, with some important improvements:
- Types are now represented by a single Type class rather than the messy
polymorphism in the Python implementation.
- Qualifiers are a bitmask instead of a set of strings.
- Bit fields are not considered a separate type.
- The lvalue/rvalue terminology is replaced with reference/value.
- Structure, union, and array values are better supported.
- Function objects are supported.
- Program distinguishes between lookups of variables, constants, and
functions.
The C rewrite is about 6x as fast as the original Python when using the
Python bindings, and about 8x when using the C API directly.
Currently, the exposed API in C is fairly conservative. In the future,
the memory reader, type index, and object index APIs will probably be
exposed for more flexibility.
2019-03-22 23:27:46 +00:00
|
|
|
|
|
|
|
|
2022-07-15 23:02:08 +01:00
|
|
|
_UNIT_TAGS = frozenset({DW_TAG.type_unit, DW_TAG.compile_unit})
|
2021-02-18 00:27:04 +00:00
|
|
|
|
|
|
|
|
2021-11-20 00:55:42 +00:00
|
|
|
def dwarf_sections(
|
2023-07-19 09:00:06 +01:00
|
|
|
units_or_dies,
|
2023-06-16 21:51:48 +01:00
|
|
|
little_endian=True,
|
|
|
|
bits=64,
|
|
|
|
*,
|
2023-07-19 09:00:06 +01:00
|
|
|
version=4,
|
2023-06-16 21:51:48 +01:00
|
|
|
lang=None,
|
|
|
|
use_dw_form_indirect=False,
|
|
|
|
compress=None,
|
2023-02-10 01:24:17 +00:00
|
|
|
split=None,
|
2021-05-05 00:51:40 +01:00
|
|
|
):
|
2023-07-19 09:00:06 +01:00
|
|
|
assert compress in (None, "zlib-gnu", "zlib-gabi")
|
2023-02-10 01:24:17 +00:00
|
|
|
assert split in (None, "dwo")
|
2021-02-18 00:27:04 +00:00
|
|
|
|
2023-07-19 09:00:06 +01:00
|
|
|
if isinstance(units_or_dies, (DwarfDie, DwarfUnit)):
|
|
|
|
units_or_dies = (units_or_dies,)
|
|
|
|
if not units_or_dies or isinstance(units_or_dies[0], DwarfUnit):
|
|
|
|
units = units_or_dies
|
2021-02-18 00:27:04 +00:00
|
|
|
else:
|
2023-07-19 09:00:06 +01:00
|
|
|
assert all(isinstance(die, (DwarfDie, DwarfLabel)) for die in units_or_dies)
|
|
|
|
assert all(
|
|
|
|
not isinstance(die, DwarfDie) or die.tag not in _UNIT_TAGS
|
|
|
|
for die in units_or_dies
|
|
|
|
)
|
|
|
|
units = (
|
|
|
|
DwarfUnit(DW_UT.compile, DwarfDie(DW_TAG.compile_unit, (), units_or_dies)),
|
|
|
|
)
|
|
|
|
assert all(isinstance(unit, DwarfUnit) for unit in units)
|
|
|
|
assert all(unit.die.tag in _UNIT_TAGS for unit in units)
|
2021-02-18 00:27:04 +00:00
|
|
|
|
2023-07-19 09:00:06 +01:00
|
|
|
unit_attribs = []
|
2020-02-26 21:22:51 +00:00
|
|
|
if lang is not None:
|
2021-02-18 00:27:04 +00:00
|
|
|
unit_attribs.append(DwarfAttrib(DW_AT.language, DW_FORM.data1, lang))
|
|
|
|
|
2023-07-19 09:00:06 +01:00
|
|
|
units = [
|
|
|
|
unit._replace(
|
|
|
|
die=unit.die._replace(attribs=list(unit.die.attribs) + unit_attribs)
|
2021-02-18 00:27:04 +00:00
|
|
|
)
|
2023-07-19 09:00:06 +01:00
|
|
|
for unit in units
|
2021-02-18 00:27:04 +00:00
|
|
|
]
|
|
|
|
|
2023-02-10 01:24:17 +00:00
|
|
|
# TODO: line number information for a split file is in the skeleton file.
|
|
|
|
# We don't have any test cases yet that use line number information from a
|
|
|
|
# split file, but when we do, we'll have to add a way to include the split
|
|
|
|
# file's line number information in the skeleton file.
|
|
|
|
if not split:
|
|
|
|
debug_line = _compile_debug_line(units, little_endian, bits, version)
|
2023-07-19 09:00:06 +01:00
|
|
|
|
2021-05-05 00:51:40 +01:00
|
|
|
debug_info, debug_types = _compile_debug_info(
|
2023-07-19 09:00:06 +01:00
|
|
|
units, little_endian, bits, version, use_dw_form_indirect
|
2021-05-05 00:51:40 +01:00
|
|
|
)
|
2021-02-18 00:27:04 +00:00
|
|
|
|
2023-07-19 09:00:06 +01:00
|
|
|
def debug_section(name, data):
|
|
|
|
assert name.startswith(".debug")
|
|
|
|
if compress == "zlib-gnu":
|
|
|
|
name = ".z" + name[1:]
|
2023-06-16 21:51:48 +01:00
|
|
|
compressed_data = bytearray(b"ZLIB")
|
|
|
|
compressed_data.extend(len(data).to_bytes(8, "big"))
|
|
|
|
compressed_data.extend(zlib.compress(data))
|
2023-07-19 09:00:06 +01:00
|
|
|
data = compressed_data
|
2023-02-10 01:24:17 +00:00
|
|
|
if split:
|
|
|
|
name += ".dwo"
|
2023-07-19 09:00:06 +01:00
|
|
|
return ElfSection(
|
|
|
|
name=name,
|
|
|
|
sh_type=SHT.PROGBITS,
|
|
|
|
data=data,
|
|
|
|
compressed=(compress == "zlib-gabi"),
|
|
|
|
)
|
|
|
|
return name
|
2023-06-16 21:51:48 +01:00
|
|
|
|
2021-02-18 00:27:04 +00:00
|
|
|
sections = [
|
2023-06-16 21:51:48 +01:00
|
|
|
debug_section(
|
2023-07-19 09:00:06 +01:00
|
|
|
".debug_abbrev", _compile_debug_abbrev(units, use_dw_form_indirect)
|
2021-02-18 00:27:04 +00:00
|
|
|
),
|
2023-07-19 09:00:06 +01:00
|
|
|
debug_section(".debug_info", debug_info),
|
2023-06-16 21:51:48 +01:00
|
|
|
debug_section(".debug_str", b"\0"),
|
2021-02-18 00:27:04 +00:00
|
|
|
]
|
2023-02-10 01:24:17 +00:00
|
|
|
if not split:
|
|
|
|
sections.append(debug_section(".debug_line", debug_line))
|
2021-02-18 00:27:04 +00:00
|
|
|
if debug_types:
|
2023-06-16 21:51:48 +01:00
|
|
|
sections.append(debug_section(".debug_types", debug_types))
|
2021-11-20 00:55:42 +00:00
|
|
|
return sections
|
|
|
|
|
2021-02-18 00:27:04 +00:00
|
|
|
|
2021-11-20 00:55:42 +00:00
|
|
|
def compile_dwarf(
|
2023-06-16 21:51:48 +01:00
|
|
|
dies,
|
|
|
|
little_endian=True,
|
|
|
|
bits=64,
|
|
|
|
*,
|
2023-07-19 09:00:06 +01:00
|
|
|
version=4,
|
2023-06-16 21:51:48 +01:00
|
|
|
lang=None,
|
|
|
|
use_dw_form_indirect=False,
|
|
|
|
compress=None,
|
2023-02-10 01:24:17 +00:00
|
|
|
split=None,
|
2021-11-20 00:55:42 +00:00
|
|
|
):
|
|
|
|
return create_elf_file(
|
|
|
|
ET.EXEC,
|
|
|
|
dwarf_sections(
|
|
|
|
dies,
|
|
|
|
little_endian=little_endian,
|
|
|
|
bits=bits,
|
2023-07-19 09:00:06 +01:00
|
|
|
version=version,
|
2021-11-20 00:55:42 +00:00
|
|
|
lang=lang,
|
|
|
|
use_dw_form_indirect=use_dw_form_indirect,
|
2023-06-16 21:51:48 +01:00
|
|
|
compress=compress,
|
2023-02-10 01:24:17 +00:00
|
|
|
split=split,
|
2021-11-20 00:55:42 +00:00
|
|
|
),
|
|
|
|
little_endian=little_endian,
|
|
|
|
bits=bits,
|
|
|
|
)
|