11cb0ef41Sopenharmony_ci# postject
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci[![CircleCI](https://dl.circleci.com/status-badge/img/gh/postmanlabs/postject/tree/main.svg?style=svg)](https://dl.circleci.com/status-badge/redirect/gh/postmanlabs/postject/tree/main)
41cb0ef41Sopenharmony_ci[![npm version](http://img.shields.io/npm/v/postject.svg)](https://npmjs.org/package/postject)
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciEasily inject arbitrary read-only resources into executable formats
71cb0ef41Sopenharmony_ci(Mach-O, PE, ELF) and use it at runtime.
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci## Install
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci```sh
121cb0ef41Sopenharmony_cinpm i -g postject
131cb0ef41Sopenharmony_ci```
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci## Usage
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci### Command line utility
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci```sh
201cb0ef41Sopenharmony_ci$ postject -h
211cb0ef41Sopenharmony_ciUsage: postject [options] <filename> <resource_name> <resource>
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ciInject arbitrary read-only resources into an executable for use at runtime
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ciArguments:
261cb0ef41Sopenharmony_ci  filename                             The executable to inject into
271cb0ef41Sopenharmony_ci  resource_name                        The resource name to use (section name on Mach-O and ELF, resource name for PE)
281cb0ef41Sopenharmony_ci  resource                             The resource to inject
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ciOptions:
311cb0ef41Sopenharmony_ci  --macho-segment-name <segment_name>  Name for the Mach-O segment (default: "__POSTJECT")
321cb0ef41Sopenharmony_ci  --output-api-header                  Output the API header to stdout
331cb0ef41Sopenharmony_ci  --overwrite                          Overwrite the resource if it already exists
341cb0ef41Sopenharmony_ci  -h, --help                           display help for command
351cb0ef41Sopenharmony_ci```
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci### Using Programatically
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci```js
401cb0ef41Sopenharmony_ciconst { inject } = require('postject');
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ciawait inject('a.out', 'lol', Buffer.from('Hello, world!'));
431cb0ef41Sopenharmony_ci```
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci## Building
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci### Prerequisites
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci* CMake
501cb0ef41Sopenharmony_ci* Ninja
511cb0ef41Sopenharmony_ci* [Emscripten (emsdk)](https://emscripten.org/docs/getting_started/downloads.html)
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci### Build Command
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci```sh
561cb0ef41Sopenharmony_ci$ npm run build
571cb0ef41Sopenharmony_ci```
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ciThe final output is placed in `dist/`, with `main.js` being the
601cb0ef41Sopenharmony_cientrypoint.
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci### Testing
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci```sh
651cb0ef41Sopenharmony_ci$ npm test
661cb0ef41Sopenharmony_ci```
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci## Design
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ciTo ensure maximum capatibility and head off unforeseen issues, the
711cb0ef41Sopenharmony_ciimplementation for each format tries to use that format's standard
721cb0ef41Sopenharmony_cipractices for embedding binary data. As such, it should be possible
731cb0ef41Sopenharmony_cito embed the binary data at build-time as well. The CLI provides the
741cb0ef41Sopenharmony_ciability to inject the resources into pre-built executables, with the
751cb0ef41Sopenharmony_cigoal that the end result should be as close as possible to what is
761cb0ef41Sopenharmony_ciobtained by embedding them at build-time.
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ciNote: Other runtime injection implementers should search the binary
791cb0ef41Sopenharmony_cicompiled with `postject-api.h` for the
801cb0ef41Sopenharmony_ci`POSTJECT_SENTINEL_fce680ab2cc467b6e072b8b5df1996b2:0` fuse and
811cb0ef41Sopenharmony_ciflip the last character to `1` to indicate that a resource has been
821cb0ef41Sopenharmony_ciinjected. A different fuse can also be used by defining the
831cb0ef41Sopenharmony_ci`POSTJECT_SENTINEL_FUSE` macro before including `postject-api.h` and
841cb0ef41Sopenharmony_cipassing the same string to postject with
851cb0ef41Sopenharmony_ci`--sentinel-fuse <sentinel_fuse>`.
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ci### Windows
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_ciFor PE executables, the resources are added into the `.rsrc` section,
901cb0ef41Sopenharmony_ciwith the `RT_RCDATA` (raw data) type.
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ciThe build-time equivalent is adding the binary data as a resource in
931cb0ef41Sopenharmony_cithe usual manner, such as the Resource Compiler, and marking it as
941cb0ef41Sopenharmony_ci`RT_RCDATA`.
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ciThe run-time lookup uses the `FindResource` and `LoadResource` APIs.
971cb0ef41Sopenharmony_ci
981cb0ef41Sopenharmony_ci### macOS
991cb0ef41Sopenharmony_ci
1001cb0ef41Sopenharmony_ciFor Mach-O executables, the resources are added as sections inside a
1011cb0ef41Sopenharmony_cinew segment.
1021cb0ef41Sopenharmony_ci
1031cb0ef41Sopenharmony_ciThe build-time equivalent of embedding binary data with this approach
1041cb0ef41Sopenharmony_ciuses a linker flag: `-sectcreate,__FOO,__foo,content.txt`
1051cb0ef41Sopenharmony_ci
1061cb0ef41Sopenharmony_ciThe run-time lookup uses APIs from `<mach-o/getsect.h>`.
1071cb0ef41Sopenharmony_ci
1081cb0ef41Sopenharmony_ci### Linux
1091cb0ef41Sopenharmony_ci
1101cb0ef41Sopenharmony_ciFor ELF executables, the resources are added as notes.
1111cb0ef41Sopenharmony_ci
1121cb0ef41Sopenharmony_ciThe build-time equivalent is to use a linker script.
113