125b3c049e70834cf33790a28643ab058b507b35cBen Cheng/* Create new subsection section in given section.
225b3c049e70834cf33790a28643ab058b507b35cBen Cheng   Copyright (C) 2002 Red Hat, Inc.
325b3c049e70834cf33790a28643ab058b507b35cBen Cheng   This file is part of Red Hat elfutils.
425b3c049e70834cf33790a28643ab058b507b35cBen Cheng   Written by Ulrich Drepper <drepper@redhat.com>, 2002.
525b3c049e70834cf33790a28643ab058b507b35cBen Cheng
625b3c049e70834cf33790a28643ab058b507b35cBen Cheng   Red Hat elfutils is free software; you can redistribute it and/or modify
725b3c049e70834cf33790a28643ab058b507b35cBen Cheng   it under the terms of the GNU General Public License as published by the
825b3c049e70834cf33790a28643ab058b507b35cBen Cheng   Free Software Foundation; version 2 of the License.
925b3c049e70834cf33790a28643ab058b507b35cBen Cheng
1025b3c049e70834cf33790a28643ab058b507b35cBen Cheng   Red Hat elfutils is distributed in the hope that it will be useful, but
1125b3c049e70834cf33790a28643ab058b507b35cBen Cheng   WITHOUT ANY WARRANTY; without even the implied warranty of
1225b3c049e70834cf33790a28643ab058b507b35cBen Cheng   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1325b3c049e70834cf33790a28643ab058b507b35cBen Cheng   General Public License for more details.
1425b3c049e70834cf33790a28643ab058b507b35cBen Cheng
1525b3c049e70834cf33790a28643ab058b507b35cBen Cheng   You should have received a copy of the GNU General Public License along
1625b3c049e70834cf33790a28643ab058b507b35cBen Cheng   with Red Hat elfutils; if not, write to the Free Software Foundation,
1725b3c049e70834cf33790a28643ab058b507b35cBen Cheng   Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
1825b3c049e70834cf33790a28643ab058b507b35cBen Cheng
1925b3c049e70834cf33790a28643ab058b507b35cBen Cheng   Red Hat elfutils is an included package of the Open Invention Network.
2025b3c049e70834cf33790a28643ab058b507b35cBen Cheng   An included package of the Open Invention Network is a package for which
2125b3c049e70834cf33790a28643ab058b507b35cBen Cheng   Open Invention Network licensees cross-license their patents.  No patent
2225b3c049e70834cf33790a28643ab058b507b35cBen Cheng   license is granted, either expressly or impliedly, by designation as an
2325b3c049e70834cf33790a28643ab058b507b35cBen Cheng   included package.  Should you wish to participate in the Open Invention
2425b3c049e70834cf33790a28643ab058b507b35cBen Cheng   Network licensing program, please visit www.openinventionnetwork.com
2525b3c049e70834cf33790a28643ab058b507b35cBen Cheng   <http://www.openinventionnetwork.com>.  */
2625b3c049e70834cf33790a28643ab058b507b35cBen Cheng
2725b3c049e70834cf33790a28643ab058b507b35cBen Cheng#ifdef HAVE_CONFIG_H
2825b3c049e70834cf33790a28643ab058b507b35cBen Cheng# include <config.h>
2925b3c049e70834cf33790a28643ab058b507b35cBen Cheng#endif
3025b3c049e70834cf33790a28643ab058b507b35cBen Cheng
3125b3c049e70834cf33790a28643ab058b507b35cBen Cheng#include <stdlib.h>
3225b3c049e70834cf33790a28643ab058b507b35cBen Cheng
3325b3c049e70834cf33790a28643ab058b507b35cBen Cheng#include <libasmP.h>
3425b3c049e70834cf33790a28643ab058b507b35cBen Cheng#include <system.h>
3525b3c049e70834cf33790a28643ab058b507b35cBen Cheng
3625b3c049e70834cf33790a28643ab058b507b35cBen Cheng
3725b3c049e70834cf33790a28643ab058b507b35cBen ChengAsmScn_t *
3825b3c049e70834cf33790a28643ab058b507b35cBen Chengasm_newsubscn (asmscn, nr)
3925b3c049e70834cf33790a28643ab058b507b35cBen Cheng     AsmScn_t *asmscn;
4025b3c049e70834cf33790a28643ab058b507b35cBen Cheng     unsigned int nr;
4125b3c049e70834cf33790a28643ab058b507b35cBen Cheng{
4225b3c049e70834cf33790a28643ab058b507b35cBen Cheng  AsmScn_t *runp;
4325b3c049e70834cf33790a28643ab058b507b35cBen Cheng  AsmScn_t *newp;
4425b3c049e70834cf33790a28643ab058b507b35cBen Cheng
4525b3c049e70834cf33790a28643ab058b507b35cBen Cheng  /* Just return if no section is given.  The error must have been
4625b3c049e70834cf33790a28643ab058b507b35cBen Cheng     somewhere else.  */
4725b3c049e70834cf33790a28643ab058b507b35cBen Cheng  if (asmscn == NULL)
4825b3c049e70834cf33790a28643ab058b507b35cBen Cheng    return NULL;
4925b3c049e70834cf33790a28643ab058b507b35cBen Cheng
5025b3c049e70834cf33790a28643ab058b507b35cBen Cheng  /* Determine whether there is already a subsection with this number.  */
5125b3c049e70834cf33790a28643ab058b507b35cBen Cheng  runp = asmscn->subsection_id == 0 ? asmscn : asmscn->data.up;
5225b3c049e70834cf33790a28643ab058b507b35cBen Cheng  while (1)
5325b3c049e70834cf33790a28643ab058b507b35cBen Cheng    {
5425b3c049e70834cf33790a28643ab058b507b35cBen Cheng      if (runp->subsection_id == nr)
5525b3c049e70834cf33790a28643ab058b507b35cBen Cheng	/* Found it.  */
5625b3c049e70834cf33790a28643ab058b507b35cBen Cheng	return runp;
5725b3c049e70834cf33790a28643ab058b507b35cBen Cheng
5825b3c049e70834cf33790a28643ab058b507b35cBen Cheng      if (runp->subnext == NULL || runp->subnext->subsection_id > nr)
5925b3c049e70834cf33790a28643ab058b507b35cBen Cheng	break;
6025b3c049e70834cf33790a28643ab058b507b35cBen Cheng
6125b3c049e70834cf33790a28643ab058b507b35cBen Cheng      runp = runp->subnext;
6225b3c049e70834cf33790a28643ab058b507b35cBen Cheng    }
6325b3c049e70834cf33790a28643ab058b507b35cBen Cheng
6425b3c049e70834cf33790a28643ab058b507b35cBen Cheng  newp = (AsmScn_t *) malloc (sizeof (AsmScn_t));
6525b3c049e70834cf33790a28643ab058b507b35cBen Cheng  if (newp == NULL)
6625b3c049e70834cf33790a28643ab058b507b35cBen Cheng    return NULL;
6725b3c049e70834cf33790a28643ab058b507b35cBen Cheng
6825b3c049e70834cf33790a28643ab058b507b35cBen Cheng  /* Same assembler context than the original section.  */
6925b3c049e70834cf33790a28643ab058b507b35cBen Cheng  newp->ctx = runp->ctx;
7025b3c049e70834cf33790a28643ab058b507b35cBen Cheng
7125b3c049e70834cf33790a28643ab058b507b35cBen Cheng  /* User provided the subsectio nID.  */
7225b3c049e70834cf33790a28643ab058b507b35cBen Cheng  newp->subsection_id = nr;
7325b3c049e70834cf33790a28643ab058b507b35cBen Cheng
7425b3c049e70834cf33790a28643ab058b507b35cBen Cheng  /* Inherit the parent's type.  */
7525b3c049e70834cf33790a28643ab058b507b35cBen Cheng  newp->type = runp->type;
7625b3c049e70834cf33790a28643ab058b507b35cBen Cheng
7725b3c049e70834cf33790a28643ab058b507b35cBen Cheng  /* Pointer to the zeroth subsection.  */
7825b3c049e70834cf33790a28643ab058b507b35cBen Cheng  newp->data.up = runp->subsection_id == 0 ? runp : runp->data.up;
7925b3c049e70834cf33790a28643ab058b507b35cBen Cheng
8025b3c049e70834cf33790a28643ab058b507b35cBen Cheng  /* We start at offset zero.  */
8125b3c049e70834cf33790a28643ab058b507b35cBen Cheng  newp->offset = 0;
8225b3c049e70834cf33790a28643ab058b507b35cBen Cheng  /* And generic alignment.  */
8325b3c049e70834cf33790a28643ab058b507b35cBen Cheng  newp->max_align = 1;
8425b3c049e70834cf33790a28643ab058b507b35cBen Cheng
8525b3c049e70834cf33790a28643ab058b507b35cBen Cheng  /* No output yet.  */
8625b3c049e70834cf33790a28643ab058b507b35cBen Cheng  newp->content = NULL;
8725b3c049e70834cf33790a28643ab058b507b35cBen Cheng
8825b3c049e70834cf33790a28643ab058b507b35cBen Cheng  /* Inherit the fill pattern from the section this one is derived from.  */
8925b3c049e70834cf33790a28643ab058b507b35cBen Cheng  newp->pattern = asmscn->pattern;
9025b3c049e70834cf33790a28643ab058b507b35cBen Cheng
9125b3c049e70834cf33790a28643ab058b507b35cBen Cheng  /* Enqueue at the right position in the list.  */
9225b3c049e70834cf33790a28643ab058b507b35cBen Cheng  newp->subnext = runp->subnext;
9325b3c049e70834cf33790a28643ab058b507b35cBen Cheng  runp->subnext = newp;
9425b3c049e70834cf33790a28643ab058b507b35cBen Cheng
9525b3c049e70834cf33790a28643ab058b507b35cBen Cheng  return newp;
9625b3c049e70834cf33790a28643ab058b507b35cBen Cheng}
97