473,854 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

utility to intentionally mangle function names?

I've got a pretty large C program with global variables and function
names strewn about (i.e. no "static" declarations in front of them).
Now I want to expose the ability for user's to supply their own shared
object / dll code which will link with my large ugly program.
Problem: naming collisions.

If my program has:

void common_function _name (void)
{
....
}

and now the caller has a similar function, they will get a link error.

What I really would like to do is "mangle" all of my names
intentionally, once I've built my code, except for those few functions
that are legitimate entry points back into my code that the caller
will need.

Thought about wrapping all my code in a namespace and compiling in C++
but this doesn't appear feasible now that I've tried it (many errors).
The other method would be to do a bunch of code conversion, which is
highly risky.

Thoughts?
Nov 14 '05 #1
8 3289
On 7 Apr 2004 07:41:45 -0700, ef***@goldengat e.com (Eric) wrote:
I've got a pretty large C program with global variables and function
names strewn about (i.e. no "static" declarations in front of them).
Now I want to expose the ability for user's to supply their own shared
object / dll code which will link with my large ugly program.
Problem: naming collisions.

If my program has:

void common_function _name (void)
{
...
}

and now the caller has a similar function, they will get a link error.

What I really would like to do is "mangle" all of my names
intentionall y, once I've built my code, except for those few functions
that are legitimate entry points back into my code that the caller
will need.
Rather than "mangle" them, why not make them static? If you can
identify them for the purpose of "mangling", surely it's easier to do
it right.
Thought about wrapping all my code in a namespace and compiling in C++
but this doesn't appear feasible now that I've tried it (many errors).
The other method would be to do a bunch of code conversion, which is
highly risky.

Thoughts?


--
Al Balmer
Balmer Consulting
re************* ***********@att .net
Nov 14 '05 #2
Eric wrote:

I've got a pretty large C program with global variables and function
names strewn about (i.e. no "static" declarations in front of them).
Now I want to expose the ability for user's to supply their own shared
object / dll code which will link with my large ugly program.
Problem: naming collisions.

If my program has:

void common_function _name (void)
{
...
}

and now the caller has a similar function, they will get a link error.

What I really would like to do is "mangle" all of my names
intentionally, once I've built my code, except for those few functions
that are legitimate entry points back into my code that the caller
will need.

Thought about wrapping all my code in a namespace and compiling in C++
but this doesn't appear feasible now that I've tried it (many errors).
The other method would be to do a bunch of code conversion, which is
highly risky.

Thoughts?


First thought: I hope you've now learned the drawbacks
of wholesale namespace pollution.

Second thought: Gather up all the objectionable names
(using ad-hoc tools -- a good place to start might be by
examining your executable's symbol table, if it has one)
and then build yourself a "mangle.h" header file:

#define common_function _name mangled_functio n_name
#define common_variable _name mangled_variabl e_name
...

#include this header at the start of each source file,
recompile, and clean up the (few, one hopes) problems.

Third thought: The success of the above approach depends
on your ability to dream up mangled names nobody else will
ever invent. That's a pretty high standard of originality,
and may even create problems where none existed before.
What you really need to do is reduce the size of the problem
(you can't eliminate it entirely) by hiding the names you
really didn't need to export in the first place. The C way
to do this is to go around sticking in `static', and the
same ad-hoc methods you used to collect the offending symbols
for "mangle.h" would also serve as a help for locating all
the right sites for `static'. Alternatively, your platform
may offer a non-C means of hiding externally-linked symbols,
typically after linking a library; consult your documentation.

Fourth thought: There really is no 100% reliable Standard
C solution to this problem, even if the code was designed from
the very beginning to be minimally intrusive of the name space.

--
Er*********@sun .com
Nov 14 '05 #3
In <b7************ **************@ posting.google. com> ef***@goldengat e.com (Eric) writes:
I've got a pretty large C program with global variables and function
names strewn about (i.e. no "static" declarations in front of them).
Now I want to expose the ability for user's to supply their own shared
object / dll code which will link with my large ugly program.
Problem: naming collisions.

If my program has:

void common_function _name (void)
{
...
}

and now the caller has a similar function, they will get a link error.

What I really would like to do is "mangle" all of my names
intentionall y, once I've built my code, except for those few functions
that are legitimate entry points back into my code that the caller
will need.

Thought about wrapping all my code in a namespace and compiling in C++
but this doesn't appear feasible now that I've tried it (many errors).


That's the right idea, you just have to implement it in C. Create your
own name space, by prefixing all the internal identifiers with a prefix
of your choice, carefully chosen to avoid likely conflicts with other
programs doing the same thing. Make a list of all the identifiers that
need to be "mangled" and a simple script around a non-interactive text
editor should solve your problem.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #4
In article <c5**********@s unnews.cern.ch> , Dan Pop wrote:
In <b7************ **************@ posting.google. com> ef***@goldengat e.com (Eric) writes:
I've got a pretty large C program with global variables and function
names strewn about (i.e. no "static" declarations in front of them).
Problem: naming collisions.

What I really would like to do is "mangle" all of my names
intentionally , once I've built my code, except for those few functions
that are legitimate entry points back into my code that the caller
will need.


That's the right idea, you just have to implement it in C. Create your
own name space, by prefixing all the internal identifiers with a prefix
of your choice, carefully chosen to avoid likely conflicts with other
programs doing the same thing. Make a list of all the identifiers that
need to be "mangled" and a simple script around a non-interactive text
editor should solve your problem.


If all your code #includes a header file, you could get
away without mangling your source code, and mangle it
transparently in the preprocessor instead.
For every global identifier that you don't want exported,

#define common_function 1 app_specific_co mmon_function1
#define common_function 2 app_specific_co mmon_function2

The substitutions will take place transparently on your
prototypes, declarations, and invocations. The user's
code that links to this stuff must _not_ #include that
header file, so it can happily use its _own_ common_function 1
without interacting with your name space.

I might take it to another level, and

#define MANGLE(x) GOLDENGATE_ ## x
#define common_function 1 MANGLE(common_f unction1)
#define common_function 2 MANGLE(common_f unction2)

This makes it easier to change your namespace in a hurry.

You can also play linker tricks, but that's probably harder,
non-portable, and definitely off topic for c.l.c.

- Larry
Nov 14 '05 #5
Thanks. I thought of another solution you started to suggest (the
linker).

I do have the option of loading a dynamic shared object (this program
runs under win and unix, many flavors). If I'm the shared object, and
I have a function with the same name as a "public" in the main, will I
get a collision? In other words, are the rules same or different than
with static libs? Seems like this might do it.

Header suggestion is also good, would go like this?

/* mangle.h */

#define common_function MANGLED_FUNCTIO N_123235

Only problem with that is looking at a thousand functions across a
hundred source files.
Nov 14 '05 #6
In article <b7************ **************@ posting.google. com>, Eric wrote:

Header suggestion is also good, would go like this?

/* mangle.h */

#define common_function MANGLED_FUNCTIO N_123235
Roughly, yes. Although I would keep some vestige of the
common_function name in the mangled version.
Only problem with that is looking at a thousand functions across a
hundred source files.


You obviously need some automation. There are better languages
than C in which to rifle through all those files to come up
with the contents of mangle.h. If you are masochistic enough
to try in C, come back to this group with sample code. Otherwise,
ask a friend fluent in perl or python for help getting started.
The end result is 100% pure C, though.

- Larry
Nov 14 '05 #7
Agree with the automation suggestion. To that end, are there any
utilities you know of that will parse a bunch of C files and:
1) output all the function names defined in those files;
2) list which functions are called by other functions and in which
module (from that, we can derive the "publics")

