gcc - How to Compile a C program which contains 32bit asm into .o file? -


introduction

i'm following through book "learning linux binary analysis". have experience working 32 bit assembly , c (however still consider myself novice). i'm having trouble , confusion of how compile c program , contains 32 bit assembly object file .o. im guessing compilation issue on part.

the source code part of example of code injection-based binary patching.

source code

#include <sys/syscall.h> int _write (int fd, void *buf, int count) {   long ret;   __asm__ __volatile__ ("pushl %%ebx\n\t"   "movl %%esi,%%ebx\n\t"   "int $0x80\n\t""popl %%ebx":"=a" (ret)                           :"0" (sys_write), "s" ((long) fd),   "c" ((long) buf), "d" ((long) count));     if (ret >= 0) {         return (int) ret;  }  return -1; } int evil_puts(void) {         _write(1, "haha puts() has been hijacked!\n", 31); } 

the problem

i attempt compile evil_puts.c .o file. used later injection simple program.

gcc -c evil_puts.c 

evil_puts.c: assembler messages:

evil_puts.c:5: error: invalid instruction suffix `push'

evil_puts.c:8: error: invalid instruction suffix `pop'

i've received before when working 32 assembly gas. , solve put '-32' flag when compiling , linking. i'm guessing problem? not sure, , don't have idea of how compile in 32 bit c , gcc if that's case?

i attempted change 64bit see if work, replacing 'l' of every command 'q' , changing registers begin 'r'. seems work. book uses 32 bit. wish keep way. ideas? sorry if basic question.

also tried '-m32' receive this:

fatal error: sys/syscall.h: no such file or directory

use gcc -m32 -c evil_puts.c -o evil_puts.o

you're getting error because don't have 32-bit libraries installed.

if using ubuntu:

sudo apt-get install gcc-multilib


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -