Can I replace the IO driver for printf at runtime using gcc? - glibc

I'm using gcc 4.8.4 on an ARM from TI. I want to route printf() and fgets() to my UART driver. I'm looking for a functional interface to install a file IO driver. But the GCC docs say "Don’t try to create your own objects of type FILE; let the library do it." I can't imagine I need to rebuild glibc to change this? Anyone know an example of creating my own FILE structure?
I can see a definition used for FILE (__sFILE in reent.h). Seems dangerous just to create one and use it. But maybe?

I want to route printf() and fgets() to my UART driver.
Are you perhaps looking for
freopen("/dev/uart", "r", stdin);
freopen("/dev/uart", "w", stdout);
Documentation.

Related

Rust library for inspecting .rlib binaries

I'm looking for a way to load and inspect .rlib binaries generated by rustc. I've hunted around the standard library without much luck. My assumption is that an .rlib contains all the type information necessary to statically type check programs that "extern crate" it. rustc::metadata is where my hunt ended. I can't quite figure out if the structures available at this point in the compiler are intended as entry points for users, or if they are solely intermediate abstractions depending on a chain of previously initialized data.
Alternatively, If there's a way to dump an .rlib to stdout in a parsable form then that's also fantastic. I tried /usr/bin/nm, but it seemed to be excluding function type signatures. Maybe I'm missing something.
Anyways, I'm working on an editor utility for emacs that I hope at some point will provide contextually relevant information such as available methods, module items and their types, etc. I'd really appreciate any hints anyone has.
The .rlib file is an ar archive file. You can use readelf to read its content.
Try readelf -s <your_lib>.rlib. The type name may be mingled/decorated by the compiler so it may not be exactly the same as in .rs file.

Linux kernel which asm headers / symbols / macros are available on all architectures?

I want to use something (header file, struct, function or macro) that is declared / defined under arch/XXX/asm/include (in my case, PAGE_TABLE) in a kernel module.
Is it possible to know if that thing is present on all architectures?
Phrased differently: what exactly is the arch-portable API that the kernel exposes to kernel space under asm/?
I could find . or grep -r into the kernel tree, but is there a better way to know that for every new architecture that comes out, that thing must be defined for the architecture to be supported? After all, even if something is furnished on all existing architectures, who guarantees that it is not just a coincidence that they all furnish those things, but that they are not mandatory?
Taking headers for example, in recent source snapshots, x86 contains acpi.h, but arm does not, but all architectures seem to have page.h. So how can I know that I can use #include <asm/page.h> but not acpi.h? page.h on the other hand, is expected to have an implementation on all archs since include/linux/ uses it in several points, and include/linux is meant to be portable to all architectures (please confirm this point).
You can check it by yourself:
Algorithm:
Init array of all the possible ARCH's e.g. i386
Init /tmp/include/name_of_arch directory for each arch
Iterate for each ARCH: make headers_install ARCH=name_of_arch INSTALL_HDR_PATH=/tmp/include/name_or_arch/
For each file in each arch folder: compute sha256sum
Find the intersection of all the commons sha256 signatures.
You can find some the ARCH's in checkstack.pl script, e.g. m68k or in the main Makefile of the kernel.

Looking for C source code for snprintf()

I need to port snprintf() to another platform that does not fully support GLibC.
I am looking for the underlying declaration in the Glibc 2.14 source code. I follow many function calls, but get stuck on vfprintf(). It then seems to call _IO_vfprintf(), but I cannot find the definition. Probably a macro is obfuscating things.
I need to see the real C code that scans the format string and calculates the number of bytes it would write if input buffer was large enough.
I also tried looking in newlib 1.19.0, but I got stuck on _svfprintf_r(). I cannot find the definition anywhere.
Can someone point me to either definition or another one for snprintf()?
I've spent quite a while digging the sources to find _svfprintf_r() (and friends) definitions in the Newlib. Since OP asked about it, I'll post my finding for the poor souls who need those as well. The following holds true for Newlib 1.20.0, but I guess it is more or less the same across different versions.
The actual sources are located in the vfprintf.c file. There is a macro _VFPRINTF_R set to one of _svfiprintf_r, _vfiprintf_r, _svfprintf_r, or _vfprintf_r (depending on the build options), and then the actual implementation function is defined accordingly:
int
_DEFUN(_VFPRINTF_R, (data, fp, fmt0, ap),
struct _reent *data _AND
FILE * fp _AND
_CONST char *fmt0 _AND
va_list ap)
{
...
http://www.ijs.si/software/snprintf/ has what they claim is a portable implementation of snprintf, including vsnprintf.c, asnprintf, vasnprintf, asprintf, vasprintf. Perhaps it can help.
The source code of the GNU C library (glibc) is hosted on sourceware.org.
Here is a link to the implementation of vfprintf(), which is called by snprintf():
https://sourceware.org/git/?p=glibc.git;a=blob;f=stdio-common/vfprintf.c

Where is the definition of function nanf() on linux

I am trying to look for definition and declaration of the function nanf() - return 'Not a Number function, which is related to the floating point functionality on Linux gcc compiler environment - (glibc).
I need to use similar/same definition for nanf() on windows to build my code using Visual Studio.
I checked following header files in the Linux src/include folders but did not see anything related to nanf declaration.
/usr/include/math.h
/usr/include/bits/nan.h
Any pointers will be helpful.
thank you,
-AD
The declaration is just (C99 §7.12.11.3):
float nanf(const char *tagp);
or macros that expand to something equivalent. A conformant implementation is highly platform-specific, however, because the standard does not define how to interpret tagp, except to say that the behavior is equivalent to a certain call to strtof, and "The nan functions return a quiet NaN, if available, with content indicated through tagp."
Instead of trying to shoehorn C99 features into the one compiler and library that stubbornly refuses to even try to implement them, why not just use a real C compiler? There are plenty out there.

WinAPI functions in new .exe

I've been looking recently into creating a new native language. I understand the (very) basics of the PE format and I've grabbed an assembler with a fairly kind interface off the webs, which I've successfully used to implement some simple functions. But I've run into a problem using functions from a library. The only way that I've called library functions from a dynamically compiled function previously is to pass in the function pointer manually- something I can't do if I create PE files and execute them in their own process. Now, I'm not planning on using the CRT, but I will need access to the Win API to implement my own standard libraries. How do I generate a reference to a WinAPI function so that the PE loader will patch it up?
You need to write an import table. It's basically a list of function names that you wish to use in your application. It's pointed to by the PE header. The loader loads the DLL files into the process memory space for you, finds the requested function in their export table and leaves the address for it in the import table. You then usually dereference that and jmp there.
Check out Izelion's assembly tutorial for the full details and for asm examples.
How about starting by emitting C instead of assembly? Then writing directly to ASM is just an optimization.
I'm not being facetious: most compilers turn out some kind of intermediate code before the final native code pass.
I realize you're trying to get away from all the null-delmited rigmarole, but you'll need that for the WinAPI functions anyway.
Re-reading your question: you do realize that you can get the WinAPI function addresses by calling LoadLibrary(), then calling GetProcAddress(), and then setting up the call...right?
If you want to see how to bootstrap this from pure assembly: the old SDKs had ASM sample code, probably the new ones still do. If they don't, the DDK will.

Resources