3

I wrote codes below and included asm/ptrace.h, compiled codes with GCC in linux, the compiler told

‘struct pt_regs’ has no member named ‘ARM_sp’

What can I do to solve this problem?

#include <asm/ptrace.h>
extern struct pt_regs saved_regs;
//...
uint32_t * inject_so_of(pid_t pid,const char *so_path)
{
    struct pt_regs regs;
    memcpy(&regs,&saved_regs,sizeof(regs));
    ptrace_readdata(pid,(void *)regs.ARM_sp,(void *)sbuf, sizeof(sbuf));
    ptrace_writedata(pid,(void *)regs.ARM_sp,(void *)so_path,strlen(so_path));
    uint32_t parameters[2];
    parameters[0] = regs.ARM_sp;
    parameters[1] = RTLD_NOW;
    if(ptrace_call(pid,find_dlopen_addr(pid),parameters,2,&regs) == -1)
    {
        DPRINTF("dlopen_error");
    }
    ptrace_getregs(pid,&regs);
    uint32_t r0 = regs.ARM_r0;
    DPRINTF("[+2]\t");
    ptrace_setregs(pid,&saved_regs);
    ptrace_writedata(pid,(uint8_t *)saved_regs.ARM_sp,(uint8_t *) sbuf,sizeof(sbuf));
    ptrace_detach(pid);
    return (uint32_t *) r0;
}
//...

The definition of pt_regs in asm/ptrace.h is:

struct pt_regs
{
    long uregs[18];
};

#define ARM_cpsr uregs[16]
#define ARM_pc uregs[15]
#define ARM_lr uregs[14]
/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */
#define ARM_sp uregs[13]
#define ARM_ip uregs[12]
#define ARM_fp uregs[11]
#define ARM_r10 uregs[10]

I am getting the following errors:

AndroidInjectSo.c:149:38: error: ‘struct pt_regs’ has no member named ‘ARM_sp’
 ptrace_writedata(pid,(void *)regs.ARM_sp,(void *)so_path,strlen(so_path));
                                  ^
AndroidInjectSo.c:151:25: error: ‘struct pt_regs’ has no member named ‘ARM_sp’
 parameters[0] = regs.ARM_sp;
                     ^
AndroidInjectSo.c:158:23: error: ‘struct pt_regs’ has no member named ‘ARM_r0’
 uint32_t r0 = regs.ARM_r0;
                   ^
AndroidInjectSo.c:161:47: error: ‘struct pt_regs’ has no member named ‘ARM_sp’
 ptrace_writedata(pid,(uint8_t *)saved_regs.ARM_sp,(uint8_t *) sbuf,sizeof(sbuf));
Blacktempel
  • 3,935
  • 3
  • 29
  • 53
Harold
  • 39
  • 3
  • 1
    Did you compile the "codes" as C or C++? You can only compile in one mode with GCC. Couple that with the languages being different, and this double tagging is just wrong. – StoryTeller - Unslander Monica Aug 29 '17 at 10:00
  • "code" is definitely the right form for programming related stuff. This one definitely seems like C - see the file extension from the screenshot. It also uses C-style of many macros, C-style casts and C-strings. – tambre Aug 29 '17 at 10:05
  • This code [seems to compile fine](https://godbolt.org/g/Bzg3VY). – vgru Aug 29 '17 at 10:23
  • It's a C program,I used "gcc -o AndroidInjectSo AndroidInjectSo.c" to compile it. – Harold Aug 29 '17 at 10:25
  • That's right it should compile with gcc, I guess the preprocessing is failing for some reason, and not replacing `ARM_sp` by `uregs[13]` – Andrés Alcarraz Aug 29 '17 at 10:28
  • It's also weird that you are casting a long to a pointer as in `(void *)regs.ARM_sp` – Andrés Alcarraz Aug 29 '17 at 10:29
  • @Harold: call `gcc -E AndroidInjectSo.c` to generate the preprocessed file `AndroidInjectSo.i`. Open this file in notepad, and search for `ARM_sp` to see if it's defined anywhere. I am presuming your are pulling the wrong header from somewhere. You can also create a separate test file [like this](https://godbolt.org/g/cwjrks) and try to compile it. – vgru Aug 29 '17 at 10:29
  • 1
    @Groo Thanks,I tried as you said and it's wierd there is no defination of ARM_sp in the preprocessed file,it only appears in where I qouted it in the code. – Harold Aug 29 '17 at 10:41
  • @Harold: ok, so there is another `"ptrace.h"` stored somewhere in your common library. You probably need to change `#include ` into `#include "asm/ptrace.h"` (if `ptrace.h` is stored inside the `arm` folder next to `AndroidInjectSo.c`). When you use angle brackets in `#include`, compiler will [search its own directories](https://stackoverflow.com/a/21594/69809) for the specified header, **before** trying inside the header in your project. – vgru Aug 29 '17 at 10:44

0 Answers0