Assuming something like that already exists, would be easy to write
python to mangle the names and come up with a mangle.h.
Nov 14 '05 #8


Eric wrote:
Agree with the automation suggestion. To that end, are there any
utilities you know of that will parse a bunch of C files and:
1) output all the function names defined in those files;
2) list which functions are called by other functions and in which
module (from that, we can derive the "publics")

Assuming something like that already exists, would be easy to write
python to mangle the names and come up with a mangle.h.


Yes. Take a look at "cscope" and it's GUI front-end, "cbrowser" both
available for free at:

http://cscope.sourceforge.net/
http://cbrowser.sourceforge.net/

Also, check out "ccalls" which is available for a small charge at:

http://www.bell-labs.com/project/wwe...ypackages.html

Regards,

Ed.

Nov 14 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
6446
by: Paul Moore | last post by:
I was just writing some code which did date/time manipulations, and I found that the Python 2.3 datetime module does not supply a number of fairly basic functions. I understand the reasoning (the datetime module provides a set of datatypes, and doesn't attempt to get into the murky waters of date algorithms) but as these things can be quite tricky to get right, I was wondering if anyone has already implemented any date algorithms, or...
7
11099
by: Ben Kial | last post by:
Is there a Python program to convert Windows long filename, like "c:\Program Files" into the old DOS 8.3 format, like "c:\Progra~1"? Thanks in advance, Ben
20
2391
by: Randy Yates | last post by:
Why is this necessary? The identifiers provide by the programmer are unique, and that is what is important - why map what the user has specified to something else? -- Randy Yates Sony Ericsson Mobile Communications Research Triangle Park, NC, USA randy.yates@sonyericsson.com, 919-472-1124
12
404
by: Eric | last post by:
I've got a pretty large C program with global variables and function names strewn about (i.e. no "static" declarations in front of them). Now I want to expose the ability for user's to supply their own shared object / dll code which will link with my large ugly program. Problem: naming collisions. If my program has: void common_function_name (void) {
3
3805
by: rrs.matrix | last post by:
hi is there any system call in unix that will search for a file in the filesystem. someting like the find utility. if not then how should i search for a file in the file system. heard of system call by name opendir and readdir - is the how it should be done. tx.
22
5370
by: John Salerno | last post by:
I might be missing something obvious here, but I decided to experiment with writing a program that involves a class, so I'm somewhat new to this in Python. Anyway, what is the best way to create a function (A) within a class that another function (B) can use? Function A is not something that an instance will ever call, so I figure it's a choice between static or class methods, but I don't know which one, or if this is even the right...
0
1952
by: abrahamvk | last post by:
Hi, How to load data into Japanese DB2 Database using DB2 Load Utility, where the table column names are in Japanese in windows environment. We could successfully load Japanese data into a table which has English column names only. The DB2 Load Utility could not understand a table that has column names in Japanese. We tried loading data after setting the following 1. Setting the DB2CodePage to 1208 2. Setting the environment...
1
1551
by: Phil Latio | last post by:
I am thinking of writing a utility that will read a class and output something like the example below to the screen:- Class: User Variable Names: $name, $age, $sex Function Names: addUser, editUser, approveUser, activateUser, deleteUser I would like to be able to actually select which classes to process and perhaps use a stylesheet to format the output. Even might look at adding other information such as last modified date.
0
9899
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9750
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10672
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10362
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7909
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7076
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5937
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4149
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3182
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.