recr 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- recr-0.1.0/LICENSE +21 -0
- recr-0.1.0/LICENSE-Ghidra +201 -0
- recr-0.1.0/NOTICE-Ghidra +49 -0
- recr-0.1.0/PKG-INFO +271 -0
- recr-0.1.0/README.md +254 -0
- recr-0.1.0/UNLICENSE +24 -0
- recr-0.1.0/binary/__init__.py +1 -0
- recr-0.1.0/binary/binary.py +105 -0
- recr-0.1.0/binary/elf.py +228 -0
- recr-0.1.0/binary/pe.py +30 -0
- recr-0.1.0/binary/raw.py +119 -0
- recr-0.1.0/cache.py +30 -0
- recr-0.1.0/codezip.py +336 -0
- recr-0.1.0/gpu/__init__.py +58 -0
- recr-0.1.0/gpu/backend.py +344 -0
- recr-0.1.0/gpu/kernels.cu +1405 -0
- recr-0.1.0/gpu/tables.py +506 -0
- recr-0.1.0/input_parser.py +38 -0
- recr-0.1.0/ir.py +283 -0
- recr-0.1.0/isa/__init__.py +28 -0
- recr-0.1.0/isa/isa.py +239 -0
- recr-0.1.0/isa/riscv/__init__.py +5 -0
- recr-0.1.0/isa/riscv/encoding.py +67 -0
- recr-0.1.0/isa/riscv/ghidra/riscv.ilp32d.sla +203 -2
- recr-0.1.0/isa/riscv/ghidra/riscv.lp64d.sla +0 -0
- recr-0.1.0/isa/riscv/isa.py +432 -0
- recr-0.1.0/isa/riscv/liveness.py +81 -0
- recr-0.1.0/isa/riscv/references.py +233 -0
- recr-0.1.0/isa/riscv/sleigher.py +183 -0
- recr-0.1.0/isa/sleigher/__init__.py +9 -0
- recr-0.1.0/isa/sleigher/sla.py +2422 -0
- recr-0.1.0/isa/sleigher/sleigher.py +111 -0
- recr-0.1.0/isa/sleigher/x86_sleigher.py +25 -0
- recr-0.1.0/isa/x86_64/__init__.py +7 -0
- recr-0.1.0/isa/x86_64/ghidra/x86-64.sla +0 -0
- recr-0.1.0/isa/x86_64/isa.py +1651 -0
- recr-0.1.0/optimizers/__init__.py +1 -0
- recr-0.1.0/optimizers/function_remover.py +48 -0
- recr-0.1.0/optimizers/outliner.py +1499 -0
- recr-0.1.0/parsers/__init__.py +1 -0
- recr-0.1.0/parsers/base.py +23 -0
- recr-0.1.0/parsers/ghidra_xml.py +241 -0
- recr-0.1.0/parsers/yaml.py +122 -0
- recr-0.1.0/pyproject.toml +33 -0
- recr-0.1.0/recompiler/__init__.py +1 -0
- recr-0.1.0/recompiler/compaction_cache.py +118 -0
- recr-0.1.0/recompiler/compiler.py +117 -0
- recr-0.1.0/recompiler/exports.py +276 -0
- recr-0.1.0/recompiler/patchstream.py +85 -0
- recr-0.1.0/recompiler/placement.py +552 -0
- recr-0.1.0/recompiler/raw.py +221 -0
- recr-0.1.0/recompiler/riscv.py +217 -0
- recr-0.1.0/recompiler/target.py +141 -0
- recr-0.1.0/recr.egg-info/PKG-INFO +271 -0
- recr-0.1.0/recr.egg-info/SOURCES.txt +67 -0
- recr-0.1.0/recr.egg-info/dependency_links.txt +1 -0
- recr-0.1.0/recr.egg-info/entry_points.txt +3 -0
- recr-0.1.0/recr.egg-info/requires.txt +5 -0
- recr-0.1.0/recr.egg-info/top_level.txt +12 -0
- recr-0.1.0/recr.py +513 -0
- recr-0.1.0/setup.cfg +4 -0
- recr-0.1.0/test/test_codezip.py +418 -0
- recr-0.1.0/test/test_imports.py +26 -0
- recr-0.1.0/test/test_recr.py +442 -0
- recr-0.1.0/test/test_recr_exports.py +91 -0
- recr-0.1.0/test/test_recr_raw.py +810 -0
- recr-0.1.0/test/test_recr_riscv.py +331 -0
- recr-0.1.0/test/test_uplifter.py +601 -0
- recr-0.1.0/uplifter.py +346 -0
recr-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Jackson Donaldson
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
recr-0.1.0/NOTICE-Ghidra
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
Ghidra
|
|
2
|
+
|
|
3
|
+
This product includes software developed at National Security Agency
|
|
4
|
+
(https://www.nsa.gov)
|
|
5
|
+
|
|
6
|
+
Portions of this product were created by the U.S. Government and not subject to
|
|
7
|
+
U.S. copyright protections under 17 U.S.C.
|
|
8
|
+
|
|
9
|
+
The remaining portions are copyright their respective authors and have been
|
|
10
|
+
contributed under the terms of one or more open source licenses, and made
|
|
11
|
+
available to you under the terms of those licenses. (See LICENSE)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
Licensing Intent
|
|
16
|
+
|
|
17
|
+
The intent is that this software and documentation ("Project") should be treated
|
|
18
|
+
as if it is licensed under the license associated with the Project ("License")
|
|
19
|
+
in the LICENSE file. However, because we are part of the United States (U.S.)
|
|
20
|
+
Federal Government, it is not that simple.
|
|
21
|
+
|
|
22
|
+
The portions of this Project written by U.S. Federal Government employees within
|
|
23
|
+
the scope of their federal employment are ineligible for copyright protection in
|
|
24
|
+
the U.S.; this is generally understood to mean that these portions of the
|
|
25
|
+
Project are placed in the public domain.
|
|
26
|
+
|
|
27
|
+
In countries where copyright protection is available (which does not include the
|
|
28
|
+
U.S.), contributions made by U.S. Federal Government employees are released
|
|
29
|
+
under the License. Merged contributions from private contributors are released
|
|
30
|
+
under the License.
|
|
31
|
+
|
|
32
|
+
The Ghidra software is released under the Apache License, Version 2.0
|
|
33
|
+
("Apache 2.0").
|
|
34
|
+
|
|
35
|
+
In addition, each module may contain numerous 3rd party components (libraries,
|
|
36
|
+
icons, etc.) that each have their own license which is compatible with Apache
|
|
37
|
+
2.0. Each module has a LICENSE.txt file that lists each license used in that
|
|
38
|
+
module and the 3rd party files that fall under that license. The license files
|
|
39
|
+
for each license used by Ghidra can be found in the licenses directory at the
|
|
40
|
+
installation root.
|
|
41
|
+
|
|
42
|
+
Also, in the GPL directory, there are several stand-alone support programs that
|
|
43
|
+
are released using the GPL 3 license. Ghidra executes these programs as needed
|
|
44
|
+
and parses the output to get the desired results. There is a licenses directory
|
|
45
|
+
under the GPL directory that has the GPL license files.
|
|
46
|
+
|
|
47
|
+
Consistent with the inbound=outbound model, contributions to any module must be
|
|
48
|
+
made available, by the contributor, under the applicable license(s). Please read
|
|
49
|
+
the Legal section of the CONTRIBUTING.md guide.
|
recr-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: recr
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Recover unused space in compiled binaries without changing their layout.
|
|
5
|
+
License-Expression: (MIT OR Unlicense) AND Apache-2.0
|
|
6
|
+
Requires-Python: >=3.10
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
License-File: UNLICENSE
|
|
10
|
+
License-File: LICENSE-Ghidra
|
|
11
|
+
License-File: NOTICE-Ghidra
|
|
12
|
+
Requires-Dist: pyelftools>=0.31
|
|
13
|
+
Requires-Dist: PyYAML>=6.0
|
|
14
|
+
Provides-Extra: test
|
|
15
|
+
Requires-Dist: pytest>=8.0; extra == "test"
|
|
16
|
+
Dynamic: license-file
|
|
17
|
+
|
|
18
|
+
# codezip
|
|
19
|
+
|
|
20
|
+
Modify an already-compiled binary to maximize unused space without changing
|
|
21
|
+
its semantics or its overall layout. Function entry points and extents stay
|
|
22
|
+
fixed; instructions may move within a function, and data is never appended.
|
|
23
|
+
|
|
24
|
+
## Status
|
|
25
|
+
|
|
26
|
+
The input parsers, ELF backend, x86-64 disassembler, function uplifter,
|
|
27
|
+
cross-reference recovery, unreferenced-function free-space detection, and CLI
|
|
28
|
+
wiring through writing the modified binary are implemented. The pipeline fills
|
|
29
|
+
every freed range with `0xff`, writes the result to the output path, and can
|
|
30
|
+
write a YAML receipt of freed and rewritten ranges. The outliner finds
|
|
31
|
+
profitable basic-block matches, extracts them into free space, replaces source
|
|
32
|
+
sites with calls, and regenerates the affected functions. Verification is still
|
|
33
|
+
a stub, so the written binary is unverified.
|
|
34
|
+
|
|
35
|
+
## Planned pipeline
|
|
36
|
+
|
|
37
|
+
1. **Parse** a reverse-engineering tool export (YAML or Ghidra XML) into
|
|
38
|
+
function records.
|
|
39
|
+
2. **Uplift** each function into instructions, basic blocks, cross-references,
|
|
40
|
+
and a control-flow graph.
|
|
41
|
+
3. **Optimize**:
|
|
42
|
+
- remove unreferenced functions and fill their bytes with `0xff`;
|
|
43
|
+
- outline repeated instruction regions aggressively.
|
|
44
|
+
|
|
45
|
+
The current CLI performs steps 1 and 2 and the free-space part of step 3, then
|
|
46
|
+
writes the modified binary.
|
|
47
|
+
|
|
48
|
+
## Layout
|
|
49
|
+
|
|
50
|
+
| Path | Purpose |
|
|
51
|
+
| --- | --- |
|
|
52
|
+
| `codezip.py` | CLI orchestrator driving parse → uplift → optimize → write |
|
|
53
|
+
| `input_parser.py` | Parser selection and entry point |
|
|
54
|
+
| `parsers/yaml.py` | YAML function export parser |
|
|
55
|
+
| `parsers/ghidra_xml.py` | Ghidra XML function export parser |
|
|
56
|
+
| `parsers/base.py` | Shared `ParseResult` type |
|
|
57
|
+
| `ir.py` | IR data model: `Address`, `StateSpace`, `Instruction`, `BasicBlock`, `Function`, `Reference`, `FreeSpace` |
|
|
58
|
+
| `ir.md` | Normative IR and ISA contract |
|
|
59
|
+
| `uplifter.py` | Function → IR conversion and cross-reference recovery |
|
|
60
|
+
| `isa/isa.py` | ISA superclass and encoding contract |
|
|
61
|
+
| `isa/sleigher/` | Direct parser/interpreter for compiled SLEIGH `.sla` files |
|
|
62
|
+
| `isa/x86_64/` | x86-64 backend and pinned Ghidra 12.2 SLEIGH spec |
|
|
63
|
+
| `optimizers/function_remover.py` | Unreferenced-function removal |
|
|
64
|
+
| `optimizers/outliner.py` | Repeated-region outlining |
|
|
65
|
+
| `binary/binary.py` | Binary file interface and format routing |
|
|
66
|
+
| `binary/elf.py` | ELF backend |
|
|
67
|
+
| `binary/pe.py` | PE backend (stub) |
|
|
68
|
+
| `binary/raw.py` | Explicit virtual-to-file mappings for raw images |
|
|
69
|
+
| `test/` | Grouped pytest suites, sample C input, NASM fixtures, and Ghidra exports |
|
|
70
|
+
|
|
71
|
+
## Input formats
|
|
72
|
+
|
|
73
|
+
YAML exports use `.yaml` or `.yml`. Use integer scalars for addresses, offsets,
|
|
74
|
+
and lengths. Use `-1` when the file offset is unknown. Extra top-level fields
|
|
75
|
+
are ignored.
|
|
76
|
+
|
|
77
|
+
```yaml
|
|
78
|
+
isa: x86_64
|
|
79
|
+
functions:
|
|
80
|
+
main:
|
|
81
|
+
start_address: 0x401020
|
|
82
|
+
file_offset: 0x1020
|
|
83
|
+
length: 0x40
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Ghidra XML: see `test/fixtures/sample_ghidra.xml`. Ghidra exports virtual
|
|
87
|
+
addresses; the active `Binary` implementation maps them to file offsets.
|
|
88
|
+
|
|
89
|
+
## Usage
|
|
90
|
+
|
|
91
|
+
Install from the repository with Python 3.10 or newer.
|
|
92
|
+
|
|
93
|
+
```sh
|
|
94
|
+
python -m pip install -e .
|
|
95
|
+
codezip ./a.out ./functions.xml -o ./a.out.codezip
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Add `-v` or `--verbose` to log pipeline progress and pretty-print every
|
|
99
|
+
uplifted function. The command writes the modified binary to `-o` (default:
|
|
100
|
+
`<binary>.codezip`). Verification is not implemented yet.
|
|
101
|
+
|
|
102
|
+
The temporary `--report-outline-matches` option prints every profitable
|
|
103
|
+
basic-block match and exits with status 1 without writing an output binary.
|
|
104
|
+
|
|
105
|
+
Pass `--receipt PATH` to also write a YAML receipt of the freed ranges and the
|
|
106
|
+
live code ranges rewritten by outlining:
|
|
107
|
+
|
|
108
|
+
```yaml
|
|
109
|
+
freed:
|
|
110
|
+
- address: 0x4010a0
|
|
111
|
+
length: 0x5
|
|
112
|
+
modified:
|
|
113
|
+
- address: 0x401156
|
|
114
|
+
length: 0x9
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
## Recompilation with recr
|
|
118
|
+
|
|
119
|
+
`recr` compiles a C extension for an x86-64, RV32, or RV64 non-PIE ELF using a
|
|
120
|
+
typed Ghidra XML export. Prefix a function definition with `@` to hook the
|
|
121
|
+
original function. Use the final component of a qualified Ghidra name, such as
|
|
122
|
+
`@AppVprintf` for `main_app::AppVprintf`. A hook name usually identifies one
|
|
123
|
+
function. When exactly two entries match and the shorter is under 8 bytes,
|
|
124
|
+
recr selects the longer entry and warns about the presumed thunk. Use
|
|
125
|
+
`-Wno-ignoring-thunk-function` to silence that warning. Calling the hook name
|
|
126
|
+
normally from the extension invokes the selected original through a trampoline.
|
|
127
|
+
C definitions in the extension take precedence over other imported names.
|
|
128
|
+
|
|
129
|
+
```sh
|
|
130
|
+
recr ./a.out ./functions.xml ./hooks.rec -o ./a.out.recr -- -Iinclude -Llib -lexample
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
Use `-p FILE` or `--patchstream FILE` to also save a binary patch stream for
|
|
134
|
+
an ELF or raw image. The stream contains the extension and the rewrites it
|
|
135
|
+
needs to use reclaimed space. Each record contains a virtual address, a byte
|
|
136
|
+
count, and that many replacement bytes. The address and count are unsigned
|
|
137
|
+
little-endian words, 4 bytes on RV32 and 8 bytes on RV64 and x86-64. Records
|
|
138
|
+
cover contiguous changed bytes within a file-backed mapping. A single
|
|
139
|
+
all-`0xff` address word terminates the stream.
|
|
140
|
+
|
|
141
|
+
GCC accepts extra compiler flags, object files, and static libraries after
|
|
142
|
+
`--`. Before `--`, `-Wall` enables all recr warnings and `-Werror` makes
|
|
143
|
+
enabled warnings fatal. Recr also passes both flags to GCC. Calls can use
|
|
144
|
+
shared-library imports already present in the original ELF. Recr uses Ghidra's
|
|
145
|
+
scalar, pointer, and array types for implicit declarations. Ghidra `string`
|
|
146
|
+
data is declared as a C `char[]`.
|
|
147
|
+
Each hook must match the original function's prototype. Recr uses the
|
|
148
|
+
prototype on the selected Ghidra function entry to declare the original for
|
|
149
|
+
calls from the extension. Ghidra does not always include function prototypes
|
|
150
|
+
in exports. If the prototype is missing, edit the function in Ghidra, select
|
|
151
|
+
**Commit all return/parameter details**, and export the XML again. Supply a C
|
|
152
|
+
declaration for other imports with missing or unsupported type metadata.
|
|
153
|
+
|
|
154
|
+
For RISC-V `__thiscall` functions, Ghidra must export `this` in `a0` as a
|
|
155
|
+
pointer to a named structure. Declare that pointer as the first C parameter
|
|
156
|
+
in the hook. A variadic hook can read arguments with `<stdarg.h>`. When it
|
|
157
|
+
calls the original, supply the variadic arguments explicitly. Pass a
|
|
158
|
+
`va_list` to a function declared to receive one.
|
|
159
|
+
|
|
160
|
+
Code can be split across executable receipt ranges. Each data section needs
|
|
161
|
+
a contiguous range and writable sections require writable space. Placement
|
|
162
|
+
fails if the extension cannot fit. The sample hook and test-only space are in
|
|
163
|
+
`test/fixtures/sample.rec` and `test/fixtures/recr_space.S`.
|
|
164
|
+
|
|
165
|
+
RISC-V builds default to `riscv64-linux-gnu-gcc`. Use `--cc` to select a
|
|
166
|
+
different cross-compiler. Hook and fragment jumps must reach their targets
|
|
167
|
+
with a single `JAL` so they leave registers intact. The RV64 sample and both
|
|
168
|
+
RISC-V hook tests run under QEMU in `test/test_recr_riscv.py`.
|
|
169
|
+
|
|
170
|
+
### Raw firmware images
|
|
171
|
+
|
|
172
|
+
Pass a `.yaml` or `.yml` manifest and the `.rec` file for a statically linked
|
|
173
|
+
raw image. Relative binary paths are resolved from the manifest directory.
|
|
174
|
+
The default output is `<binary>.recr`.
|
|
175
|
+
|
|
176
|
+
```sh
|
|
177
|
+
recr ./firmware.yaml ./hooks.rec -o ./firmware.patched.bin
|
|
178
|
+
recr ./firmware.yaml ./functions.xml ./hooks.rec -o ./firmware.patched.bin
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
```yaml
|
|
182
|
+
binary: firmware.bin
|
|
183
|
+
isa: x86_64
|
|
184
|
+
regions:
|
|
185
|
+
- {start_address: 0x400000, file_offset: 0x10, length: 0x70}
|
|
186
|
+
- {start_address: 0x410000, file_offset: 0x80, length: 0x100, permissions: rwx}
|
|
187
|
+
- {start_address: 0x420000, file_offset: 0x180, length: 0x10, permissions: rw}
|
|
188
|
+
- {start_address: 0x420010, length: 0x100, permissions: rw}
|
|
189
|
+
entry_points: [0x400000]
|
|
190
|
+
functions:
|
|
191
|
+
_start: {start_address: 0x400000, length: 0x20}
|
|
192
|
+
transform:
|
|
193
|
+
start_address: 0x400020
|
|
194
|
+
length: 0x10
|
|
195
|
+
declaration: "extern int transform(int value);"
|
|
196
|
+
spare: {start_address: 0x410000, length: 0x100}
|
|
197
|
+
symbols:
|
|
198
|
+
counter: {address: 0x420000, declaration: "extern int counter;"}
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
Give each region a virtual start and byte length. Provide `file_offset` for
|
|
202
|
+
file-backed regions. Omit it for runtime-only memory such as BSS. Recr can
|
|
203
|
+
resolve Ghidra data symbols in those regions. Scanning, hook placement, and
|
|
204
|
+
binary writes are limited to file-backed regions. Regions may appear in any
|
|
205
|
+
order and must be disjoint in virtual memory and, where backed, in the file.
|
|
206
|
+
Permissions default to `rx`. Include file-backed data regions containing
|
|
207
|
+
pointers so codezip can scan them for live functions. List reset, interrupt,
|
|
208
|
+
and other entry addresses that firmware can reach from outside the mapped
|
|
209
|
+
image under `entry_points` so codezip preserves their functions.
|
|
210
|
+
|
|
211
|
+
Use `functions` to identify hooks and give codezip the extents it can analyze
|
|
212
|
+
for reclaimable space. Give every hooked function its original C prototype in
|
|
213
|
+
`declaration` when the manifest supplies the function records. Recr skips
|
|
214
|
+
functions whose full extent falls outside a single file-backed executable
|
|
215
|
+
region and warns by default. Use
|
|
216
|
+
`-Wno-function-outside-executable-range` to suppress this warning, including
|
|
217
|
+
with `-Wall` or `-Werror`. Recr also skips functions shorter than the target's
|
|
218
|
+
instruction alignment and warns by default. Use `-Wno-skip-short-functions`
|
|
219
|
+
to suppress that warning. The file offset of each retained function is
|
|
220
|
+
derived from the mapping. If you pass a Ghidra XML export,
|
|
221
|
+
it supplies the ISA, functions, and C types, so omit `isa` and `functions`
|
|
222
|
+
from the manifest. `symbols` binds additional names to fixed virtual addresses.
|
|
223
|
+
An optional `declaration` supplies the C declaration implicitly visible in
|
|
224
|
+
the `.rec` file. Raw RISC-V manifests must include the original architecture
|
|
225
|
+
and ABI, for example `riscv: {march: rv64gc, abi: lp64d}` with `isa: riscv64`.
|
|
226
|
+
|
|
227
|
+
## Development
|
|
228
|
+
|
|
229
|
+
```sh
|
|
230
|
+
python -m pip install -e '.[test]'
|
|
231
|
+
python -m pytest -q
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
To uplift and pretty-print every function in the sample input:
|
|
235
|
+
|
|
236
|
+
```sh
|
|
237
|
+
python test/uplift_sample.py
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
An opt-in end-to-end check builds pinned pcre2, rewrites its test executables
|
|
241
|
+
with codezip, re-runs pcre2's suite, and runs pcre2's pattern corpus through
|
|
242
|
+
`pcre2grep` to exercise its statically linked library (requires Ghidra and
|
|
243
|
+
network access, about 4 minutes):
|
|
244
|
+
|
|
245
|
+
```sh
|
|
246
|
+
make -C test e2e
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
The full check also runs on RV64 under QEMU. The little-endian RV32 and RV64
|
|
250
|
+
backends support function removal, instruction relocation, and outlining.
|
|
251
|
+
|
|
252
|
+
```sh
|
|
253
|
+
make -C test e2e-riscv
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
To check that the suite would notice a corrupted rewrite, run the mutation
|
|
257
|
+
sensitivity check after it:
|
|
258
|
+
|
|
259
|
+
```sh
|
|
260
|
+
make -C test e2e-mutation
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
The optimization pipeline now runs function removal and outlining before
|
|
264
|
+
writing the output.
|
|
265
|
+
|
|
266
|
+
## License
|
|
267
|
+
|
|
268
|
+
You can use the original project code under either the [MIT License](LICENSE)
|
|
269
|
+
or the [Unlicense](UNLICENSE). The bundled Ghidra 12.2 processor specifications
|
|
270
|
+
are distributed under the [Apache License 2.0](LICENSE-Ghidra) with the
|
|
271
|
+
[Ghidra attribution notice](NOTICE-Ghidra).
|