Packing a file into an ELF executable - binary

I'm currently looking for a way to add data to an already compiled ELF executable, i.e. embedding a file into the executable without recompiling it.
I could easily do that by using cat myexe mydata > myexe_with_mydata, but I couldn't access the data from the executable because I don't know the size of the original executable.
Does anyone have an idea of how I could implement this ? I thought of adding a section to the executable or using a special marker (0xBADBEEFC0FFEE for example) to detect the beginning of the data in the executable, but I do not know if there is a more beautiful way to do it.
Thanks in advance.

You could add the file to the elf file as a special section with objcopy(1):
objcopy --add-section sname=file oldelf newelf
will add the file to oldelf and write the results to newelf (oldelf won't be modified)
You can then use libbfd to read the elf file and extract the section by name, or just roll your own code that reads the section table and finds you section. Make sure to use a section name that doesn't collide with anything the system is expecting -- as long as your name doesn't start with a ., you should be fine.

I've created a small library called elfdataembed which provides a simple interface for extracting/referencing sections embedded using objcopy. This allows you to pass the offset/size to another tool, or reference it directly from the runtime using file descriptors. Hopefully this will help someone in the future.
It's worth mentioning this approach is more efficient than compiling to a symbol, as it allows external tools to reference the data without needing to be extracted, and it also doesn't require the entire binary to be loaded into memory in order to extract/reference it.

Related

How can I get a list of help context IDs used in a CHM help file?

I have a C++ program that calls AfxMessageBox, passing in a help file context ID. When I click Help on the resulting message box, I get a litte error box that says "Failed to launch help." I need to verify that I'm using a valid help ID. Is there a way to examine a CHM file to find out what context IDs are valid for it? Either a commercial tool or a way to write a C# or C++ or even Python program to do it.
Edit: This is an old CHM file, and I do not have the files that were used to create it. I used 7-zip to extract its contents into a folder, but I see nothing there that tells me what context IDs the file has. When the error occurs, the C++ code assigns a value of 135 to the error, and then it adds 0x30000 (196,608) to that for no reason I know. Then, when the message box gets generated, 0x30000 is subtracted and the result, 135, is passed in to AfxMessageBox() as the help ID. I've tried both 135 and 196743 (0x30000 + 135) in the AfxMessageBox() call but neither has worked. The files extracted from the help file by 7-Zip include a set of .htm file with numbers for names, but the numbers have no relation to the context IDs that I can find.
There is another execution path in my code that uses the same help file. As near as I can tell, it uses low-level functions to create a dialog box that resembles the output of AfxMessageBox(). When I force that code path use context ID 196743, I get the expected help page. Unfortunately, it's not easy to get from the code that doesn't work to the code that does.
Edit:
Your additional Edit in the question take me back many years. Not related to your original question but some links to read:
Starting with HTML Help
Adding HTML Help to Existing Dialog-Based Application
Extracting help contextID by code you must have deep knowledge of the CHM internals.
For a single value e.g. 10000 you may want to test the contextID using PowerShell or a DOS prompt. This opens the requested .chm in the Help viewer, and uses a context ID to request a URL to display.
hh.exe -mapid 10000 ms-its:CHM-example.chm
Sometimes you only have the CHM output file and nobody can locate the source files. It’s not always possible to recover all files and data you need.
Decompiling loses the alias.h and map.h files and their information for F1-Help (context sensitive help).
You may know a CHM is something like a zipped web (HTML archive) with some additional system files of metadata. Context ID's are mostly integrated by compiling a alias.h and map.h file. The purpose of the two files is to ease the coordination between developer and help author. The mapping file links an ID to the map number - typically this can be easily created by the developer and passed to the help author. Then the help author creates an alias file linking the IDs to the topic names (See: Creating Context-Sensitive Help for Applications.
I'm using FAR HTML as a toolbox full of various authoring, file and HTML utilities (Disclaimer: It is freeware now!).
Please note there is a download link (for required Microsoft HTMLHelp Workshop) because the Microsoft download links are broken.
The CHM can be opened using FAR HTML and by copy and paste you have all ID and topic information (ALIAS section). In Help File Explorer you need to open Internal Files > #IVB.

Fortran90 - compiled program creates a blank csv file instead of reading the existing one

In short: I am trying to load a csv file but the program always overwrites the existing file as an empty new file.
Longer: I am pretty new to Fortran, so bear with me. I am trying to read data from a csv file into a fortran program. Now I didn't write the program and it is pretty big, so I can't post the whole thing here. The program consists of a whole bunch of .f90 files and everything is compiled using a makefile. Now since I am loading the gcc module before compiling, I am assuming that it is compiled using GNU Fortran, because it is part of gcc. (idk how to find out if that is correct)
The compiler returns an executable in a different directory. When I execute the program in that directory it apparently overwrites the existing .csv file with a new blank one, so the program only reads "End of File". I don't know why it always creates a new file, how do I stop it from doing so?
As a side note, the csv file I am trying to read simply consists of a single column of floats, e.g.
"0.01, 0.13, 0.041,..." etc.
The code that I inserted into a subroutine of one of the .f90 files is the following:
real*8, dimension(nz) :: Nsq
integer :: i
open(10, file='Nsq.csv')
do i=1,20
read(10, *) Nsq(i)
enddo
close(10)
I have also tried to write a small test program, essentially running the same code as above. That one works just fine and outputs the contents of the csv file without any issues. For that one I use gfortran to compile it.
I have no experience in Fortran at all, so I am completely stumped, why this happens. I know the chances are slim that you guys can help me with this, since I can't provide the whole source code. But maybe someone has an idea why this occurs. Maybe you know an alternate way of reading csv files?
Thanks for your time.
The open-statement in Fortran OPEN(connect-spec-list), has a lot of connection specifications which define how an external file should be managed (see. Fortran 2018 Standard sec 12.5.6).
When you open a file using the simplest form of the open-statement:
OPEN(unit=unitid,file="filename")
A lot of default assumptions are made such as: ACCESS="SEQUENTIAL", ASYNCHRONOUS="NO", BLANK="NULL", .... The most important ones, however, are ACTION and STATUS which define the purpose of the file. The action specification states if you want to use the file for reading, writing or both, while the status essentially defines if we work on an existing file or not, and what we should do with it (replace it, keep it, ...)
Both these specifications have a default compiler dependent state.
In the Intel compiler suit, the default is action="readwrite", status="unknown" (see here and here)
Intel defines the status="unknown" as :Indicates the file may or may not exist. If the file does not exist, a new file is created and its status changes to 'OLD'.
The Gnu compiler suit has a different take on this. The default action is defined by a set of rules which depend on its accessibility if the file exists (+rw,+r-w,-r+w) (see here). The behaviour for the default action="unknown" is not documented but seems to be REWRITE (see Default Status of "Unknown" in Open)
It is advised to use a proper method if you know what you want to do with the file:
OPEN(newunit=unitid, file="filename", action="read", status="old")

Erlang: How to include libraries

I'm writing a simple Erlang program that requests an URL and parses the response as JSON.
To do that, I need to use a Library called Jiffy. I downloaded and compiled it, and now i have a .beam file along with a .app file. My question is: How do I use it? How do I include this library in my program?. I cannot understand why I can't find an answer on the web for something that must be very crucial.
Erlang has an include syntax, but receives a .hrl file.
Thanks!
You don't need to include the file in your project. In Erlang, it is at run time that the code will try to find any function. So the module you are using must be in the search path of the VM which run your code at the point you need it, that's all.
For this you can add files to your path when you start erlang: erl -pa your/path/to/beam (it exists also -pz see erlang doc)
Note that it is also possible to modify the path from the application itself using code:add_path(Dir).
You should have a look to the OTP way to build applications in erlang documentation or Learn You Some Erlang, and also look at Rebar a tool that helps you to manage erlang application (for example starting with rebar or rebar wiki)
To add to Pascal's answer, yes Erlang will search for your files at runtime and you can add extra paths as command line arguments.
However, when you build a project of a scale that you are including other libraries, you should be building an Erlang application. This normally entails using rebar.
When using rebar, your app should have a deps/ directory. To include jiffy in your project, it is easiest to simply clone the repo into deps/jiffy. That is all that needs to be done for you to do something like jiffy:decode(Data) in your project.
Additionally, you can specify additional include files in your rebar.config file by adding extra lines {erl_opts, [{i, "./Some/path/to/file"}]}.. rebar will then look for file.so using that path.

using a function which is defined in a scheme file, inside another scheme file

I am new to Scheme.
I wrote a program that defines a function named "run", and I stored it as "Run.scm".
Then I have a "test.scm" file which uses this "run" function which I defined it inside "Run.scm".
I don't know how to include the "Run.scm" inside "test.scm" that I can use "run" function inside test file. Can anyone help me?
Compatible method
If you have a file with source code you can in any Scheme comforming program use load. So in your test you can do this:
% ls
test.scm Run.scm
Contents of test.scm
(load "Run.scm")
(run)
The new and better way (R6RS and later)
If you have a R6RS or a R7RS you have the ability to make a library. It is implementation specific how the library is incorporated into it but not how the source file looks. Read you documentation to how you add the library to your system.
Then, imagine you have made an awesome/utility.scm library. In R6rs/R7RS you would add it to your code like this:
(import (awesome utility))
;; start using the imported code..
(awesome-function '(1 2 3 4)) ; ==> (4 3 2 1)
Alternative by R5RS Schemes
Since R5RS and earlier just had load most implementations made their own way of loading both libraries and source files. eg. Racket has require and Chicken Scheme has import. To use these will lock you in with one supplier, but many libraries do it by building a implementation specific start file that import the other files in the special way to make out the differences between them or make a source file based on parts with gnu make or similar program.

read input from html forms, execute local program,output results

I have a question that will help me understand how stuff works and investigate feasibility of a bigger plan I have in mind.
Simply given - lets assume that all things run locally - I am wondering if it is possible to:
create an html page with a form that will prompt the user to enter
the local path of an input file inputFile.dat
this input file will be fed to a c++ exe program that expects it for input
the c++ exe file will run (this exe file depends on libraries etc but lets assume all are local here)
... and will output the result on screen
It sounds simple, but is it?
Many thanks folks!
Yes, this is definitely possible, if you want to use PHP or some other scripting language.
Create a form on your web page
Add the directory of the program
Add the directory of the data
When submitted, use the PHP (or other language) exec function (docs) to execute the program, with the supplied data as argument
The exec function returns output from the program.
Display the output as wished on your page.
Good luck!