繁体   English   中英

将地址存储为char *中的int *

[英]store the address as int* from char*

我已经解析了objdump文件,仅在“ out.txt”文件中获得了如下的地址和函数名。现在,我想将地址存储在int *数组中并将函数名存储在const char **数组中,并且使用了strtok,但是当我我正在尝试使用atoi从char *转换为int *,所有字母都被删除了,那么如何将函数名称的地址存储在int *数组中?

400400 _init
400420 puts@plt-0x10
400430 puts@plt
400440 printf@plt
400450 __libc_start_main@plt
400460 .plt.got
400470 _start
400520 __do_global_dtors_aux
400540 frame_dummy
4005fd main
400660 __libc_csu_init
4006d0 __libc_csu_fini
4006d4 _fini



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>

int main()
{

    FILE* fp;
    long file_size;
    char* buffer;

    fp = fopen("out.txt", "rb");
    if (!fp) {
        perror("out.txt");
        exit(1);
    }

    fseek(fp, 0L, SEEK_END);
    file_size = ftell(fp);
    rewind(fp);

    buffer = calloc(1, file_size + 1);
    if (!buffer) {
        fclose(fp);
        fputs("calloc failed", stderr);
        exit(1);
    }

    if (1 != fread(buffer, file_size, 1, fp)) {

        fclose(fp);
        free(buffer);
        fputs("can't read the file", stderr);
        exit(1);
    }

    printf("%s\n", buffer);

    int* address;
    const char** names;
    char* strArray[40];

    address = calloc(1, file_size + 1);
    names = calloc(1, file_size + 1);

    char* token = strtok(buffer, " \n");
    int i = 0;
    while (token) {
        printf("%2d %s\n", i, token);
        strArray[i] = strdup(token);
        token = strtok(NULL, " \n");    
        i++;
    }
    printf("i value is %d\n",i);
    int j=0;
    int count=0;

    while(j<i){
        address[count] = atoi(strArray[j]);
        names[count] = strArray[j + 1];
        count++;
        j = j + 2;
    }
    printf("Addresses:\n");
    for(int k=0;k<count;k++){
        printf("%d\n", address[k]);

    }

    printf("Function names:\n");
    for(int m=0;m<count;m++){

        printf("%s\n", names[m]);
    }

    return 0;
}

输出是这样的,它将地址存储为int *时会删除所有字母

Addresses:
400400
400420
400430
400440
400450
4004
4004
400520
400540
4005
400660
4006
4006
Function names:
_init
puts@plt-0x10
puts@plt
printf@plt
__libc_start_main@plt
.plt.got
_start
__do_global_dtors_aux
frame_dummy
main
__libc_csu_init
__libc_csu_fini
_fini

atoi用于转换包含十进制数字的字符串。 您需要使用以16为底的strtoul来转换包含十六进制地址的字符串。 您还需要将它们打印为十六进制数字。 –伊恩·雅培(Ian Abbott)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM