Skip to content

Instantly share code, notes, and snippets.

@tewilove
Created November 5, 2015 13:55
Show Gist options
  • Save tewilove/eb04e0f1c760c019fe80 to your computer and use it in GitHub Desktop.
Save tewilove/eb04e0f1c760c019fe80 to your computer and use it in GitHub Desktop.
<?php
$target = isset($argv[1]) ? $argv[1] : "modem";
$out = shell_exec("readelf -l ${target}.mdt 2>/dev/null");
// printf($out);
$seg = array();
$lines = split("\n", $out);
for ($i = count($lines) - 1; $i >= 0; $i--) {
if (!strstr($lines[$i], "NULL") &&
!strstr($lines[$i], "LOAD") &&
!strstr($lines[$i], "DYNAMIC") &&
!strstr($lines[$i], "INTERP")) {
unset($lines[$i]);
continue;
}
$seg []= trim($lines[$i]);
}
$seg = array_reverse($seg);
unlink("${target}.out");
$fp = fopen("${target}.out", "wb");
for ($i = 0; $i < count($seg) - 1; $i++) {
list($_a, $offset, $_c, $_d, $size) = sscanf($seg[$i], "%s %x %x %x %x");
if ($size == 0)
continue;
$offset_fp = ftell($fp);
if ($offset > $offset_fp) {
$zero = "";
for ($n = 0; $n < $offset - $offset_fp; $n++)
$zero .= "\0";
fwrite($fp, $zero);
}
fseek($fp, $offset);
$src = sprintf("${target}.b%02d", $i);
printf("%s <= %s\n", "${target}.out", $src);
fwrite($fp, file_get_contents($src));
}
fclose($fp);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment