├── .gitignore ├── Cargo.lock ├── Cargo.toml ├── LICENSE ├── README.md └── src └── main.rs /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | **/*.rs.bk 3 | -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "addr2line" 5 | version = "0.15.1" 6 | source = "registry+https://github.com/rust-lang/crates.io-index" 7 | checksum = "03345e98af8f3d786b6d9f656ccfa6ac316d954e92bc4841f0bba20789d5fb5a" 8 | dependencies = [ 9 | "gimli 0.24.0", 10 | ] 11 | 12 | [[package]] 13 | name = "adler" 14 | version = "1.0.2" 15 | source = "registry+https://github.com/rust-lang/crates.io-index" 16 | checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" 17 | 18 | [[package]] 19 | name = "ansi_term" 20 | version = "0.11.0" 21 | source = "registry+https://github.com/rust-lang/crates.io-index" 22 | checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" 23 | dependencies = [ 24 | "winapi", 25 | ] 26 | 27 | [[package]] 28 | name = "arrayvec" 29 | version = "0.4.12" 30 | source = "registry+https://github.com/rust-lang/crates.io-index" 31 | checksum = "cd9fd44efafa8690358b7408d253adf110036b88f55672a933f01d616ad9b1b9" 32 | dependencies = [ 33 | "nodrop", 34 | ] 35 | 36 | [[package]] 37 | name = "atty" 38 | version = "0.2.14" 39 | source = "registry+https://github.com/rust-lang/crates.io-index" 40 | checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" 41 | dependencies = [ 42 | "hermit-abi", 43 | "libc", 44 | "winapi", 45 | ] 46 | 47 | [[package]] 48 | name = "autocfg" 49 | version = "1.0.1" 50 | source = "registry+https://github.com/rust-lang/crates.io-index" 51 | checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" 52 | 53 | [[package]] 54 | name = "backtrace" 55 | version = "0.3.60" 56 | source = "registry+https://github.com/rust-lang/crates.io-index" 57 | checksum = "b7815ea54e4d821e791162e078acbebfd6d8c8939cd559c9335dceb1c8ca7282" 58 | dependencies = [ 59 | "addr2line", 60 | "cc", 61 | "cfg-if", 62 | "libc", 63 | "miniz_oxide", 64 | "object 0.25.1", 65 | "rustc-demangle", 66 | ] 67 | 68 | [[package]] 69 | name = "bitflags" 70 | version = "1.2.1" 71 | source = "registry+https://github.com/rust-lang/crates.io-index" 72 | checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" 73 | 74 | [[package]] 75 | name = "byteorder" 76 | version = "1.4.3" 77 | source = "registry+https://github.com/rust-lang/crates.io-index" 78 | checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" 79 | 80 | [[package]] 81 | name = "cc" 82 | version = "1.0.68" 83 | source = "registry+https://github.com/rust-lang/crates.io-index" 84 | checksum = "4a72c244c1ff497a746a7e1fb3d14bd08420ecda70c8f25c7112f2781652d787" 85 | 86 | [[package]] 87 | name = "cfg-if" 88 | version = "1.0.0" 89 | source = "registry+https://github.com/rust-lang/crates.io-index" 90 | checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" 91 | 92 | [[package]] 93 | name = "clap" 94 | version = "2.33.3" 95 | source = "registry+https://github.com/rust-lang/crates.io-index" 96 | checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002" 97 | dependencies = [ 98 | "ansi_term", 99 | "atty", 100 | "bitflags", 101 | "strsim", 102 | "textwrap", 103 | "unicode-width", 104 | "vec_map", 105 | ] 106 | 107 | [[package]] 108 | name = "crc32fast" 109 | version = "1.2.1" 110 | source = "registry+https://github.com/rust-lang/crates.io-index" 111 | checksum = "81156fece84ab6a9f2afdb109ce3ae577e42b1228441eded99bd77f627953b1a" 112 | dependencies = [ 113 | "cfg-if", 114 | ] 115 | 116 | [[package]] 117 | name = "failure" 118 | version = "0.1.8" 119 | source = "registry+https://github.com/rust-lang/crates.io-index" 120 | checksum = "d32e9bd16cc02eae7db7ef620b392808b89f6a5e16bb3497d159c6b92a0f4f86" 121 | dependencies = [ 122 | "backtrace", 123 | "failure_derive", 124 | ] 125 | 126 | [[package]] 127 | name = "failure_derive" 128 | version = "0.1.8" 129 | source = "registry+https://github.com/rust-lang/crates.io-index" 130 | checksum = "aa4da3c766cd7a0db8242e326e9e4e081edd567072893ed320008189715366a4" 131 | dependencies = [ 132 | "proc-macro2 1.0.27", 133 | "quote 1.0.9", 134 | "syn 1.0.72", 135 | "synstructure", 136 | ] 137 | 138 | [[package]] 139 | name = "fallible-iterator" 140 | version = "0.1.6" 141 | source = "registry+https://github.com/rust-lang/crates.io-index" 142 | checksum = "eb7217124812dc5672b7476d0c2d20cfe9f7c0f1ba0904b674a9762a0212f72e" 143 | 144 | [[package]] 145 | name = "flate2" 146 | version = "1.0.20" 147 | source = "registry+https://github.com/rust-lang/crates.io-index" 148 | checksum = "cd3aec53de10fe96d7d8c565eb17f2c687bb5518a2ec453b5b1252964526abe0" 149 | dependencies = [ 150 | "cfg-if", 151 | "crc32fast", 152 | "libc", 153 | "miniz_oxide", 154 | ] 155 | 156 | [[package]] 157 | name = "gimli" 158 | version = "0.16.1" 159 | source = "registry+https://github.com/rust-lang/crates.io-index" 160 | checksum = "9a7f6ee5390883802431e4abe323390f52f10ff16e8f8d2d6ce598251f900ede" 161 | dependencies = [ 162 | "arrayvec", 163 | "byteorder", 164 | "fallible-iterator", 165 | "stable_deref_trait", 166 | ] 167 | 168 | [[package]] 169 | name = "gimli" 170 | version = "0.24.0" 171 | source = "registry+https://github.com/rust-lang/crates.io-index" 172 | checksum = "0e4075386626662786ddb0ec9081e7c7eeb1ba31951f447ca780ef9f5d568189" 173 | 174 | [[package]] 175 | name = "goblin" 176 | version = "0.0.19" 177 | source = "registry+https://github.com/rust-lang/crates.io-index" 178 | checksum = "c65cd533b33e3d04c6e393225fa8919ddfcf5862ca8919c7f9a167c312ef41c2" 179 | dependencies = [ 180 | "log", 181 | "plain", 182 | "scroll", 183 | ] 184 | 185 | [[package]] 186 | name = "hermit-abi" 187 | version = "0.1.18" 188 | source = "registry+https://github.com/rust-lang/crates.io-index" 189 | checksum = "322f4de77956e22ed0e5032c359a0f1273f1f7f0d79bfa3b8ffbc730d7fbcc5c" 190 | dependencies = [ 191 | "libc", 192 | ] 193 | 194 | [[package]] 195 | name = "libc" 196 | version = "0.2.95" 197 | source = "registry+https://github.com/rust-lang/crates.io-index" 198 | checksum = "789da6d93f1b866ffe175afc5322a4d76c038605a1c3319bb57b06967ca98a36" 199 | 200 | [[package]] 201 | name = "log" 202 | version = "0.4.14" 203 | source = "registry+https://github.com/rust-lang/crates.io-index" 204 | checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" 205 | dependencies = [ 206 | "cfg-if", 207 | ] 208 | 209 | [[package]] 210 | name = "memchr" 211 | version = "2.4.0" 212 | source = "registry+https://github.com/rust-lang/crates.io-index" 213 | checksum = "b16bd47d9e329435e309c58469fe0791c2d0d1ba96ec0954152a5ae2b04387dc" 214 | 215 | [[package]] 216 | name = "miniz_oxide" 217 | version = "0.4.4" 218 | source = "registry+https://github.com/rust-lang/crates.io-index" 219 | checksum = "a92518e98c078586bc6c934028adcca4c92a53d6a958196de835170a01d84e4b" 220 | dependencies = [ 221 | "adler", 222 | "autocfg", 223 | ] 224 | 225 | [[package]] 226 | name = "nodrop" 227 | version = "0.1.14" 228 | source = "registry+https://github.com/rust-lang/crates.io-index" 229 | checksum = "72ef4a56884ca558e5ddb05a1d1e7e1bfd9a68d9ed024c21704cc98872dae1bb" 230 | 231 | [[package]] 232 | name = "object" 233 | version = "0.11.0" 234 | source = "registry+https://github.com/rust-lang/crates.io-index" 235 | checksum = "2e7934267669d9b8637926ed243df1b917b40e43dd070aea2a178296e6ed1b72" 236 | dependencies = [ 237 | "flate2", 238 | "goblin", 239 | "parity-wasm", 240 | "scroll", 241 | "uuid", 242 | ] 243 | 244 | [[package]] 245 | name = "object" 246 | version = "0.25.1" 247 | source = "registry+https://github.com/rust-lang/crates.io-index" 248 | checksum = "9023c1c0973b327f073c7f2fceb9bcc049862f93a7d14c6feb46c8a56460a0d5" 249 | dependencies = [ 250 | "memchr", 251 | ] 252 | 253 | [[package]] 254 | name = "parity-wasm" 255 | version = "0.35.7" 256 | source = "registry+https://github.com/rust-lang/crates.io-index" 257 | checksum = "3e1e076c4e01399b6cd0793a8df42f90bba3ae424671ef421d1608a943155d93" 258 | dependencies = [ 259 | "byteorder", 260 | ] 261 | 262 | [[package]] 263 | name = "plain" 264 | version = "0.2.3" 265 | source = "registry+https://github.com/rust-lang/crates.io-index" 266 | checksum = "b4596b6d070b27117e987119b4dac604f3c58cfb0b191112e24771b2faeac1a6" 267 | 268 | [[package]] 269 | name = "proc-macro2" 270 | version = "0.4.30" 271 | source = "registry+https://github.com/rust-lang/crates.io-index" 272 | checksum = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759" 273 | dependencies = [ 274 | "unicode-xid 0.1.0", 275 | ] 276 | 277 | [[package]] 278 | name = "proc-macro2" 279 | version = "1.0.27" 280 | source = "registry+https://github.com/rust-lang/crates.io-index" 281 | checksum = "f0d8caf72986c1a598726adc988bb5984792ef84f5ee5aa50209145ee8077038" 282 | dependencies = [ 283 | "unicode-xid 0.2.2", 284 | ] 285 | 286 | [[package]] 287 | name = "quote" 288 | version = "0.6.13" 289 | source = "registry+https://github.com/rust-lang/crates.io-index" 290 | checksum = "6ce23b6b870e8f94f81fb0a363d65d86675884b34a09043c81e5562f11c1f8e1" 291 | dependencies = [ 292 | "proc-macro2 0.4.30", 293 | ] 294 | 295 | [[package]] 296 | name = "quote" 297 | version = "1.0.9" 298 | source = "registry+https://github.com/rust-lang/crates.io-index" 299 | checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7" 300 | dependencies = [ 301 | "proc-macro2 1.0.27", 302 | ] 303 | 304 | [[package]] 305 | name = "rustc-demangle" 306 | version = "0.1.19" 307 | source = "registry+https://github.com/rust-lang/crates.io-index" 308 | checksum = "410f7acf3cb3a44527c5d9546bad4bf4e6c460915d5f9f2fc524498bfe8f70ce" 309 | 310 | [[package]] 311 | name = "rustc_version" 312 | version = "0.2.3" 313 | source = "registry+https://github.com/rust-lang/crates.io-index" 314 | checksum = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" 315 | dependencies = [ 316 | "semver", 317 | ] 318 | 319 | [[package]] 320 | name = "scroll" 321 | version = "0.9.2" 322 | source = "registry+https://github.com/rust-lang/crates.io-index" 323 | checksum = "2f84d114ef17fd144153d608fba7c446b0145d038985e7a8cc5d08bb0ce20383" 324 | dependencies = [ 325 | "rustc_version", 326 | "scroll_derive", 327 | ] 328 | 329 | [[package]] 330 | name = "scroll_derive" 331 | version = "0.9.5" 332 | source = "registry+https://github.com/rust-lang/crates.io-index" 333 | checksum = "8f1aa96c45e7f5a91cb7fabe7b279f02fea7126239fc40b732316e8b6a2d0fcb" 334 | dependencies = [ 335 | "proc-macro2 0.4.30", 336 | "quote 0.6.13", 337 | "syn 0.15.44", 338 | ] 339 | 340 | [[package]] 341 | name = "semver" 342 | version = "0.9.0" 343 | source = "registry+https://github.com/rust-lang/crates.io-index" 344 | checksum = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" 345 | dependencies = [ 346 | "semver-parser", 347 | ] 348 | 349 | [[package]] 350 | name = "semver-parser" 351 | version = "0.7.0" 352 | source = "registry+https://github.com/rust-lang/crates.io-index" 353 | checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" 354 | 355 | [[package]] 356 | name = "stable_deref_trait" 357 | version = "1.2.0" 358 | source = "registry+https://github.com/rust-lang/crates.io-index" 359 | checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" 360 | 361 | [[package]] 362 | name = "strsim" 363 | version = "0.8.0" 364 | source = "registry+https://github.com/rust-lang/crates.io-index" 365 | checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" 366 | 367 | [[package]] 368 | name = "structpack" 369 | version = "0.1.0" 370 | dependencies = [ 371 | "clap", 372 | "failure", 373 | "gimli 0.16.1", 374 | "object 0.11.0", 375 | ] 376 | 377 | [[package]] 378 | name = "syn" 379 | version = "0.15.44" 380 | source = "registry+https://github.com/rust-lang/crates.io-index" 381 | checksum = "9ca4b3b69a77cbe1ffc9e198781b7acb0c7365a883670e8f1c1bc66fba79a5c5" 382 | dependencies = [ 383 | "proc-macro2 0.4.30", 384 | "quote 0.6.13", 385 | "unicode-xid 0.1.0", 386 | ] 387 | 388 | [[package]] 389 | name = "syn" 390 | version = "1.0.72" 391 | source = "registry+https://github.com/rust-lang/crates.io-index" 392 | checksum = "a1e8cdbefb79a9a5a65e0db8b47b723ee907b7c7f8496c76a1770b5c310bab82" 393 | dependencies = [ 394 | "proc-macro2 1.0.27", 395 | "quote 1.0.9", 396 | "unicode-xid 0.2.2", 397 | ] 398 | 399 | [[package]] 400 | name = "synstructure" 401 | version = "0.12.4" 402 | source = "registry+https://github.com/rust-lang/crates.io-index" 403 | checksum = "b834f2d66f734cb897113e34aaff2f1ab4719ca946f9a7358dba8f8064148701" 404 | dependencies = [ 405 | "proc-macro2 1.0.27", 406 | "quote 1.0.9", 407 | "syn 1.0.72", 408 | "unicode-xid 0.2.2", 409 | ] 410 | 411 | [[package]] 412 | name = "textwrap" 413 | version = "0.11.0" 414 | source = "registry+https://github.com/rust-lang/crates.io-index" 415 | checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" 416 | dependencies = [ 417 | "unicode-width", 418 | ] 419 | 420 | [[package]] 421 | name = "unicode-width" 422 | version = "0.1.8" 423 | source = "registry+https://github.com/rust-lang/crates.io-index" 424 | checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" 425 | 426 | [[package]] 427 | name = "unicode-xid" 428 | version = "0.1.0" 429 | source = "registry+https://github.com/rust-lang/crates.io-index" 430 | checksum = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" 431 | 432 | [[package]] 433 | name = "unicode-xid" 434 | version = "0.2.2" 435 | source = "registry+https://github.com/rust-lang/crates.io-index" 436 | checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" 437 | 438 | [[package]] 439 | name = "uuid" 440 | version = "0.7.4" 441 | source = "registry+https://github.com/rust-lang/crates.io-index" 442 | checksum = "90dbc611eb48397705a6b0f6e917da23ae517e4d127123d2cf7674206627d32a" 443 | 444 | [[package]] 445 | name = "vec_map" 446 | version = "0.8.2" 447 | source = "registry+https://github.com/rust-lang/crates.io-index" 448 | checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" 449 | 450 | [[package]] 451 | name = "winapi" 452 | version = "0.3.9" 453 | source = "registry+https://github.com/rust-lang/crates.io-index" 454 | checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" 455 | dependencies = [ 456 | "winapi-i686-pc-windows-gnu", 457 | "winapi-x86_64-pc-windows-gnu", 458 | ] 459 | 460 | [[package]] 461 | name = "winapi-i686-pc-windows-gnu" 462 | version = "0.4.0" 463 | source = "registry+https://github.com/rust-lang/crates.io-index" 464 | checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" 465 | 466 | [[package]] 467 | name = "winapi-x86_64-pc-windows-gnu" 468 | version = "0.4.0" 469 | source = "registry+https://github.com/rust-lang/crates.io-index" 470 | checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" 471 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "structpack" 3 | version = "0.1.0" 4 | authors = ["dolson"] 5 | edition = "2018" 6 | license = "Apache-2.0" 7 | 8 | [dependencies] 9 | gimli = "0.16.1" 10 | object = "0.11.0" 11 | failure = "0.1.5" 12 | clap = "2.32.0" 13 | 14 | [profile.release] 15 | debug = true 16 | 17 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 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 2021 Activision Publishing, Inc. 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. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # structpack 2 | 3 | This tool queries the DWARF data inside of an ELF file and prints a list of structures that could take up less space if their members were rearranged to account for padding. 4 | -------------------------------------------------------------------------------- /src/main.rs: -------------------------------------------------------------------------------- 1 | // Copyright 2021 Activision Publishing, Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | use clap::{App, Arg}; 16 | use failure::{format_err, Error}; 17 | use gimli::{ 18 | DW_AT_byte_size, DW_AT_count, DW_AT_data_member_location, DW_AT_name, DW_AT_type, 19 | DW_TAG_array_type, DW_TAG_base_type, DW_TAG_class_type, DW_TAG_const_type, DW_TAG_pointer_type, 20 | DW_TAG_structure_type, DW_TAG_subrange_type, DebugAbbrev, DebugInfo, DebugStr, LittleEndian, 21 | }; 22 | use object::Object; 23 | 24 | use std::collections::HashMap; 25 | 26 | fn get_dwarf_string( 27 | debug_str: &DebugStr, 28 | attribute: gimli::Result>>, 29 | ) -> Option { 30 | if let Ok(Some(gimli::AttributeValue::DebugStrRef(offset))) = attribute { 31 | if let Ok(s) = debug_str.get_str(offset) { 32 | if let Ok(s) = s.to_string() { 33 | return Some(s.to_string()); 34 | } 35 | } 36 | } 37 | None 38 | } 39 | 40 | fn get_dwarf_usize( 41 | attribute: gimli::Result>>, 42 | ) -> Option { 43 | match attribute { 44 | Ok(Some(gimli::AttributeValue::Udata(data))) => Some(data as usize), 45 | Ok(Some(gimli::AttributeValue::Data1(array))) => Some(u8::from_le_bytes(array) as usize), 46 | Ok(Some(gimli::AttributeValue::Data2((array, _)))) => { 47 | Some(u16::from_le_bytes(array) as usize) 48 | } 49 | Ok(Some(gimli::AttributeValue::Data4((array, _)))) => { 50 | Some(u32::from_le_bytes(array) as usize) 51 | } 52 | Ok(Some(gimli::AttributeValue::Data8((array, _)))) => Some(usize::from_le_bytes(array)), 53 | _ => None, 54 | } 55 | } 56 | 57 | #[derive(Clone, Debug)] 58 | struct CppMember { 59 | name: String, 60 | type_name: String, 61 | type_size: usize, 62 | byte_offset: usize, 63 | array_count: Vec, 64 | } 65 | 66 | impl CppMember { 67 | fn total_size(&self) -> usize { 68 | let mut val = self.type_size; 69 | for x in &self.array_count { 70 | val *= x; 71 | } 72 | val 73 | } 74 | } 75 | impl std::fmt::Display for CppMember { 76 | fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { 77 | let mut array = String::new(); 78 | for x in &self.array_count { 79 | array += &format!("[{}]", x); 80 | } 81 | writeln!( 82 | f, 83 | " {} {}{}; // {} bytes", 84 | self.type_name, 85 | self.name, 86 | array, 87 | self.total_size() 88 | )?; 89 | Ok(()) 90 | } 91 | } 92 | 93 | #[derive(Clone, Debug)] 94 | struct CppStruct { 95 | name: String, 96 | size: usize, 97 | members: Vec, 98 | } 99 | 100 | impl std::fmt::Display for CppStruct { 101 | fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { 102 | writeln!(f, "struct {}", self.name)?; 103 | writeln!(f, "{{",)?; 104 | for m in &self.members { 105 | write!(f, "{}", m)?; 106 | } 107 | writeln!(f, "}};")?; 108 | Ok(()) 109 | } 110 | } 111 | 112 | fn struct_from_entry<'abbrev, 'unit, R, Offset>( 113 | debug_str: &gimli::DebugStr, 114 | e: &gimli::DebuggingInformationEntry<'abbrev, 'unit, R, Offset>, 115 | ) -> CppStruct 116 | where 117 | R: gimli::Reader + 'unit, 118 | Offset: gimli::ReaderOffset + 'unit, 119 | { 120 | CppStruct { 121 | name: get_dwarf_string(&debug_str, e.attr_value(DW_AT_name)).unwrap_or_default(), 122 | size: get_dwarf_usize(e.attr_value(DW_AT_byte_size)).unwrap_or_default(), 123 | members: Vec::new(), 124 | } 125 | } 126 | 127 | fn member_from_entry<'abbrev, 'unit, R, Offset>( 128 | compilation_unit: &'unit gimli::CompilationUnitHeader, 129 | abbrev: &'abbrev gimli::Abbreviations, 130 | debug_str: &gimli::DebugStr, 131 | mut e: gimli::DebuggingInformationEntry<'abbrev, 'unit, R, Offset>, 132 | ) -> CppMember 133 | where 134 | R: gimli::Reader + 'unit, 135 | Offset: gimli::ReaderOffset + 'unit, 136 | { 137 | let name = get_dwarf_string(&debug_str, e.attr_value(DW_AT_name)).unwrap_or_default(); 138 | let byte_offset = get_dwarf_usize(e.attr_value(DW_AT_data_member_location)).unwrap_or_default(); 139 | 140 | // We must chase nested typedefs until we get the actual size, 141 | // but we want the top-level name to be the one that we save. 142 | let mut type_name = String::new(); 143 | let mut type_size = 0; 144 | let mut is_pointer = false; 145 | let mut is_const = false; 146 | let mut array_count = Vec::new(); 147 | while let Ok(Some(gimli::AttributeValue::UnitRef(unit_offset))) = e.attr_value(DW_AT_type) { 148 | if let Ok(mut cursor) = compilation_unit.entries_at_offset(abbrev, unit_offset) { 149 | let _ = cursor.next_entry(); 150 | e = cursor.current().unwrap().clone(); 151 | if type_name.is_empty() { 152 | type_name = 153 | get_dwarf_string(&debug_str, e.attr_value(DW_AT_name)).unwrap_or_default(); 154 | } 155 | #[allow(non_upper_case_globals)] 156 | match e.tag() { 157 | DW_TAG_pointer_type => is_pointer = true, 158 | DW_TAG_const_type => is_const = true, 159 | DW_TAG_array_type => { 160 | let mut array_cursor = cursor.clone(); 161 | while let Ok(Some((delta_depth, array_child))) = array_cursor.next_dfs() { 162 | if delta_depth < 0 { 163 | break; 164 | } 165 | if array_child.tag() == DW_TAG_subrange_type { 166 | array_count.push( 167 | get_dwarf_usize(array_child.attr_value(DW_AT_count)) 168 | .unwrap_or_default(), 169 | ); 170 | } 171 | } 172 | } 173 | DW_TAG_structure_type | DW_TAG_base_type | DW_TAG_class_type => { 174 | type_size = get_dwarf_usize(e.attr_value(DW_AT_byte_size)).unwrap_or_default(); 175 | } 176 | _ => {} 177 | } 178 | } else { 179 | break; 180 | } 181 | } 182 | if is_pointer { 183 | if type_name.is_empty() { 184 | type_name = "void".to_string(); 185 | } 186 | type_name += "*"; 187 | type_size = 8; 188 | } 189 | if is_const { 190 | type_name = format!("const {}", type_name); 191 | } 192 | CppMember { 193 | name, 194 | type_name, 195 | type_size, 196 | byte_offset, 197 | array_count, 198 | } 199 | } 200 | 201 | fn add_padding_blocks(s: &mut CppStruct) -> bool { 202 | let mut expected_offset = 0; 203 | let mut last_offset = 0; 204 | let mut new_members = Vec::with_capacity(s.members.len() * 2); 205 | for m in &s.members { 206 | let type_size = m.total_size(); 207 | if type_size == 0 { 208 | return false; 209 | } 210 | 211 | if expected_offset == m.byte_offset { 212 | expected_offset += type_size; 213 | } else if m.byte_offset > expected_offset { 214 | new_members.push(CppMember { 215 | name: String::new(), 216 | type_name: "".to_string(), 217 | type_size: m.byte_offset - expected_offset, 218 | byte_offset: expected_offset, 219 | array_count: Vec::new(), 220 | }); 221 | expected_offset = m.byte_offset + type_size; 222 | } else if m.byte_offset == last_offset { 223 | // This is a union... 224 | expected_offset = std::cmp::max(expected_offset, m.byte_offset + type_size); 225 | } 226 | new_members.push(m.clone()); 227 | last_offset = m.byte_offset; 228 | } 229 | if expected_offset < s.size { 230 | new_members.push(CppMember { 231 | name: String::new(), 232 | type_name: "".to_string(), 233 | type_size: s.size - expected_offset, 234 | byte_offset: expected_offset, 235 | array_count: Vec::new(), 236 | }); 237 | } 238 | s.members = new_members; 239 | true 240 | } 241 | 242 | fn align_up(amount: usize, align: usize) -> usize { 243 | let remainder = align - amount % align; 244 | amount + remainder 245 | } 246 | 247 | #[cfg(test)] 248 | mod tests { 249 | use super::*; 250 | #[test] 251 | fn align() { 252 | assert_eq!(align_up(42, 4), 44); 253 | } 254 | } 255 | 256 | fn count_padding(s: &CppStruct) -> (usize, usize) { 257 | let mut max_base_size = 1; 258 | let mut padding = 0; 259 | let mut only_last_member = false; 260 | for (i, m) in s.members.iter().enumerate() { 261 | if m.type_name == "" { 262 | if i == s.members.len() - 1 && padding == 0 { 263 | only_last_member = true; 264 | } 265 | padding += m.type_size; 266 | } else { 267 | let base_size = if m.type_size > 8 { 268 | if m.type_size % 4 == 0 { 269 | 4 270 | } else { 271 | 8 272 | } 273 | } else { 274 | m.type_size 275 | }; 276 | max_base_size = std::cmp::max(max_base_size, base_size); 277 | } 278 | } 279 | if only_last_member { 280 | // If only the last member is padded, we can't improve on this regardless 281 | // of our guess. 282 | (padding, s.size) 283 | } else { 284 | let optimal_size = align_up(s.size - padding, max_base_size); 285 | (padding, optimal_size) 286 | } 287 | } 288 | 289 | fn main() -> Result<(), Error> { 290 | let matches = App::new("structpack") 291 | .version("1.0") 292 | .about("Return structure packing info from an elf file.") 293 | .arg( 294 | Arg::with_name("file") 295 | .help("The name of a local file to be inspected") 296 | .short("f") 297 | .long("file") 298 | .value_name("FILENAME") 299 | .takes_value(true) 300 | .required(false), 301 | ) 302 | .get_matches(); 303 | 304 | let filename = matches.value_of("file").unwrap(); 305 | println!("Investigating {}...", filename); 306 | 307 | let data = std::fs::read(filename)?; 308 | let obj = object::File::parse(&data).map_err(|e| format_err!("{}", e))?; 309 | let debug_info_data = obj 310 | .section_data_by_name(".debug_info") 311 | .ok_or_else(|| format_err!("Could not read .debug_info section data from elf file"))?; 312 | let debug_info = DebugInfo::new(&debug_info_data, LittleEndian); 313 | 314 | let debug_abbrev_data = obj 315 | .section_data_by_name(".debug_abbrev") 316 | .ok_or_else(|| format_err!("Could not read .debug_abbrev section data from elf file"))?; 317 | let debug_abbrev = DebugAbbrev::new(&debug_abbrev_data, LittleEndian); 318 | 319 | let debug_str_data = obj 320 | .section_data_by_name(".debug_str") 321 | .ok_or_else(|| format_err!("Could not read .debug_str section data from elf file"))?; 322 | let debug_str = DebugStr::new(&debug_str_data, LittleEndian); 323 | 324 | let mut structs: Vec = Vec::new(); 325 | 326 | let mut structs_seen: HashMap = HashMap::new(); 327 | 328 | let mut iter = debug_info.units(); 329 | while let Ok(Some(unit)) = iter.next() { 330 | if let Ok(abbrev) = unit.abbreviations(&debug_abbrev) { 331 | let mut cursor = unit.entries(&abbrev); 332 | let _ = cursor.next_dfs(); 333 | while let Some((_, entry)) = cursor.next_dfs()? { 334 | if (entry.tag() == DW_TAG_structure_type || entry.tag() == DW_TAG_class_type) 335 | && entry.has_children() 336 | { 337 | let mut cpp_struct = struct_from_entry(&debug_str, &entry); 338 | if cpp_struct.name.is_empty() || cpp_struct.size == 0 { 339 | continue; 340 | } 341 | if structs_seen.get(&cpp_struct.name).is_some() { 342 | continue; 343 | } 344 | if let Some((_, first_member_entry)) = cursor.next_dfs()? { 345 | cpp_struct.members.push(member_from_entry( 346 | &unit, 347 | &abbrev, 348 | &debug_str, 349 | first_member_entry.clone(), 350 | )); 351 | while let Some(member_entry) = cursor.next_sibling()? { 352 | cpp_struct.members.push(member_from_entry( 353 | &unit, 354 | &abbrev, 355 | &debug_str, 356 | member_entry.clone(), 357 | )); 358 | } 359 | } 360 | if add_padding_blocks(&mut cpp_struct) { 361 | let (padding, optimal_size) = count_padding(&cpp_struct); 362 | if padding != 0 && cpp_struct.size != optimal_size { 363 | let optimal_padding = optimal_size - (cpp_struct.size - padding); 364 | println!( 365 | "{} - {} bytes, {} padding (optimal size should be {} bytes, {} padding)", 366 | cpp_struct.name, cpp_struct.size, padding, optimal_size, optimal_padding 367 | ); 368 | println!("{}", cpp_struct); 369 | } 370 | structs_seen.insert(cpp_struct.name.clone(), (padding, cpp_struct.size)); 371 | structs.push(cpp_struct); 372 | } 373 | } 374 | } 375 | } 376 | } 377 | 378 | println!("found {} structs", structs.len()); 379 | 380 | Ok(()) 381 | } 382 | --------------------------------------------------------------------------------