使用C在内存中查找程序代码

时间:2022-09-01 12:00:44

If I have a string formatting program in C, that consists of only one file, is it possible to find where that file resides in memory and let the running program process its own source file?

如果我在C中有一个字符串格式化程序,它只包含一个文件,是否可以找到该文件驻留在内存中的位置并让正在运行的程序处理自己的源文件?

When a C program runs, is it possible to define a pointer that points to the actual code and process that?

当一个C程序运行时,是否可以定义一个指向实际代码的指针并进行处理?

I don't want to open and read the source file, but do it from memory.

我不想打开并读取源文件,而是从内存中执行。

4 个解决方案

#1


I don't know if I have correctly understood the question, but I give it a try:

我不知道我是否正确理解了这个问题,但我试一试:

You can access to the pathname of the source file of your C software with the __FILE__ directive. Using it for purposes other than displaying it implies that it has not moved or disappeared since the last compilation.

您可以使用__FILE__指令访问C软件源文件的路径名。将它用于显示它以外的目的意味着它自上次编译后没有移动或消失。

#2


Usually C is compiled before running. This means that the source code is translated to executable machine instructions. So the source code is probably not available in memory when you run the program.

通常在运行之前编译C语言。这意味着源代码被转换为可执行的机器指令。因此,运行程序时,源代码可能在内存中不可用。

#3


Do you mean your program analyses machine code? Or C source?

你的意思是你的程序分析机器代码?还是C源?

Your C program, once it's compiled, is not in C anymore so the running program will be in machine code, not C.

你的C程序一旦编译就不再存在于C程序中,因此正在运行的程序将是机器代码,而不是C.

Why don't you want to open the source file from disk?

为什么不想从磁盘中打开源文件?

#4


When a C program runs, is it possible to define a pointer that points to the actual code and process that?

当一个C程序运行时,是否可以定义一个指向实际代码的指针并进行处理?

I suppose you could take the address of a function by using a function pointer, and read/write by casting it to an integer pointer. However, this would be highly unusual, difficult and dangerous for numerous reasons. For writing code, the machine code could be in a read-only section of memory. Moreover, you'd have to write machine code to memory.

我想你可以通过使用函数指针获取函数的地址,并通过将其转换为整数指针来读取/写入。然而,出于多种原因,这将是非常不寻常的,困难的和危险的。对于编写代码,机器代码可以位于内存的只读部分。而且,您必须将机器代码写入内存。

#1


I don't know if I have correctly understood the question, but I give it a try:

我不知道我是否正确理解了这个问题,但我试一试:

You can access to the pathname of the source file of your C software with the __FILE__ directive. Using it for purposes other than displaying it implies that it has not moved or disappeared since the last compilation.

您可以使用__FILE__指令访问C软件源文件的路径名。将它用于显示它以外的目的意味着它自上次编译后没有移动或消失。

#2


Usually C is compiled before running. This means that the source code is translated to executable machine instructions. So the source code is probably not available in memory when you run the program.

通常在运行之前编译C语言。这意味着源代码被转换为可执行的机器指令。因此,运行程序时,源代码可能在内存中不可用。

#3


Do you mean your program analyses machine code? Or C source?

你的意思是你的程序分析机器代码?还是C源?

Your C program, once it's compiled, is not in C anymore so the running program will be in machine code, not C.

你的C程序一旦编译就不再存在于C程序中,因此正在运行的程序将是机器代码,而不是C.

Why don't you want to open the source file from disk?

为什么不想从磁盘中打开源文件?

#4


When a C program runs, is it possible to define a pointer that points to the actual code and process that?

当一个C程序运行时,是否可以定义一个指向实际代码的指针并进行处理?

I suppose you could take the address of a function by using a function pointer, and read/write by casting it to an integer pointer. However, this would be highly unusual, difficult and dangerous for numerous reasons. For writing code, the machine code could be in a read-only section of memory. Moreover, you'd have to write machine code to memory.

我想你可以通过使用函数指针获取函数的地址,并通过将其转换为整数指针来读取/写入。然而,出于多种原因,这将是非常不寻常的,困难的和危险的。对于编写代码,机器代码可以位于内存的只读部分。而且,您必须将机器代码写入内存。