Linking files with LD [closed] - c

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I read this tutorial: http://www.osdever.net/tutorials/view/writing-a-simple-c-kernel
I tried linking the files using the likerscript that the tutorial provides. But LD gives me an error saying that it cannot read the file put out by nasm. Does anyone know what I am doing wrong?

If you executed the tutorial precisely as shown, then the problem is most likely here:
nasm -f aout kernel_start.asm -o ks.o
This produces an object file in the thoroughly obsolete a.out format. You're probably working through the tutorial on either a Windows or a Linux host system; the linkers that come with these systems expect object files in PECOFF and ELF format, respectively. There is probably another thing you can put after the -f in the above command that will make nasm produce the correct format.
Alternatively, learn to write AT&T assembly language instead. Then you can make an object file out of your .asm file with gcc -c just like the C source code, and you will automatically get the right format. The AT&T equivalent of the trivial startup file you have in that tutorial would be
.text
.globl start
start:
call k_main
cli
hlt
Take note also that I removed the leading underscore from the call instruction's argument. That underscore is only appropriate if the C code is compiled to an a.out-format object file, which (we suspect) it isn't.

Related

display filename, line number and function in C without modifying the source code [duplicate]

This question already has answers here:
how to trace function call in C?
(10 answers)
Closed 8 years ago.
I am new in a company, working with C source code which almost lacks any kind of tracing mechanism.
I would like to know whether or not the application passes through a certain file and where (which function).
I could do this using breakpoints, but the concerned file contains a huge lot of functions.
Therefore I'm looking for some kind of tool, that I can attach to the application, and that gives an output of following kind:
-- Main.c (main_function())
---- submain.c (submain_function())
...
From that, I then could deduce where (which filename, which function) the application is passing.
Does anybody know whether or not such a tool exists?
Thanks
If you're on linux, gdb might come handy.
You can compile the code using -g or -g3 option with gcc, then run the binary using gdb ./<executable_name>, set a breakpoint on desired function in any of the source files and check the call.
While stepping through the application, it will show the filename and line number of the executing instruction.
Note: Please check this and this for a detailed understanding.
I assume you develop on Linux. Then you could also customize the GCC compiler, in particular using MELT (a lispy domain specific language to extend GCC), to have the compiler add some logging at many places. For that you'll need to insert a new GCC "optimization" pass doing such a job, and most importantly, you'll need to understand some details about GCC internal representations (Gimple, Tree-s, Basic blocks, ...)
However, that would probably require more than a week of work from your part. Unless your code base is really big (at least half a million
of lines) that might not worth the effort

How to determine the value of passing arguments of a function via the backtrace of a process? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a backtrace of a process. and I want determine the value of a argument of one function called in the call trace, I have the .o file and I disassemble it, So I have the assembly procedure of that particular function, How can I calculate the value of the function's passing argument through the backtrace and the assembly code? It's on the ARM platform, I'm not quite familar with the ARM call trace, and assembly code.
If the code is written in C, this information is not available solely from the executable image, for the simple reason that C does not mangle function name symbols in order to encode the function parameter types.
Brief experimentation with gdb shows that if C code is compiled with the debugging flag, -g, gcc does put sufficient information into the executable's debug records for gdb to be able to figure out the function parameter types, and display function arguments in the backtrace.
But, if the executable is not compiled with -g, all that's in the executable are the function names, and their addresses, and that's all that gdb can show, in a backtrace from a coredump.
So, if you're working with .o files containing C code, without any debug stuff in it, there's nothing that can show you what the function parameters are,

How to read mex.c file in Matlab code [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a Matlab code for measurement and when I try to run it this error pops up:
Undefined function `mexLoadMeasurements` for input arguments of type `char`.
Error in LoadMeasurements (line 56)
measurements = mexLoadMeasurements(attr.Name);
I also have the file measurements_io.mex.c in my directory, but I don't know how to make it readable for Matlab to run my code.
Thanks
You need to compile the c file into a mex library (in matlab).
Setup your Matlab's mex compiler:
>> mex -setup
Compile the c source code
>> mex -largeArrayDims -O measurements_io.mex.c -output mexLoadMeasurements
Read more about mex files here.

how to execute step by step in C language? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I want to know how to execute step by step for my c program. I'm not getting answer as expected. So please tell me how to see step by step execution. I'm new to linux
Compile the program with '-g' option
example as
Compile the program
gcc test.c -g
which will generate a.out pass it with gdb
gdb a.out
then set breakpoint to main
gdb) break main
Then run your program in gdb
gdb) run
then break point hits use 'n' or 'next' to step to different lines
gdb) n
Use 's' for stepping into function and 'p' printing var value
Example :
gdb) s <fun_name>
gdb) p x
More convenient than gdb (especially for a beginer) may be some of IDEs. I'd suggest qtcreator.
At least a makefile (for ready project) will be required. You can also create a new project in qtcreator and import your files.
There are many debugging tools in Linux. I prefer gdb.
Usage:
while compiling use -g flag with. Ex:
gcc -g *.c
to see step by step execution use gdb tool:
ex: gdb ./a.out
Then give start command to start main function.
Then press s and continuosly press enter it will execute step by step.
Note: if it's a library function please press n instead of s.
to quit from execution press q.
Please refer –-help on debugging time to know more info. There are many instructions you have to know like run, break, next, info breakpoints..........

How do I generate a .cpp file from source code? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm starting with C programming, and have written a program and am ready to compile. I've heard that mingw is a good choice, but the documentation for using it starts with a .cpp file and then turns that into a .exe. What I currently have is just the pure source (i.e. just text commands), how do I turn that into a .cpp? Thanks.
A cpp file is a C++ source file. Many tools (compilers, editors, etc.) can work with .c files or .cpp files.
A source file refers to the actual code you write. You know, stuff like
int main() {
printf("Hello, world!");
return 0;
}
That would be source code. 4 lines of it. So generally, you can't generate it. You have to write it. It sort of sounds like you wrote a .c file and are getting confused because you're using a tool whose example uses a .cpp file.
If the tool works with both C and C++, then use it with the .c file.
If it only works with C++... then you'll have to use something else.
If you have written code in a file like "xyz.txt" then simply rename it to "xyz.c". Also, you can take a file named like "xyz.c" create a copy of it and rename that copy to "xyz.cpp" and then modify "xyz.cpp" so that it can be compiled as C++ code.
In either case the "xyz.c" when compiled becomes "xyz.exe" and "xyz.cpp" will also become an "xyz.exe" load module, but not the same as the one generated from C code.
If you want load modules for a program written in C and the same program written in C++ then you will have to give them different names, like: "xyz.c" and "abd.cpp".
Hope this helps, and welcome to our world of programming!

Resources