473,769 Members | 6,120 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Undefined reference to memcpy in standalone program

Hi,

I've just upgraded my gcc and I'm currently trying to compile some code for
my own operating system kernel, but I am getting an error of "Undefined
reference to `memcpy`" when I try to link it using the GNU linker. I do not
reference the symbol memcpy explicitly anywhere in the offending function.
I have narrowed the offending code down to the initialisation of an array
of pointers to char (i.e. an array of strings), which is given below. It
seems that for some reason gcc is inserting calls to the standard library
in some attempt to optimise my code. How do I stop it from doing this? I'm
running version 3.3.1 and am compiling with the --freestanding flag, which
I thought should be enough to prevent this kind of behaviour (it certainly
was on previous versions; either that or gcc did not try and perform this
action then).

char* CPUstrings[] = {
"UNKNOWN Processor",
"Generic 386",
"NexGen 586",
"Cyrix M1 or IBM Bluelightning",
"Generic 486",
"Intel 386",
"Intel 486DX",
"Intel 486SX",
"Intel 486DX2",
"Intel 486SL",
"Intel 486SX2",
"Intel 486DX2 WB",
"Intel 486DX4",
"Intel 486DX4 WB",
"Intel 486",
"Intel Pentium Early P5",
"Intel Pentium 80501",
"Intel Pentium P54C 80502",
"Intel Pentium P24T (overdrive for 486 socket)",
"Intel Pentium MMX P55C",
"Intel Pentium P54C",
"Intel Pentium MMX P55",
"Intel Pentium P5",
"Intel Pentium Pro (Sample)",
"Intel Pentium Pro",
"Intel Pentium II (Klamath)",
"Intel Pentium II (Deschutes)/Xeon/Celeron",
"Intel Celeron A/Pentium II",
"Intel Pentium III (Katmai)",
"Intel Pentium III (Coppermine)",
"Intel Pentium III Xeon (Cascades)",
"Intel Pentium P6",
"Intel Pentium IV",
"UMC 486DX",
"UMC 486SX"
"UMC 486",
"AMD 486DX2, or DX4 in 2x WT mode",
"AMD 486DX2, or DX4 in 2x WB mode",
"AMD 486DX4, or 5x86 in 3x WT mode",
"AMD 486DX4 SV8B, 3x WB mode",
"AMD 5x86, 4x WT mode",
"AMD 5x86, 4x WB mode",
"AMD 486",
"AMD K5 Model 0",
"AMD K5 Model 1",
"AMD K5 Model 2",
"AMD K5 Model 3",
"AMD K6 Model 6",
"AMD K6 Model 7",
"AMD K6-II Model 8",
"AMD K6-III Model 9 (Sharptooth)",
"AMD K6-II+/K6-III+",
"AMD K5/K6",
"AMD Athlon 0.25u (external L2 cache)",
"AMD Athlon 0.18u (external L2 cache)",
"AMD Duron",
"AMD Athlon 0.18u (integrated L2 cache)",
"AMD K7 Athlon/Duron",
"Rise mP6 iDragon .25u",
"Rise mP6 iDragon .18u",
"Rise mP6 iDragon II .25u",
"Rise mP6 iDragon II .18u",
"Rise mP6",
"TransMeta Crusoe"
};

Thanks!

