LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-13-2012, 10:26 PM   #1
alphisb0t
Member
 
Registered: Jun 2004
Distribution: gentoo 2008.desktop
Posts: 110

Rep: Reputation: 15
programming for multiple architectures


I've recently got a new laptop which I installed a 64bit distro of Gentoo on. Previously I had a 32bit installation as my last laptop was pretty old and had only 3gigs of RAM.

My issue is now that I am coding on a 64bit platform suddenly I get errors compiling code that I wrote on the 32bit platform. In particular there are differences in certain structures related to ptrace.

struct user_regs_struct

This structure has .eip whereas it now uses the 64bit instruction pointer .rip.

I don't want to have two versions of the same code. There has to be a way to write architecture agnostic code that will work on either platform. Is there a standard methodology for this? Articles or books etc?

Any help is greatly appreciated!
 
Old 02-13-2012, 10:48 PM   #2
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 211Reputation: 211Reputation: 211
struct user_regs_struct is a kernel entity, correct?

In this case, you could cross compile a 32-bit kernel with
Code:
make ARCH=i386
Or, if you are building things that include user.h, be sure to use the one in the kernel source tree, not the one in /usr/include (which will have been generated for x86_64.) Alternately, you can install the 32 bit headers to a separate directory. (On Ubuntu for example arch-dependent headers are in /usr/include/x86_64-linux-gnu/ and similar.)

Also, if you're just trying to make some 32 bit binaries, add an -m32 flag.

Last edited by jhwilliams; 02-13-2012 at 10:58 PM.
 
Old 02-14-2012, 07:41 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Are you absolutely sure that you have to use registers directly?
 
Old 02-14-2012, 12:52 PM   #4
alphisb0t
Member
 
Registered: Jun 2004
Distribution: gentoo 2008.desktop
Posts: 110

Original Poster
Rep: Reputation: 15
Including headers from kernel source instead of my local /usr/include is a good idea. However the fact that the name of the struct member representing the instruction pointer is completely different depending on your arch (.eip vs .rip) it wouldn't work in this case either. There are tons of open source software that you can build regardless of your arch. I'm sure there is a standard way to do this, I'm just not sure how since I've never had this particular problem before.

@NevemTeve: yes the purpose of this ptrace program is specifically related to being able to debug an application. I need to be able to access members of the user_regs_struct.

I'm familiar with pragma comments for some functionality (like linking against libs not setup explicitly in the project itself) and I suspect I can use these for arch differences as well.

Any ideas/suggestions are much appreciated.
 
Old 02-14-2012, 02:17 PM   #5
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Use architecture-specific accessors to get information from the user_regs_struct, and treat the structure itself as an opaque cookie.
Code:
#if defined(__x86_64)
/* x86-64 user_regs_struct accessors:
*/
#define REG_IP_NAME      "rip"
#define REG_IP_TYPE      unsigned long
#define REG_IP_FMT       "lu"
#define REG_IP_HEX       "lx"
#define REG_IP_VALUE(r)  ((r).rip)

#define REG_ALL_PATTERN   "rip=0x%lx r15=0x%lx ... sp=0x%lx"
#define REG_ALL_VALUES(r) (r).rip, (r).r15, (r).sp

#elif defined(__i386)
/* x86 user_regs_struct accessors:
*/
#define REG_IP_NAME      "ip"
#define REG_IP_TYPE      unsigned long
#define REG_IP_FMT       "lu"
#define REG_IP_HEX       "lx"
#define REG_IP_VALUE(r)  ((r).ip)

#define REG_ALL_PATTERN   "ip=0x%lx ax=0x%lx ... sp=0x%lx"
#define REG_ALL_VALUES(r) (r).ip, (r).ax, (r).sp

#else
#error Unsupported architecture
#endif

/* Example: output the values of all generic registers to a string.
*/
static size_t generic_registers(char *const buffer, const size_t len, const struct user_regs_struct regs)
{
    return snprintf(buffer, len, REG_ALL_PATTERN, REG_ALL_VALUES(regs));
}
These accessors are just examples. You may also wish to use static inline functions, but preprocessor macros let you use any type in your program.

If you need to modify the register values, then I recommend you create an array of all logical register names categorized by type, the use an accessor function which returns a void pointer to that value in the user_regs_struct, and wrap it around a macro which dereferences the pointer using the correct type. (This way the macro is an lvalue.)

If you want a more detailed example, describe some of the actions you need to do wrt. the user_regs_struct, and I'd be happy to oblige.

Last edited by Nominal Animal; 02-14-2012 at 02:19 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Learning Multiple Programming languages. jmc1987 Programming 5 11-05-2011 10:53 AM
Programming in multiple languages - How? steve296 Programming 7 07-13-2010 09:18 AM
[SOLVED] PXeboot install ubuntu 10.04 with multiple architectures linuxmandrake Linux - Server 1 05-03-2010 08:31 PM
Programming to multiple video cards Max_r Programming 14 06-28-2009 10:51 AM
About multiple timeout in Socket Programming lisa000 Linux - General 1 10-20-2007 05:07 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration