Skip to main content

gdbstub/stub/core_impl/
section_offsets.rs

1use super::prelude::*;
2use crate::protocol::commands::ext::SectionOffsets;
3
4impl<T: Target, C: Connection> GdbStubImpl<T, C> {
5    pub(crate) fn handle_section_offsets(
6        &mut self,
7        res: &mut ResponseWriter<'_, C>,
8        target: &mut T,
9        command: SectionOffsets,
10    ) -> Result<HandlerStatus, Error<T::Error, C::Error>> {
11        let ops = match target.support_section_offsets() {
12            Some(ops) => ops,
13            None => return Ok(HandlerStatus::Handled),
14        };
15
16        crate::__dead_code_marker!("section_offsets", "impl");
17
18        let handler_status = match command {
19            SectionOffsets::qOffsets(_cmd) => {
20                use crate::target::ext::section_offsets::Offsets;
21
22                match ops.get_section_offsets().map_err(Error::TargetError)? {
23                    Offsets::Sections { text, data, bss } => {
24                        res.write_str("Text=")?;
25                        res.write_num(text)?;
26
27                        res.write_str(";Data=")?;
28                        res.write_num(data)?;
29
30                        // "Note: while a Bss offset may be included in the response,
31                        // GDB ignores this and instead applies the Data offset to the Bss section."
32                        //
33                        // While this would suggest that it's OK to omit `Bss=` entirely, recent
34                        // versions of GDB seem to require that `Bss=` is present.
35                        //
36                        // See https://github.com/bminor/binutils-gdb/blob/master/gdb/remote.c#L4149-L4159
37                        let bss = bss.unwrap_or(data);
38                        res.write_str(";Bss=")?;
39                        res.write_num(bss)?;
40                    }
41                    Offsets::Segments { text_seg, data_seg } => {
42                        res.write_str("TextSeg=")?;
43                        res.write_num(text_seg)?;
44
45                        if let Some(data) = data_seg {
46                            res.write_str(";DataSeg=")?;
47                            res.write_num(data)?;
48                        }
49                    }
50                }
51                HandlerStatus::Handled
52            }
53        };
54
55        Ok(handler_status)
56    }
57}