Pass a command line argument to a WInAPI function using assembly language - masm

I am learning assembly language using the MASM32 package.
I am stuck with a problem that I am unable to resolve.
I have a small program that accepts a single command line parameter and passes that to a a WInAPI function named OutputDebugStringA which accepts a string as its input parameter.
The problem is that I don't know how to convert the contents of the buffer to a string so that I can pass it to the OutputDebugStringA function
Thanks in advance for any tips
Mathew
Code
.486
.model flat,stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
include \masm32\include\masm32.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib
includelib \masm32\lib\masm32.lib
include \masm32\vkdebug\dbproc\debug.inc
includelib \masm32\vkdebug\dbproc\debug.lib
include \masm32\MasmBasic\MasmBasic.inc
.data
buffer BYTE 128 (5)
.code
start:
invoke GetCL, 1, addr buffer ; capture first command line parameter, store it on MyBuffer
invoke OutputDebugStringA,Str$(buffer)
invoke ExitProcess, 0
end start

Related

Line number of the caller of a preloaded library function

Let's say I have a program (program.c) that uses rand function in standard C library.
1 #include <stdlib.h>
2 int main(){
3 int rand_number = rand();
4 }
I also have a shared library (intercept.c) that I created to change the behaviour of rand function (simply adds +1 to the result) in the standard library.
int rand(void){
int (*rand_func)();
rand_func = dlsym(RTLD_NEXT, "rand");
int result = (*rand_func)();
return result + 1;
}
And I run the program with
LD_PRELOAD=./intercept.so ./program
Is there any way to get the line number (Line 3) and name of the caller function (main) without modifying the program.c's source code?
It is not immediate, but you can use backtrace() in order to obtain each frame in the call stack.
Then invoking the external command eu-addr2line -f -C -s --pretty-print -p your_pid the_previous_frames... (with popen() or pipe()/fork()/dup2()/exec()...) and parsing its output will provide the information you need
(if compiled with -g).
regarding:
Is there any way to get the line number (Line 3) and name of the caller function (main) without modifying the program.c's source code?
compile the program with the -ggdb3 option, Then set a break point where you want to stop the program. Then use the backtrace command bt. This will show the function names, the line numbers, etc
Another (Linux specific) approach is to compile everything with -g (perhaps also -O) using GCC and to use Ian Taylor's excellent libbacktrace.
That library parses the DWARF debug information and knows line numbers.
You'll need several hours to understand libbacktrace (read carefully the header file). I am using it in RefPerSys

Location of the definition of open() in xv6

