Building firmware Patch for embedded applications - c

I have a library stack that is not going to change, and an firmware that is going to use only this stack. Firmware will change alot along the way. I don't want to every time release the whole image(including library stack) because of limited memory and resources issue(This is an embedded application not a desktop or server).
I just want to release the application image and that automatically be able to use the library image. I am not sure how to do it. I know in Windows for example this is handled by dll's. But this is an embedded application and has no OS. Binary images loads to memory and processor is going to execute it.
Any experience/suggestions?
Toolchain: IAR 8051

This depends quite a bit on your tool-chain. Here's a possible high-view approach.
Compile your library into an executable image, setting your linker to use a particular portion of your flash memory space. You'll probably need a fake/stub entry function for the linker to be happy.
Once that is done, find all of the addresses of the symbols used by the library and instruct your linker as to those symbol locations when building your normal program, and do not instruct the link process to use the intermediary library objects when linking. Also instruct the linker to place the code into the section of flash that is update-able.
What you will then have is an image for the library, and the ability to build new versions of the main program image using at library.
This could probably be scripted if your linker output format is an unstripped elf (prior to converting to a binary for burning on the flash), and if your linker can accept a plain text file for instructions (both are true if you are using the gnu toolchains). I'd recommend scripting it for your sanity unless the library has very few externally visible functions and variables in it.
I do have to agree with some of the commentors; unless transferring the library is very hard, you should just build a single simple image that includes the library and push the whole thing. You might say the library will never change now, but inevitably something will come up that requires a change to the library code, and if you change the library and cannot keep the symbols in exactly the same spot, all of your application images will not be able to work with the new library. This is a recipe for a nightmare when dealing with compatible software (firmware) updates.

Related

How to manage features that depend on libraries that may or may not be installed?

I'm writing some new functionality for a graphics program (written mostly in C, with small parts in C++). The new functionality will make use of libgmic. Some users of the program will have libgmic installed, quite a lot will not. The program is monolithic: I'm not writing a plugin, this will be part of the main program. Compiling the program with the right headers is easy, but I need to be able to check at runtime whether the library is installed on the user's system or not in order to enable / disable the particular menu item so that users without the library installed can't invoke this piece of functionality and crash the program. What's the best way of going about this?
You need to load the library at runtime with dlopen (or LoadLibrary on Windows) instead of linking to it, get function pointers with dlsym (GetProcAddress on Windows) and use them instead of function prototypes from the headers. Otherwise your program will simply fail to startup without the library (or crash, in some cases).
Some libraries support such usage well, such as providing types for all the functions you need. With others you’re on your own but that’s still possible.

Getting known library paths from ldconfig for use with dlopen

I have a program written in C that uses dlopen for loading plug-in modules. When the library is dynamically loaded, it runs constructor code which register pointer to structure with function implementations with the main application by use of exported function. I want to use absolute path for specifying the file to dlopen.
Then I have other part of the program with takes file, determine if it is ELF, then looks into the ELF header for specific ELF section, read this section and extract from it pertinent information. This way it filters only shared libraries which I have previously tagged as a plug-in module.
However, I am solving a problem how to discover them on the fly (in portable Linux way, i.e. it will run on Debian and on Fedora too and so on) from the main program. I have been thinking about using ldconfig for this. (As the modules will be installed by way of distro packaging system, APT for example.) Is there any way how to programmatically get the string list of known libraries from C program other than directly reading the /etc/ld.co.cache file? I was thinking that maybe there is some header library which will give char** when I ask.
Or, maybe is there any better solution to my problem?
(I am proponent of using standard system components that programming one-off solutions which will need support in the future.)

How to programmatically look up a symbol in a running application

