MCPcopy Create free account
hub / github.com/Marus/cortex-debug / validateELFHeader

Function validateELFHeader

src/common.ts:734–767  ·  view source on GitHub ↗
(exe: string, cb?: (str: string, fatal: boolean) => void)

Source from the content-addressed store, hash-verified

732// Returns true if the ELF header match the elf magic number, false in all other cases
733//
734export function validateELFHeader(exe: string, cb?: (str: string, fatal: boolean) => void): boolean {
735 try {
736 if (!fs.existsSync(exe)) {
737 if (cb) {
738 cb(`File not found "executable": "${exe}"`, true);
739 }
740 return false;
741 }
742 const buffer = Buffer.alloc(16);
743 const fd = fs.openSync(exe, 'r');
744 const n = fs.readSync(fd, buffer, 0, 16, 0);
745 fs.closeSync(fd);
746 if (n !== 16) {
747 if (cb) {
748 cb(`Could not read 16 bytes from "executable": "${exe}"`, true);
749 }
750 return false;
751 }
752 // First four chars are 0x7f, 'E', 'L', 'F'
753 if ((buffer[0] !== 0x7f) || (buffer[1] !== 0x45) || (buffer[2] !== 0x4c) || (buffer[3] !== 0x46)) {
754 if (cb) {
755 cb(`Not a valid ELF file "executable": "${exe}". Many debug functions can fail or not work properly`, false);
756 }
757 return false;
758 }
759 return true;
760 }
761 catch (e) {
762 if (cb) {
763 cb(`Could not read file "executable": "${exe}" ${e ? e.toString() : ''}`, true);
764 }
765 return false;
766 }
767}
768
769//
770// You have two choices.

Callers 2

loadFromObjdumpAndNmMethod · 0.90

Calls 1

toStringMethod · 0.80

Tested by

no test coverage detected