Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
haproxy/scripts/minidbg.sh at master · FireBurn/haproxy · GitHub
Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Events
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
FireBurn
haproxy
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
haproxy
/
scripts
/
minidbg.sh
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
executable file
·
152 lines (137 loc) · 6.03 KB
master
Breadcrumbs
haproxy
/
scripts
/
minidbg.sh
Copy path
Top
File metadata and controls
Code
Blame
executable file
·
152 lines (137 loc) · 6.03 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!
/bin/sh
#
#
Stores a compressed symbol table into an already linked ELF file.
#
#
Copyright (C) 2026 HAProxy Technologies
#
#
This program is free software; you can redistribute it and/or
#
modify it under the terms of the GNU General Public License
#
as published by the Free Software Foundation; either version
#
2 of the License, or (at your option) any later version.
#
#
The table describes the file it is stored in, so it can only be produced once
#
that file is linked, and storing it must not move anything. The table is
#
therefore appended at the end, the .gnu_debugdata section header reserved at
#
link time is pointed at it so that gdb finds it by name, and the note segment
#
reserved at link time is turned into a loadable one covering it, so that it is
#
also readable from memory alone. The address is picked past everything the
#
file already maps.
#
#
usage: minidbg.sh <elf> <table>
:
"
${READELF
:=
readelf}
"
elf=
"
$1
"
pay=
"
$2
"
#
The loader maps the segment with mmap(), which needs its file offset and its
#
address to be congruent modulo the page size of the machine that runs it, not
#
of the one that builds it. Aligning both to the largest page size in use (64 kB
#
on arm64 and ppc64) satisfies every smaller one as well, at the cost of at most
#
that much padding once.
PAGE=65536
die
() {
echo
"
minidbg:
$1
"
>&2
;
exit
1
;
}
#
die2() is only used strictly before anything is written, and tells the
#
caller with its status of 2 that the program may be kept as is, just
#
without the table; this is how a linker that leaves no segment of its
#
own to the note (e.g. gold merges the notes) is told apart from a failure
#
that may have half-written the program.
die2
() {
echo
"
minidbg:
$1
"
>&2
;
exit
2
;
}
[
$#
=
2 ]
||
die2
"
usage:
$0
<elf> <table>
"
hdr=
$(
LC_ALL=C
"
$READELF
"
-hW
"
$elf
"
)
||
die2
"
cannot read
$elf
"
case
"
$hdr
"
in
*
ELF64
*
) w=8 ;;
*
ELF32
*
) w=4 ;;
*
) die2
"
unknown ELF class
"
;;
esac
case
"
$hdr
"
in
*
"
big endian
"
*
) be=1 ;;
*
) be=0 ;;
esac
#
an empty value would silently be taken as zero by the arithmetic below, so
#
everything read from the listings is checked before being used
num
() {
case
"
$2
"
in
'
'
|
*
[!0-9]
*
) die2
"
cannot read the
$1
of
$elf
from
$READELF
"
;;
esac
}
phoff=
$(
echo
"
$hdr
"
|
awk
'
/Start of program header/ { print $(NF-3) }
'
)
phentsize=
$(
echo
"
$hdr
"
|
awk
'
/Size of program header/ { print $(NF-1) }
'
)
shoff=
$(
echo
"
$hdr
"
|
awk
'
/Start of section header/ { print $(NF-3) }
'
)
shentsize=
$(
echo
"
$hdr
"
|
awk
'
/Size of section header/ { print $(NF-1) }
'
)
num
"
program header offset
"
"
$phoff
"
num
"
program header size
"
"
$phentsize
"
num
"
section header offset
"
"
$shoff
"
num
"
section header size
"
"
$shentsize
"
#
the two placeholders: the section gives the name, the note gives the segment.
#
Their index in the listing is not their index in the table, so the section is
#
located by the number readelf prints, and the note by where it starts.
secs=
$(
LC_ALL=C
"
$READELF
"
-SW
"
$elf
"
)
||
die2
"
cannot read the sections of
$elf
"
sec=
$(
echo
"
$secs
"
|
awk
'
{ for (i = 1; i < NF; i++) if ($i == ".gnu_debugdata") {
n = (i == 2) ? $1 : $(i - 1); gsub(/[][]/, "", n); print n; exit } }
'
)
[
-n
"
$sec
"
]
||
die2
"
no .gnu_debugdata section in
$elf
, was it reserved at link time?
"
num
"
section index
"
"
$sec
"
note=
$(
echo
"
$secs
"
|
awk
'
{ for (i = 1; i < NF; i++) if ($i == ".note.hapdbg") {
o = $(i + 3); z = $(i + 4); sub(/^0*/, "", o); sub(/^0*/, "", z)
print o " " z; exit } }
'
)
[
-n
"
$note
"
]
||
die2
"
no .note.hapdbg section in
$elf
, was it reserved at link time?
"
#
the note's own segment is the one that starts where the note does and holds
#
nothing else, so that turning it into a loadable one loses no other note
segs=
$(
LC_ALL=C
"
$READELF
"
-lW
"
$elf
"
)
||
die2
"
cannot read the segments of
$elf
"
seg=
$(
echo
"
$segs
"
|
awk -v want=
"
$note
"
'
$2 ~ /^0x/ && $3 ~ /^0x/ && $4 ~ /^0x/ {
if ($1 == "NOTE") {
o = $2; z = $5; sub(/^0x0*/, "", o); sub(/^0x0*/, "", z)
if (o " " z == want) { print idx; exit }
}
idx++
}
'
)
[
-n
"
$seg
"
]
||
die2
"
the note of
$elf
has no segment of its own
"
num
"
segment index
"
"
$seg
"
#
the table is mapped past everything the file already maps
maxva=0
for
e
in
$(
echo
"
$segs
"
|
awk
'
$1 == "LOAD" && $3 ~ /^0x/ { print $3 "+" $6 }
'
)
;
do
e=
$((
$e
))
[
$e
-gt
$maxva
]
&&
maxva=
$e
done
[
$maxva
-gt
0 ]
||
die2
"
$elf
has no loadable segment
"
paylen=
$(
wc -c
<
"
$pay
"
)
||
die2
"
cannot read
$pay
"
[
"
$paylen
"
-gt
0 ]
||
die2
"
$pay
is empty
"
elflen=
$(
wc -c
<
"
$elf
"
)
off=
$((
(elflen
+
PAGE
-
1
)
/
PAGE
*
PAGE
))
vaddr=
$((
(maxva
+
PAGE
-
1
)
/
PAGE
*
PAGE
))
#
writes <size> bytes of <value> at <offset>, in the file's byte order
#
Use escapes in the format: BSD printf's %b truncates at NUL bytes.
wr
() {
_o=
$1
;
_s=
$2
;
_v=
$3
;
_i=0
;
_e=
'
'
while
[
$_i
-lt
$_s
]
;
do
_b=
$(
printf
'
\\%03o
'
$((
(_v
>>
(
8
*
_i
))
&
255
)
))
if
[
"
$be
"
=
1 ]
;
then
_e=
"
$_b$_e
"
;
else
_e=
"
$_e$_b
"
;
fi
_i=
$((
_i
+
1
))
done
printf
"
$_e
"
|
dd of=
"
$elf
"
bs=1 seek=
$_o
conv=notrunc
2>
/dev/null
||
die
"
cannot patch
$elf
"
}
#
append at a page-aligned end, so that offset and address stay congruent
dd if=/dev/zero bs=1 count=
$((
off
-
elflen
))
2>
/dev/null
>>
"
$elf
"
||
die
"
cannot pad
$elf
"
cat
"
$pay
"
>>
"
$elf
"
||
die
"
cannot append the table to
$elf
"
if
[
$w
=
8 ]
;
then
sh_type=4
;
sh_flags=8
;
sh_addr=16
;
sh_off=24
;
sh_size=32
;
sh_align=48
ph_type=0
;
ph_flags=4
;
ph_off=8
;
ph_va=16
;
ph_pa=24
;
ph_fsz=32
;
ph_msz=40
;
ph_align=48
else
sh_type=4
;
sh_flags=8
;
sh_addr=12
;
sh_off=16
;
sh_size=20
;
sh_align=32
ph_type=0
;
ph_off=4
;
ph_va=8
;
ph_pa=12
;
ph_fsz=16
;
ph_msz=20
;
ph_flags=24
;
ph_align=28
fi
s=
$((
shoff
+
sec
*
shentsize
))
wr
$((
s
+
sh_type
))
4 1
#
SHT_PROGBITS
wr
$((
s
+
sh_flags
))
$w
2
#
SHF_ALLOC
wr
$((
s
+
sh_addr
))
$w
$vaddr
wr
$((
s
+
sh_off
))
$w
$off
wr
$((
s
+
sh_size
))
$w
$paylen
wr
$((
s
+
sh_align
))
$w
16
p=
$((
phoff
+
seg
*
phentsize
))
wr
$((
p
+
ph_type
))
4 1
#
PT_LOAD
wr
$((
p
+
ph_flags
))
4 4
#
PF_R
wr
$((
p
+
ph_off
))
$w
$off
wr
$((
p
+
ph_va
))
$w
$vaddr
wr
$((
p
+
ph_pa
))
$w
$vaddr
wr
$((
p
+
ph_fsz
))
$w
$paylen
wr
$((
p
+
ph_msz
))
$w
$paylen
wr
$((
p
+
ph_align
))
$w
$PAGE
You can’t perform that action at this time.