Commit eae15c1
authored
Before this change, `LocateFlapiSectionInBuffer` returned `nullopt`
for any input whose magic wasn't `MH_MAGIC_64`. Universal binaries
(`FAT_MAGIC` / `FAT_MAGIC_64`) -- the format produced by `lipo
-create` and consumed by Homebrew-style formulas that ship both
arches -- therefore couldn't be packed or self-inspected.
A universal binary is a small big-endian fat header followed by N
thin Mach-O slices at distinct file offsets. The `section_64.offset`
read inside a slice is relative to the slice's base, not the fat
file -- both `OverwriteFlapiSection` (write side) and
`LocateBundleInRange` (read side) treat the returned offset as an
absolute file offset, so a naive accept-fat patch would have written
to the wrong byte address.
This PR:
- Adds `ReadU32BE` / `ReadU64BE` -- fat headers are big-endian on
disk regardless of host endianness.
- Adds `ParseFatHeader` (namespace-private). Walks `fat_arch[]`
(20-byte records for `FAT_MAGIC/CIGAM`, 32-byte for the `_64`
variants). Selection rule: first slice whose cputype matches the
host arch (compile-time, via `__aarch64__` / `__x86_64__`), else
the first slice. Rejects malformed `nfat_arch` (0 or > 64) and
slice extents past EOF.
- Splits `LocateFlapiSectionInBuffer` into an outer dispatch + an
inner `LocateFlapiSectionAt(buffer, base)` overload. On fat input
the outer call parses the fat header, picks a slice, and recurses
via the inner overload with the slice's absolute offset as base.
The inner overload adds base to whatever the load-cmd walker
returns, so callers see an absolute file offset.
- `IsMachOMagic` extended to recognise `FAT_MAGIC_64` / `FAT_CIGAM_64`
in addition to the 32-bit fat variants it already accepted.
- `OverwriteFlapiSection` is unchanged -- the new absolute-offset
invariant makes its existing `seekp(file_offset)` land in the
correct slice automatically.
- Header doc updated to drop the "fat / universal not supported"
caveat and document the slice-selection rule.
Test fixture: new `BuildFatMachO(slices)` helper wraps any number
of `BuildMachO64` outputs with a big-endian fat header + 4 KiB-
aligned slice placement. Four new test cases (issue #66 acceptance):
- single-slice fat: section located at the absolute offset
(slice_offset + intra-slice section offset).
- two-slice fat (arm64 + x86_64): parser picks the host-matching
slice; deterministic per-host expected offset via `#ifdef`.
- two PPC slices (host arch doesn't match either): parser falls back
to the first slice and stays deterministic across exotic hosts.
- OverwriteFlapiSection round-trip inside a slice: write a 7-byte
payload through the located section, verify the bytes land at the
returned absolute offset, and confirm a re-locate finds the same
section.
Test plan:
- ctest: 642 / 642 pass (637 previous + 5 new -- 4 fat cases +
the round-trip).
- pytest test_self_packaging.py + test_self_packaging_http.py:
11 / 11 pass (no regression on the Linux EOCD tail-scan path,
which is unaffected by this change).
Closes #66.
1 parent 1779bc9 commit eae15c1
3 files changed
Lines changed: 397 additions & 18 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
32 | | - | |
| 32 | + | |
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
36 | | - | |
37 | | - | |
38 | | - | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
39 | 45 | | |
40 | 46 | | |
41 | 47 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
| 33 | + | |
| 34 | + | |
33 | 35 | | |
34 | 36 | | |
35 | 37 | | |
36 | 38 | | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
37 | 58 | | |
38 | 59 | | |
39 | 60 | | |
| |||
46 | 67 | | |
47 | 68 | | |
48 | 69 | | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
49 | 84 | | |
50 | 85 | | |
51 | 86 | | |
| |||
177 | 212 | | |
178 | 213 | | |
179 | 214 | | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
180 | 333 | | |
181 | 334 | | |
182 | 335 | | |
183 | 336 | | |
184 | 337 | | |
185 | 338 | | |
186 | | - | |
| 339 | + | |
| 340 | + | |
187 | 341 | | |
188 | 342 | | |
189 | 343 | | |
| |||
193 | 347 | | |
194 | 348 | | |
195 | 349 | | |
196 | | - | |
197 | | - | |
198 | | - | |
199 | | - | |
200 | | - | |
201 | | - | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
202 | 362 | | |
203 | 363 | | |
204 | | - | |
205 | | - | |
206 | | - | |
207 | | - | |
208 | | - | |
209 | | - | |
210 | | - | |
| 364 | + | |
211 | 365 | | |
212 | 366 | | |
213 | 367 | | |
| |||
0 commit comments