1 //! Get section/segment relocation offsets from the target.
2 //!
3 //! For some targets, sections may be relocated from their base address. As
4 //! a result, the stub may need to tell GDB the final section addresses
5 //! to ensure that debug symbols are resolved correctly after relocation.
6 //!
7 //! _Note:_ This extension corresponds to the `qOffsets` command, which is
8 //! limited to reporting the offsets for code, data and bss, and is
9 //! generally considered a legacy feature.
10 //!
11 //! For System-V architectures GDB may use the `qXfer:libraries-svr4:read`
12 //! command to try to learn about loaded libraries and this can be implemented
13 //! with the [`LibrariesSvr4`
14 //! trait](crate::target::ext::libraries::LibrariesSvr4). Note that not all
15 //! targets may query this and it may not be applicable in all situations
16 //! either.
17 //!
18 //! For targets where library offsets are maintained externally (e.g. Windows)
19 //! you should consider implementing the more flexible `qXfer:library:read`.
20 //! See issue [#20](https://github.com/daniel5151/gdbstub/issues/20) for more
21 //! info.
22 //!
23 //! For System-V architectures GDB is capable of extracting library offsets
24 //! from memory if it knows the base address of the dynamic linker. The base
25 //! address can be specified by either implementing this command or by including
26 //! a `AT_BASE` entry in the response to the more modern `qXfer:auxv:read`
27 //! command. See issue [#20](https://github.com/daniel5151/gdbstub/issues/20)
28 //! for more info.
29 
30 use crate::arch::Arch;
31 use crate::target::Target;
32 
33 /// Describes the offset the target loaded the image sections at, so the target
34 /// can notify GDB that it needs to adjust the addresses of symbols.
35 ///
36 /// GDB supports either section offsets, or segment addresses.
37 pub enum Offsets<U> {
38     /// Section offsets relative to their base addresses.
39     Sections {
40         /// The offset of the `.text` section.
41         text: U,
42         /// The offset of the `.data` section.
43         data: U,
44         /// The offset of the `.bss` section.
45         ///
46         /// _Note:_ GDB expects that `bss` is either `None` or equal to `data`.
47         bss: Option<U>,
48     },
49 
50     /// Absolute addresses of the first two segments.
51     ///
52     /// _Note:_ any extra segments will kept at fixed offsets relative to the
53     /// last relocated segment.
54     Segments {
55         /// The absolute address of the first segment which conventionally
56         /// contains program code.
57         text_seg: U,
58         /// The absolute address of the second segment which conventionally
59         /// contains modifiable data.
60         data_seg: Option<U>,
61     },
62 }
63 
64 /// Target Extension - Get section/segment relocation offsets from the target.
65 ///
66 /// Corresponds to the `qOffset` command. See the [section_offset module
67 /// documentation](index.html).
68 pub trait SectionOffsets: Target {
69     /// Return the target's current section (or segment) offsets.
get_section_offsets(&mut self) -> Result<Offsets<<Self::Arch as Arch>::Usize>, Self::Error>70     fn get_section_offsets(&mut self) -> Result<Offsets<<Self::Arch as Arch>::Usize>, Self::Error>;
71 }
72 
73 define_ext!(SectionOffsetsOps, SectionOffsets);
74