I have an assignment that has me design my own system call. To do this, I would like to view the definition of the open system call. By this, I mean I would like to see how the actual open(const char*, const int) is defined, not sys_open (Since I know where the source code is and can read it).
In both xv6's documentation and files in xv6-public, I am unable to find any reference of the prototype of definition.
The theory of my friend and I is that it's defined in some asm file, or some .o file.
Would anyone happen to know where the actual source code is? I'd appreciate this greatly.
Tried a ctrl-f for open in the source documentation, and tried a grep over all files in xv6-public. Found nothing.
Well,
open is declared in user.h.
open is defined in usys.S:
Lets see:
SYSCALL(open)
Will be transformed in
.globl open;
open:
movl $SYS_ ## open, %eax;
int $T_SYSCALL;
ret
What happened?
When function open is called
register %eax is set to SYS_open (which value is 15 (see syscall.h) and SYS_open is defined in sysfile.c
interuption T_SYSCALL (64 see traps.h) is raised.
after the system call returned, open returns too.

how to use address defined in linkerscript in C source file?

In a C application, I want to place a big buffer at an address after the variables, stack and dma address ranges. Of course I can define a section in the wanted location in the linker script and declare a big array in C and give a section attribute to the array. But I want to do it without actually declaring the big array because it makes the executable too big. I want to use the array address to do the same.
I'm using gcc and try to use an address I define in the linker script inside the C source file.
Here is how I tried it.
in the linker script file (which is ldabtsm.lds.S in my case),
...
. = ALIGN(16777216);
.vimbuffs : {
*(.vimbuffs)
}
vimbuffs = .;
...
I tried using vimbuffs in the C source file.
So I did (if I can print, I can use it anyway..)
extern unsigned int vimbuffs; // from linker script
printf("vimbuffs = %x\n", vimbuffs);
From the map file, I can see the vimbufs is assigned to 0x3b3f6000 which is just right, I want it to be aligned. But when run the program and print the value, I see
vimbuffs = a07f233d
What is wrong?

Windbg stacktrace shows line numbers before or after macro expansion

I have a C file with several macros.
The exe generated from the file crashes several times reporting events in the Windows event viewer. Upon taking a dump of the process and analyzing it using WinDbg with the correct pdb files for the symbols, we get the stacktrace and know the function which is causing the problem.
The stacktrace shows the line number of our function code which called other functions one of which led to the crash-
08 msvcr80!fwrite(void * buffer = 0x00000000`01ded180, unsigned int64 size =
0x1fff38, unsigned int64 count = 0x524fe123, struct _iobuf * stream =
0x00000000`00000000)+0x5f [f:\dd\vctools\crt_bld\self_64_amd64\crt\src\fwrite.c
# 77]
09 <function name>(void * param = 0x00000000`02d15a00)+0xb02
[<path to file> # 1516]
Our function called fwrite, which is shown to be at line 1516. However, there is no call to fwrite at 1516. (The crash happens because the stream argument to fwrite is 0x0)
I was wondering if these line numbers correspond to the source file after the macros are expanded ? What could be the reason for a possibly wrong line number ?
EDIT : The exe here is a debug build and was compiled with optimizations disabled.
I loaded the dump again in WinDbg but also linked in the source file to WinDbg itself this time. It points to line 1516 and upon viewing that in the source from WinDbg, it points to a line where there is no call to fwrite. However, there is such a call a few lines above.
Well i do not have direct answer to question here :(
But i would make a COD file. a file that maps source code to assembly code. Then see the assembly code generated for the function of interest. Specifically related to line 1516.
Am hoping that would give a fair insight as to whats going on behind the scene. You may want to give a quick try.
You just need to turn on a compiler flag to generate COD file. More can be read here

Undefined reference to extern int stm32

I'm using Atrollic Studio(problem also exists in Eclipse).
.h file
extern int i2cInitIO(uint channel, uint hz);
extern int i2cIO(uint device, byte *put, uint putlen, byte *get, uint getlen);
.c file
#include "tollosI2C.h"
int i2cGetReg(uint device, byte reg, byte *get) {
// write one byte address then read 1 byte data
return i2cIO(device, &reg, 1, get, 1);
} // i2cGetReg
I have a problem: undefined reference to `i2cIO'.This project is need to be compiled by ARM tool chain.
StM32F103VET6 - high density devices.I'm use ST-Link.
UPD: my .h file - http://pastebin.com/52ftBxR9
and c. file - http://pastebin.com/CcjpVZUP
Compiler invocation command - "gcc" without braces.
Compiler invocation arguments - "-E -P -v -dD ${plugin_state_location}/specs.c" without braces.
OK, your environment is called Atollic (spelling mistake), but from the name of the header file I conclude you are using the Tollos supervisor from Mike Cowlishaw.
Secondly, your compilation options may not be correct, since the -E option for GCC results in only preprocessed output being generated, the error you report is a linker error, however.
Without more information, I would assume you're missing a library containing the i2cIO implementation, probably a missing option for the linker command line.
Since you seem to be using a processor variant not directly supported by Tollos, I suppose you want to port Tollos for your processor. Check your makefile cq. Atollic project setup to include the correct libraries. And if appropriate, replace the -E option with -c.

Resources