您可以使用LD

编程入门 行业动态 更新时间:2024-10-09 15:23:04
本文介绍了您可以使用LD_PRELOAD更改结构吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

可能重复: 是否可以LD_PRELOAD带有不同参数的函数?

Possible Duplicate: Is it possible to LD_PRELOAD a function with different parameters?

我有一个结构,我们称它为 my_struct ,它声明如下.

I have a structure, lets call it my_struct, which is declared as follows.

struct my_struct { int a; int b; }

出于某种原因,我想向其添加一个新字段,因此我想按如下所述对其进行更改.

For some reason I want to add a new field to it, so I want to change it as follows.

struct my_struct { int a; int b; int c; }

说我在共享对象库中执行此操作,然后我通过LD_PRELOAD运行该程序.我的程序将使用第二个定义还是原始的定义.

Say I do this in my shared object library which I then LD_PRELOAD to run the program. Will my program use the second definition or the original one.

推荐答案

否,原始代码将不使用新定义.试想一下,如果这样做了,那将有什么改变.首先,必须将堆上的每个my_struct扩展为包括新字段.这可能意味着必须调整数据段的大小,并且所有向前移动的数据都需要重新定位其引用.在编译时评估的sizeof struct my_struct将与新定义不一致.这些只是使您无法做的事情的一些原因(至少以您尝试的方式).

No, the original code will not use the new definition. Imagine the case if it did and what would have to change. Firstly, each my_struct on the heap would have to be expanded to include the new field. This would likely mean that data sections have to be resized and all data that is shifted forward would need its references relocated. sizeof struct my_struct, which is evaluated at compile-time would then be inconsistent with the new definition. These are just a few of the reasons which make what you are trying to do impossible (at least in the way you're attempting).

根据旧定义编译并链接的原始代码将继续使用旧的my_struct.共享对象中的任何新代码都会根据新的定义进行编译并使用,因此将具有c字段.

The original code that was compiled and linked against the old definition will continue to use the old my_struct. Any new code in your shared object will be compiled against and use the new definition and hence will have a c field.

一旦原始程序被编译,它对my_struct一无所知.该信息仅由编译器用来生成访问结构成员的偏移量,并使其知道如何将其布置在内存中.信息仍然可能以符号形式提供,但这不会影响定义的获取方式.与函数解析不同,代码/数据的生成和结构解析严格来说是编译时操作.

Once the original program has been compiled, it does not know anything about my_struct. That information is just used by the compiler to generate offsets to access structure members and allows it to know how to lay it out in memory. It is possible that information is still available in the form of symbols but this would not effect how the definition is picked up. Unlike function resolution, the code/data generation and resolution of a structure is strictly a compile-time operation.

更多推荐

您可以使用LD

本文发布于:2023-10-26 18:04:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1530927.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:您可以   LD

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!