xref: /openbsd-src/gnu/usr.bin/binutils-2.17/gas/literal.c (revision 3d8817e467ea46cf4772788d6804dd293abfb01a)
1*3d8817e4Smiod /* literal.c - GAS literal pool management.
2*3d8817e4Smiod    Copyright 1994, 2000, 2005 Free Software Foundation, Inc.
3*3d8817e4Smiod    Written by Ken Raeburn (raeburn@cygnus.com).
4*3d8817e4Smiod 
5*3d8817e4Smiod    This file is part of GAS, the GNU Assembler.
6*3d8817e4Smiod 
7*3d8817e4Smiod    GAS is free software; you can redistribute it and/or modify
8*3d8817e4Smiod    it under the terms of the GNU General Public License as published by
9*3d8817e4Smiod    the Free Software Foundation; either version 2, or (at your option)
10*3d8817e4Smiod    any later version.
11*3d8817e4Smiod 
12*3d8817e4Smiod    GAS is distributed in the hope that it will be useful,
13*3d8817e4Smiod    but WITHOUT ANY WARRANTY; without even the implied warranty of
14*3d8817e4Smiod    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*3d8817e4Smiod    GNU General Public License for more details.
16*3d8817e4Smiod 
17*3d8817e4Smiod    You should have received a copy of the GNU General Public License
18*3d8817e4Smiod    along with GAS; see the file COPYING.  If not, write to
19*3d8817e4Smiod    the Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
20*3d8817e4Smiod 
21*3d8817e4Smiod /* This isn't quite a "constant" pool.  Some of the values may get
22*3d8817e4Smiod    adjusted at run time, e.g., for symbolic relocations when shared
23*3d8817e4Smiod    libraries are in use.  It's more of a "literal" pool.
24*3d8817e4Smiod 
25*3d8817e4Smiod    On the Alpha, this should be used for .lita and .lit8.  (Is there
26*3d8817e4Smiod    ever a .lit4?)  On the MIPS, it could be used for .lit4 as well.
27*3d8817e4Smiod 
28*3d8817e4Smiod    The expressions passed here should contain either constants or symbols,
29*3d8817e4Smiod    not a combination of both.  Typically, the constant pool is accessed
30*3d8817e4Smiod    with some sort of GP register, so the size of the pool must be kept down
31*3d8817e4Smiod    if possible.  The exception is section offsets -- if you're storing a
32*3d8817e4Smiod    pointer to the start of .data, for example, and your machine provides
33*3d8817e4Smiod    for 16-bit signed addends, you might want to store .data+32K, so that
34*3d8817e4Smiod    you can access all of the first 64K of .data with the one pointer.
35*3d8817e4Smiod 
36*3d8817e4Smiod    This isn't a requirement, just a guideline that can help keep .o file
37*3d8817e4Smiod    size down.  */
38*3d8817e4Smiod 
39*3d8817e4Smiod #include "as.h"
40*3d8817e4Smiod #include "subsegs.h"
41*3d8817e4Smiod 
42*3d8817e4Smiod #ifdef NEED_LITERAL_POOL
43*3d8817e4Smiod 
44*3d8817e4Smiod valueT
add_to_literal_pool(sym,addend,sec,size)45*3d8817e4Smiod add_to_literal_pool (sym, addend, sec, size)
46*3d8817e4Smiod      symbolS *sym;
47*3d8817e4Smiod      valueT addend;
48*3d8817e4Smiod      segT sec;
49*3d8817e4Smiod      int size;
50*3d8817e4Smiod {
51*3d8817e4Smiod   segT current_section = now_seg;
52*3d8817e4Smiod   int current_subsec = now_subseg;
53*3d8817e4Smiod   valueT offset;
54*3d8817e4Smiod   bfd_reloc_code_real_type reloc_type;
55*3d8817e4Smiod   char *p;
56*3d8817e4Smiod   segment_info_type *seginfo = seg_info (sec);
57*3d8817e4Smiod   fixS *fixp;
58*3d8817e4Smiod 
59*3d8817e4Smiod   offset = 0;
60*3d8817e4Smiod   /* @@ This assumes all entries in a given section will be of the same
61*3d8817e4Smiod      size...  Probably correct, but unwise to rely on.  */
62*3d8817e4Smiod   /* This must always be called with the same subsegment.  */
63*3d8817e4Smiod   if (seginfo->frchainP)
64*3d8817e4Smiod     for (fixp = seginfo->frchainP->fix_root;
65*3d8817e4Smiod 	 fixp != (fixS *) NULL;
66*3d8817e4Smiod 	 fixp = fixp->fx_next, offset += size)
67*3d8817e4Smiod       {
68*3d8817e4Smiod 	if (fixp->fx_addsy == sym && fixp->fx_offset == addend)
69*3d8817e4Smiod 	  return offset;
70*3d8817e4Smiod       }
71*3d8817e4Smiod 
72*3d8817e4Smiod   subseg_set (sec, 0);
73*3d8817e4Smiod   p = frag_more (size);
74*3d8817e4Smiod   memset (p, 0, size);
75*3d8817e4Smiod 
76*3d8817e4Smiod   switch (size)
77*3d8817e4Smiod     {
78*3d8817e4Smiod     case 4:
79*3d8817e4Smiod       reloc_type = BFD_RELOC_32;
80*3d8817e4Smiod       break;
81*3d8817e4Smiod     case 8:
82*3d8817e4Smiod       reloc_type = BFD_RELOC_64;
83*3d8817e4Smiod       break;
84*3d8817e4Smiod     default:
85*3d8817e4Smiod       abort ();
86*3d8817e4Smiod     }
87*3d8817e4Smiod   fix_new (frag_now, p - frag_now->fr_literal, size, sym, addend, 0,
88*3d8817e4Smiod 	   reloc_type);
89*3d8817e4Smiod 
90*3d8817e4Smiod   subseg_set (current_section, current_subsec);
91*3d8817e4Smiod   offset = seginfo->literal_pool_size;
92*3d8817e4Smiod   seginfo->literal_pool_size += size;
93*3d8817e4Smiod   return offset;
94*3d8817e4Smiod }
95*3d8817e4Smiod #endif
96