I have an application which I'm trying to debug, however running it under gdb is producing different results, and it would be nice to have it output true symbol information when confronted with an address. for instance.
I have a method which is called periodically and I can determine the address of the call site. However, I'd like to print out the symbol information at run time for this address. I know I can run "nm" on the executable but that is outside of the application. I want to be able to do it from within the application itself.
I'm using GCC 4.7.2 on a linux platform.
(eddited to explain why I can't use gdb)
Dynamic symbol information can be accessed via the DT_DYNAMIC segment, which is loaded into memory and can be accessed by asking dlopen(3) for a handle to the main executable.
Static symbol information can be read only from the actual executable file, or an external file, as it is not listed in the loadable segments.
With just dynamic information, you will not be able to resolve anything that is not exported, which means you will most likely only see library calls unless your executable has its symbol table exported, so static information is probably the way to go.
This involves either lots of parsing, or using the bfd library built from binutils.
I'd seriously wonder if that was really worth the effort, though. You might get the same information from using the profiling support in gcc.

Creating ELF binaries without using libelf or other libraries

Recently I tried to write a simple compiler on the linux platform by myself.
When it comes to the backend of the compiler, I decided to generate ELF-formatted binaries without using a third-party library, such as libelf.
Instead I want to try to write machine code directly into the file coresponding to the ELF ABI just by using the write() function and controlling all details of the ELF file.
The advantage of this approach is that I can control everything for my compiler.
But I am hesitating. Is that way feasible, considering how detailed the ELF ABI is?
I hope for any suggestions and pointers to good available resources available.
How easy/feasible this is depends on what features you want to support. If you want to use dynamic linking, you have to deal with the symbol table, relocations, etc. And of course if you want to be able to link with existing libraries, even static ones, you'll have to support whatever they need. But if your goal is just to make standalone static ELF binaries, it's really very easy. All you need is a main ELF header (100% boilerplate) and 2 PT_LOAD program headers: one to load your program's code segment, the other to load its data segment. In theory they could be combined, but security-hardened kernels do not allow a given page to be both writable and executable, so it would be smart to separate them.
Some suggested reading:
http://www.linuxjournal.com/article/1059

Embedded systems header functions

I am new to embedded systems and want to learn more,
I am currently optimizing a software with regards on the footprint for an ARM embedded system, and are wondering, the header files that you include in your source files. Where are they put?
Right now I am just using a software (OVP) to simulate the ARM hardware platform but in real hardware, you have to put the header files somewhere right? Like in gcc have the standard library on the hd. Do we have to insert this library in the embedded machine as well? Space is limited! And is there any way to minimize the size of the library? Thanks!
Example
#include <stdio.h>
#include <stdlib.h>
I am using the cross compiler arm-elf-gcc
Best Regards
Mr Gigu
You appear to possess a few fundamental misunderstandings about compiled executable code. The following applies to embedded and desktop systems.
Header files are no more than sourcefiles like any other. The difference is that they are inserted into the compilation unit by the pre-processor rather than compiled directly. Also in most cases they contain declarative statements only, and do not generally contribute to the generated code in the sense of executable instructions or stored data.
At runtime none of your source code is required to exist on the target; it is the work of the compiler to generate native executable machine code from your source. It is this machine code that is stored and runs on the target.
A header file is not the same thing as a library. It is merely (generally) the declaration of library content (function prototypes and other symbol declarations such as constants, data, macros, enumerations). The library takes the form of pre-compiled/assembled object code stored in a combined archive. It is the job of the linker to combine the required library code with the object code generated from compilation of your own source. It is this linked executable that is stored and executed on the target, not the original source code.
An exception regarding header files containing declarative code only is when they contain in-line code or executable code in a macro. However such code only occupies space in your application if explicitly called by the application.
When library code is linked, only those library object code components required to resolve references in the application code are linked, not the entire library (unless the entire library is composed of only a single object file).
The library does indeed have to get included in the image that is burned into the embedded system's memory. Usually you tell the linker to strip out unused functions, which goes a long way towards conserving memory. But this memory is the memory your program takes up in flash or whatever you use for non-volatile code storage. It doesn't say anything about how much RAM your program takes at runtime. You can also tell your compiler to optimize for space, and also use different runtime libraries - the ones provided by the vendor are often not as fast or small as they could be.

Resources