Richard Hayden.
Nov 14 '05 #1
7 12096
begin followup to Richard Hayden:
char* CPUstrings[] = {


Try again with

const char* const CPUstrings[] = {

--
Für Google, Tux und GPL!
Nov 14 '05 #2
Alexander Bartolich wrote:
begin followup to Richard Hayden:
char* CPUstrings[] = {


Try again with

const char* const CPUstrings[] = {


No change in output I'm afraid...

Thanks,

Richard Hayden.
Nov 14 '05 #3
begin followup to Richard Hayden:
No change in output I'm afraid...


Then the problem is something else.

$ const char* const CPUstrings[] = {
"UNKNOWN Processor",
"Generic 386"
};

void _start()
{
asm(
"mov $4,%eax\n"
"mov $1,%ebx\n"
"mov (CPUstrings),%e cx\n"
"mov $17,%edx\n"
"int $0x80\n"

"mov $1,%eax\n"
"mov $27,%ebx\n"
"int $0x80\n"
);
}

$ gcc -c -ffreestanding -Wall stand.c && ld stand.o -o stand
$ ./stand ; /bin/echo -e "\n$?"
UNKNOWN Processor
27

$ objdump -M intel -d stand

stand: file format elf32-i386

Disassembly of section .text:

08048074 <_start>:
8048074: 55 push ebp
8048075: 89 e5 mov ebp,esp
8048077: b8 04 00 00 00 mov eax,0x4
804807c: bb 01 00 00 00 mov ebx,0x1
8048081: 8b 0d 9c 80 04 08 mov ecx,ds:0x804809 c
8048087: ba 11 00 00 00 mov edx,0x11
804808c: cd 80 int 0x80
804808e: b8 01 00 00 00 mov eax,0x1
8048093: bb 1b 00 00 00 mov ebx,0x1b
8048098: cd 80 int 0x80
804809a: 5d pop ebp
804809b: c3 ret

Obviously this thread has left the topic of comp.lang.c far behind...
Perhaps comp.lang.asm.x 86, gnu.gcc or linux.dev.gcc is better suited.

--
Für Google, Tux und GPL!
Nov 14 '05 #4
In article <news:bs******* ***@titan.btint ernet.com>
Richard Hayden <ra********@yah oo.co.uk> writes:
... I am getting an error of "Undefined reference to `memcpy`" [even though he never calls memcpy() himself].
I have narrowed the offending code down to the initialisation of an array
of pointers to char (i.e. an array of strings), which is given below.
This is probably not it.

More likely you have some function like:

void fn(void) {
char buf[] = "some initialization" ;
...
}

and gcc is doing the initialization via memcpy(); or you have a
struct-to-struct assignment that the compiler implements via a
call to memcpy() (although under various optimizations gcc will
subsequently inline this on x86 CPUs).
It seems that for some reason gcc is inserting calls to the
standard library in some attempt to optimise my code. How do
I stop it from doing this? I'm running version 3.3.1 and am
compiling with the --freestanding flag ...


("-ffreestanding", I presume.)

Unfortunately, hosted compilers are allowed to "know" all about
the entire C standard library and make arbitrary changes to your
source, and freestanding compilers are sufficiently difficult to
talk about to be mostly off-topic in comp.lang.c (freestanding
systems may not even have a main() function, as no doubt you already
know, having no doubt had to write your own startup code -- you can
then pick your own name for the entry point for your program).

GCC in particular may still use memcpy(), memset(), and a whole
host of generic 64-bit function calls (__muldi2, __divdi2, etc.),
even under -ffreestanding, depending on the machine in question
and sometimes on optimization levels. It may also need machine-
specific functions for other "ordinary" operations, such as
integer multiply and divide (.mul, .urem, etc., on pre-V8 sparc).

With GCC in particular, once you know which .o file(s) refer to
the unwanted function, you can compile to assembly code and read
this to pin down precisely where and why it wants some particular
function. It usually only takes a little "thinking like a
compiler-writer", at that point, to come up with ways to change
the generated code. (Or of course -- as in this case -- you can
simply write a little standalone memcpy()...)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #5

"Richard Hayden" <ra********@yah oo.co.uk> wrote in message
news:bs******** **@titan.btinte rnet.com...
Hi,

I've just upgraded my gcc and I'm currently trying to compile some code for my own operating system kernel, but I am getting an error of "Undefined
reference to `memcpy`" when I try to link it using the GNU linker. I do not reference the symbol memcpy explicitly anywhere in the offending function.
I have narrowed the offending code down to the initialisation of an array
of pointers to char (i.e. an array of strings), which is given below. It
seems that for some reason gcc is inserting calls to the standard library
in some attempt to optimise my code. How do I stop it from doing this? I'm
running version 3.3.1 and am compiling with the --freestanding flag, which
I thought should be enough to prevent this kind of behaviour (it certainly
was on previous versions; either that or gcc did not try and perform this
action then).

char* CPUstrings[] = {
"UNKNOWN Processor",
"Generic 386",
"NexGen 586",
"Cyrix M1 or IBM Bluelightning",
"Generic 486",
"Intel 386",
"Intel 486DX",
"Intel 486SX",
"Intel 486DX2",
"Intel 486SL",
"Intel 486SX2",
"Intel 486DX2 WB",
"Intel 486DX4",
"Intel 486DX4 WB",
"Intel 486",
"Intel Pentium Early P5",
"Intel Pentium 80501",
"Intel Pentium P54C 80502",
"Intel Pentium P24T (overdrive for 486 socket)",
"Intel Pentium MMX P55C",
"Intel Pentium P54C",
"Intel Pentium MMX P55",
"Intel Pentium P5",
"Intel Pentium Pro (Sample)",
"Intel Pentium Pro",
"Intel Pentium II (Klamath)",
"Intel Pentium II (Deschutes)/Xeon/Celeron",
"Intel Celeron A/Pentium II",
"Intel Pentium III (Katmai)",
"Intel Pentium III (Coppermine)",
"Intel Pentium III Xeon (Cascades)",
"Intel Pentium P6",
"Intel Pentium IV",
"UMC 486DX",
"UMC 486SX"
"UMC 486",
"AMD 486DX2, or DX4 in 2x WT mode",
"AMD 486DX2, or DX4 in 2x WB mode",
"AMD 486DX4, or 5x86 in 3x WT mode",
"AMD 486DX4 SV8B, 3x WB mode",
"AMD 5x86, 4x WT mode",
"AMD 5x86, 4x WB mode",
"AMD 486",
"AMD K5 Model 0",
"AMD K5 Model 1",
"AMD K5 Model 2",
"AMD K5 Model 3",
"AMD K6 Model 6",
"AMD K6 Model 7",
"AMD K6-II Model 8",
"AMD K6-III Model 9 (Sharptooth)",
"AMD K6-II+/K6-III+",
"AMD K5/K6",
"AMD Athlon 0.25u (external L2 cache)",
"AMD Athlon 0.18u (external L2 cache)",
"AMD Duron",
"AMD Athlon 0.18u (integrated L2 cache)",
"AMD K7 Athlon/Duron",
"Rise mP6 iDragon .25u",
"Rise mP6 iDragon .18u",
"Rise mP6 iDragon II .25u",
"Rise mP6 iDragon II .18u",
"Rise mP6",
"TransMeta Crusoe"
};


You have specified that the strings and the pointer table to them have to be
initialised read/write data. Make the pointer table and target data
constants using the 'const' keyword twice to avoid the initialisation (const
char * const CPUstrings[] ...). This has the side effect that you have to
use the table and strings in such context that const data is allowed there.

HTH

Tauno Voipio
tauno voipio @ iki fi

Nov 14 '05 #6
On Wed, 24 Dec 2003 23:29:54 +0000 (UTC), Richard Hayden
<ra********@yah oo.co.uk> wrote:
Hi,

I've just upgraded my gcc and I'm currently trying to compile some code for
my own operating system kernel, but I am getting an error of "Undefined
reference to `memcpy`" when I try to link it using the GNU linker. I do not
reference the symbol memcpy explicitly anywhere in the offending function.
I have narrowed the offending code down to the initialisation of an array
of pointers to char (i.e. an array of strings), which is given below. <snip>
char* CPUstrings[] = { <snipped>


In addition to the other answers, especially (as usual) Chris Torek,
is there a reason you actually need it initialized at runtime and
can't just make it static? Doesn't your OS/boot support init data? Do
you want to make changes one time through the function and have them
gone (restored) the next time through (doesn't seem likely to me)?

- David.Thompson1 at worldnet.att.ne t
Nov 14 '05 #7
Eric Sosman wrote:
Hi,

I've just upgraded my gcc and I'm currently trying to compile some code for
my own operating system kernel, but I am getting an error of "Undefined
reference to `memcpy`" when I try to link it using the GNU linker. I do not
reference the symbol memcpy explicitly anywhere in the offending function.
I have narrowed the offending code down to the initialisation of an array
of pointers to char (i.e. an array of strings), which is given below. It
seems that for some reason gcc is inserting calls to the standard library
in some attempt to optimise my code. How do I stop it from doing this? I'm
running version 3.3.1 and am compiling with the --freestanding flag, which
I thought should be enough to prevent this kind of behaviour (it certainly
was on previous versions; either that or gcc did not try and perform this
action then).

char* CPUstrings[] = {
"UNKNOWN Processor",
"Generic 386",
"NexGen 586",
"Cyrix M1 or IBM Bluelightning",
"Generic 486",
"Intel 386",
"Intel 486DX",
"Intel 486SX",
"Intel 486DX2",
"Intel 486SL",
"Intel 486SX2",
"Intel 486DX2 WB",
"Intel 486DX4",
"Intel 486DX4 WB",
"Intel 486",
"Intel Pentium Early P5",
"Intel Pentium 80501",
"Intel Pentium P54C 80502",
"Intel Pentium P24T (overdrive for 486 socket)",
"Intel Pentium MMX P55C",
"Intel Pentium P54C",
"Intel Pentium MMX P55",
"Intel Pentium P5",
"Intel Pentium Pro (Sample)",
"Intel Pentium Pro",
"Intel Pentium II (Klamath)",
"Intel Pentium II (Deschutes)/Xeon/Celeron",
"Intel Celeron A/Pentium II",
"Intel Pentium III (Katmai)",
"Intel Pentium III (Coppermine)",
"Intel Pentium III Xeon (Cascades)",
"Intel Pentium P6",
"Intel Pentium IV",
"UMC 486DX",
"UMC 486SX"
"UMC 486",
"AMD 486DX2, or DX4 in 2x WT mode",
"AMD 486DX2, or DX4 in 2x WB mode",
"AMD 486DX4, or 5x86 in 3x WT mode",
"AMD 486DX4 SV8B, 3x WB mode",
"AMD 5x86, 4x WT mode",
"AMD 5x86, 4x WB mode",
"AMD 486",
"AMD K5 Model 0",
"AMD K5 Model 1",
"AMD K5 Model 2",
"AMD K5 Model 3",
"AMD K6 Model 6",
"AMD K6 Model 7",
"AMD K6-II Model 8",
"AMD K6-III Model 9 (Sharptooth)",
"AMD K6-II+/K6-III+",
"AMD K5/K6",
"AMD Athlon 0.25u (external L2 cache)",
"AMD Athlon 0.18u (external L2 cache)",
"AMD Duron",
"AMD Athlon 0.18u (integrated L2 cache)",
"AMD K7 Athlon/Duron",
"Rise mP6 iDragon .25u",
"Rise mP6 iDragon .18u",
"Rise mP6 iDragon II .25u",
"Rise mP6 iDragon II .18u",
"Rise mP6",
"TransMeta Crusoe"
};

Thanks!

Richard Hayden.


A while ago I read that memcpy was the one function reference
gcc-compiled code cannot do without.

Why not implement it yourself, or link it in from some gcc-provided
library for your target platform? You don't buy any unnecessary code
when doing this.

greetings,
Thomas

Nov 14 '05 #8

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

Similar topics

2
13125
by: RU | last post by:
Hi, I am working on a porting project to port C/C++ application from unixware C++, AT&T Standard components to g++ with STL on Linux. This application has been working properly on Unixware/C++/AT&T componets environment. I have been able to compile all modules after making necessary changes in LINUX/gcc/STL environment. We have two templates defined XList and XMap.
2
40617
by: ferdous | last post by:
Hi Greetings to all I am new to ProC. I am compiling a very simple program written in ProC. I can compile the program using proc filename.pc and it generates the corresponding c code :) , but when i try to compile the C program using cc -O3 -DPRECOMP -I. -I/opt/oracle/products/9.2.0/rdbms/public -I/opt/oracle/products/9.2.0/precomp/public -I/opt/oracle/products/9.2.0/rdbms/demo
3
11837
by: blueblueblue2005 | last post by:
Hi, I am learning C++ using the examples from Deitel How to Program in C++. I download the example code. now I am working on the operator overloading. I am working under Ubuntu Linux 5.04. I installed the g++ compiler package from synaptic package manager, g++-3.3, and set up a soft link g++. when I compile the sample code( the command I use is: g++ fig11_05.cpp), I got error message like this: /tmp/ccJDZHAY.o(.text+0x4f): In function...
2
2843
by: D. Stimits | last post by:
In PostgreSQL 7.2 (Redhat 7.3 version, so it is patched), I'm trying to create a Version-1 C server extension. The Version-0 format works, the Version-1 version fails with: undefined reference to 'pg_detoast_datum' According to docs at: http://www.postgresql.org/docs/7.2/interactive/xfunc-c.html ....by using Version-1 the pg_detoast_datum is no longer needed. FYI, the function being created takes and returns a text argument. Are the...
4
75442
by: Albert Oppenheimer | last post by:
I have a small program, test.c. It runs OK. This is on Linux (kernel 2.6.8.1, GCC 3.4.1). I decided to convert it to C++ before expanding it to a larger program. I changed the filename to test.cpp and added extern "C" { } around main and all the non-static subroutines. It compiles OK, but when I
5
11360
by: druberego | last post by:
I read google and tried to find the solution myself. YES I do know that you can get undefined references if you: a) forget to implement the code for a prototype/header file item, or b) you forget to pass all the necessary object files to the linker. Neither of those are my problem. Please bear with me as the question I ask is rather long and I think it's beyond a CS101 level of linker stupidity. If it is a stupid CS101 mistake I'm making...
3
2054
by: s.z.s | last post by:
Hi! I hope the solution to that is not too stupid... I've got three files: <snip test_main.cc> #include"test.hh" int main(void) { A<inta1; a1.saywhat();
1
10925
by: taiyang902 | last post by:
i program under linux ,and using kdevelop c/c++. the code follow, #ifdef HAVE_CONFIG_H #include <config.h> #endif #include <iostream> #include <cstdlib> using namespace std;
1
1492
by: ajaycse | last post by:
I am facing the problem of calling cfitsio library from C program in linux. when i am giving compiling my C program using options "gcc -o CreateLevel1DataFitsFile CreateLevel1DataFitsFile.c -lm -lcfitsio " i am getting following error /usr/lib/../lib/gcc/i386-redhat-linux/3.4.4/../../../libcfitsio.a(getcolj.o)(.text+0x7065):/backup/Astrosat/learning/cfits/cfitsio/getcolj.c:3098: more undefined references to `__xtoll' follow...
0
9590
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...
1
10000
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8879
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6675
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
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3968
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3571
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
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.